Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8818: perform.rb

File perform.rb, 1.2 kB (added by lifofifo, 2 years ago)

Performance test for rails

Line 
1 require 'benchmark'
2
3 messages = Message.find :all # 800 records
4 n = 10000
5
6 puts "Before patching..\n"
7
8 Benchmark.bm do |x|
9   # String
10   x.report { n.times { messages.map{|m| m.sender} } }
11   x.report { n.times { messages.map(&:sender) } }
12  
13   # Integer
14   x.report { n.times { messages.map{|m| m.id} } }
15   x.report { n.times { messages.map(&:id) } }
16 end
17
18 class Symbol
19   def to_proc
20     lambda { |obj| obj.send self }
21   end
22 end
23
24 puts "After patching..\n"
25
26 Benchmark.bm do |x|
27   # String
28   x.report { n.times { messages.map{|m| m.sender} } }
29   x.report { n.times { messages.map(&:sender) } }
30  
31   # Integer
32   x.report { n.times { messages.map{|m| m.id} } }
33   x.report { n.times { messages.map(&:id) } }
34 end
35
36 # $ script/runner perform.rb
37 # Before patching..
38 #       user     system      total        real
39 #  10.560000   0.070000  10.630000 ( 10.847635)
40 #  76.020000   0.500000  76.520000 ( 78.656028)
41 #   9.850000   0.070000   9.920000 ( 10.262587)
42 #  75.170000   0.490000  75.660000 ( 77.694786)
43 # After patching..
44 #       user     system      total        real
45 #  10.570000   0.070000  10.640000 ( 10.864351)
46 #  11.930000   0.080000  12.010000 ( 12.352464)
47 #   9.850000   0.070000   9.920000 ( 10.218872)
48 #  11.360000   0.090000  11.450000 ( 11.899277)