1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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.
This commit is contained in:
Jon Leighton 2012-08-17 11:41:50 +01:00
parent 17bb324f38
commit 1411fc1986
2 changed files with 36 additions and 39 deletions

View file

@ -96,3 +96,5 @@ end
# A gem necessary for ActiveRecord tests with IBM DB # A gem necessary for ActiveRecord tests with IBM DB
gem 'ibm_db' if ENV['IBM_DB'] gem 'ibm_db' if ENV['IBM_DB']
gem 'benchmark-ips'

View file

@ -1,7 +1,6 @@
TIMES = (ENV['N'] || 10000).to_i
require File.expand_path('../../../load_paths', __FILE__) require File.expand_path('../../../load_paths', __FILE__)
require "active_record" require "active_record"
require 'benchmark/ips'
conn = { :adapter => 'sqlite3', :database => ':memory:' } conn = { :adapter => 'sqlite3', :database => ':memory:' }
@ -88,9 +87,22 @@ puts 'Inserting 10,000 users and exhibits...'
) )
end 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
Benchmark.bm(46) do |x| 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.ips do |x|
ar_obj = Exhibit.find(1) ar_obj = Exhibit.find(1)
attrs = { :name => 'sam' } attrs = { :name => 'sam' }
attrs_first = { :name => 'sam' } attrs_first = { :name => 'sam' }
@ -101,77 +113,60 @@ Benchmark.bm(46) do |x|
:created_at => Date.today :created_at => Date.today
} }
x.report("Model#id (x#{(TIMES * 100).ceil})") do x.report("Model#id") do
(TIMES * 100).ceil.times { ar_obj.id } ar_obj.id
end end
x.report 'Model.new (instantiation)' do x.report 'Model.new (instantiation)' do
TIMES.times { Exhibit.new } Exhibit.new
end end
x.report 'Model.new (setting attributes)' do x.report 'Model.new (setting attributes)' do
TIMES.times { Exhibit.new(attrs) } Exhibit.new(attrs)
end end
x.report 'Model.first' do x.report 'Model.first' do
TIMES.times { Exhibit.first.look } Exhibit.first.look
end end
x.report 'Model.named_scope' do x.report 'Model.named_scope' do
TIMES.times { Exhibit.limit(10).with_name.with_notes } 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) }
end end
x.report 'Model.create' do x.report 'Model.create' do
TIMES.times { Exhibit.create(exhibit) } Exhibit.create(exhibit)
end end
x.report 'Resource#attributes=' do x.report 'Resource#attributes=' do
TIMES.times { e = Exhibit.new(attrs_first)
exhibit = Exhibit.new(attrs_first) e.attributes = attrs_second
exhibit.attributes = attrs_second
}
end end
x.report 'Resource#update' do x.report 'Resource#update' do
TIMES.times { Exhibit.first.update_attributes(:name => 'bob') } Exhibit.first.update_attributes(:name => 'bob')
end end
x.report 'Resource#destroy' do x.report 'Resource#destroy' do
TIMES.times { Exhibit.first.destroy } Exhibit.first.destroy
end end
x.report 'Model.transaction' do x.report 'Model.transaction' do
TIMES.times { Exhibit.transaction { Exhibit.new } } Exhibit.transaction { Exhibit.new }
end end
x.report 'Model.find(id)' do x.report 'Model.find(id)' do
id = Exhibit.first.id User.find(1)
TIMES.times { Exhibit.find(id) }
end end
x.report 'Model.find_by_sql' do 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 end
x.report "Model.log x(#{TIMES * 10})" do x.report "Model.log" do
(TIMES * 10).times { Exhibit.connection.send(:log, "hello", "world") {} } Exhibit.connection.send(:log, "hello", "world") {}
end end
x.report "AR.execute(query) (#{TIMES / 2})" do x.report "AR.execute(query)" do
(TIMES / 2).times { ActiveRecord::Base.connection.execute("Select * from exhibits where id = #{(rand * 1000 + 1).to_i}") } ActiveRecord::Base.connection.execute("Select * from exhibits where id = #{(rand * 1000 + 1).to_i}")
end end
end end