aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/examples
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2012-08-17 11:41:50 +0100
committerJon Leighton <j@jonathanleighton.com>2012-08-17 11:43:48 +0100
commitbfff0ed33301aaf2c917d0b8ac8eaffc0593281e (patch)
tree6224adad30a633160fa399250cd436e2fdcc6449 /activerecord/examples
parent4e31f826dc5a833d3fb4e0e41b0eb146dde04282 (diff)
downloadrails-bfff0ed33301aaf2c917d0b8ac8eaffc0593281e.tar.gz
rails-bfff0ed33301aaf2c917d0b8ac8eaffc0593281e.tar.bz2
rails-bfff0ed33301aaf2c917d0b8ac8eaffc0593281e.zip
Use benchmark/ips to measure AR performance
This means we can more easily compare numbers, and we don't have to specify a single N for all reports, which previously meant that some tests were running many more/fewer iterations than necessary.
Diffstat (limited to 'activerecord/examples')
-rw-r--r--activerecord/examples/performance.rb73
1 files changed, 34 insertions, 39 deletions
diff --git a/activerecord/examples/performance.rb b/activerecord/examples/performance.rb
index 31f3e02bb8..55679b644c 100644
--- a/activerecord/examples/performance.rb
+++ b/activerecord/examples/performance.rb
@@ -1,7 +1,6 @@
-TIMES = (ENV['N'] || 10000).to_i
-
require File.expand_path('../../../load_paths', __FILE__)
require "active_record"
+require 'benchmark/ips'
conn = { :adapter => 'sqlite3', :database => ':memory:' }
@@ -88,9 +87,22 @@ puts 'Inserting 10,000 users and exhibits...'
)
end
-require 'benchmark'
+# These ones need more than 5 secs in order to get a useful result
+Benchmark.ips(20) do |x|
+ x.report("Model.all limit(100)") do
+ Exhibit.look Exhibit.limit(100)
+ end
+
+ x.report "Model.all limit(100) with relationship" do
+ Exhibit.feel Exhibit.limit(100).includes(:user)
+ end
+
+ x.report "Model.all limit(10,000)" do
+ Exhibit.look Exhibit.limit(10000)
+ end
+end
-Benchmark.bm(46) do |x|
+Benchmark.ips do |x|
ar_obj = Exhibit.find(1)
attrs = { :name => 'sam' }
attrs_first = { :name => 'sam' }
@@ -101,77 +113,60 @@ Benchmark.bm(46) do |x|
:created_at => Date.today
}
- x.report("Model#id (x#{(TIMES * 100).ceil})") do
- (TIMES * 100).ceil.times { ar_obj.id }
+ x.report("Model#id") do
+ ar_obj.id
end
x.report 'Model.new (instantiation)' do
- TIMES.times { Exhibit.new }
+ Exhibit.new
end
x.report 'Model.new (setting attributes)' do
- TIMES.times { Exhibit.new(attrs) }
+ Exhibit.new(attrs)
end
x.report 'Model.first' do
- TIMES.times { Exhibit.first.look }
+ Exhibit.first.look
end
x.report 'Model.named_scope' do
- TIMES.times { Exhibit.limit(10).with_name.with_notes }
- end
-
- x.report("Model.all limit(100) (x#{(TIMES / 10).ceil})") do
- (TIMES / 10).ceil.times { Exhibit.look Exhibit.limit(100) }
- end
-
- x.report "Model.all limit(100) with relationship (x#{(TIMES / 10).ceil})" do
- (TIMES / 10).ceil.times { Exhibit.feel Exhibit.limit(100).includes(:user) }
- end
-
- x.report "Model.all limit(10,000) x(#{(TIMES / 1000).ceil})" do
- (TIMES / 1000).ceil.times { Exhibit.look Exhibit.limit(10000) }
+ Exhibit.limit(10).with_name.with_notes
end
x.report 'Model.create' do
- TIMES.times { Exhibit.create(exhibit) }
+ Exhibit.create(exhibit)
end
x.report 'Resource#attributes=' do
- TIMES.times {
- exhibit = Exhibit.new(attrs_first)
- exhibit.attributes = attrs_second
- }
+ e = Exhibit.new(attrs_first)
+ e.attributes = attrs_second
end
x.report 'Resource#update' do
- TIMES.times { Exhibit.first.update_attributes(:name => 'bob') }
+ Exhibit.first.update_attributes(:name => 'bob')
end
x.report 'Resource#destroy' do
- TIMES.times { Exhibit.first.destroy }
+ Exhibit.first.destroy
end
x.report 'Model.transaction' do
- TIMES.times { Exhibit.transaction { Exhibit.new } }
+ Exhibit.transaction { Exhibit.new }
end
x.report 'Model.find(id)' do
- id = Exhibit.first.id
- TIMES.times { Exhibit.find(id) }
+ User.find(1)
end
x.report 'Model.find_by_sql' do
- TIMES.times {
- Exhibit.find_by_sql("SELECT * FROM exhibits WHERE id = #{(rand * 1000 + 1).to_i}").first
- }
+ Exhibit.find_by_sql("SELECT * FROM exhibits WHERE id = #{(rand * 1000 + 1).to_i}").first
end
- x.report "Model.log x(#{TIMES * 10})" do
- (TIMES * 10).times { Exhibit.connection.send(:log, "hello", "world") {} }
+ x.report "Model.log" do
+ Exhibit.connection.send(:log, "hello", "world") {}
end
- x.report "AR.execute(query) (#{TIMES / 2})" do
- (TIMES / 2).times { ActiveRecord::Base.connection.execute("Select * from exhibits where id = #{(rand * 1000 + 1).to_i}") }
+ x.report "AR.execute(query)" do
+ ActiveRecord::Base.connection.execute("Select * from exhibits where id = #{(rand * 1000 + 1).to_i}")
end
end