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
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 "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
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)
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