Skip to content

Instantly share code, notes, and snippets.

@mhorbul
Created March 29, 2010 21:44
Show Gist options
  • Save mhorbul/348450 to your computer and use it in GitHub Desktop.
Save mhorbul/348450 to your computer and use it in GitHub Desktop.
namespace :benchmark do
namespace :find do
desc "Benchmarking find by id"
task :simple => :environment do
Benchmark.bm(20) do |bm|
bm.report 'find-by-id' do
5_000.times do
Person.find(100)
end
end
bm.report 'find-by-id cached' do
5_000.times do
Rails.cache.fetch("person/100", :expires_in => 5.minutes) do
Person.find(100)
end
end
end
end
end
desc "Benchmarking find complex"
task :complex => :environment do
Benchmark.bm(20) do |bm|
created_at = 2.days.ago
bm.report 'find-by-attributes' do
5_000.times do
Person.find_all_by_name_and_email_and_created_at(
'username%', '%domain.com', created_at,
:limit => 50, :order => "id desc")
end
end
ActiveRecord::Base.
connection.execute("CREATE INDEX " +
"index_people_on_name_and_email_and_created_at " +
"ON people (name, email, created_at)")
bm.report 'find-by-attributes-indexed' do
5_000.times do
Person.find_all_by_name_and_email_and_created_at(
'username%', '%domain.com', created_at,
:limit => 50, :order => "id desc")
end
end
ActiveRecord::Base.
connection.execute("DROP INDEX " +
"index_people_on_name_and_email_and_created_at on people")
bm.report 'find-by-attributes cached' do
5_000.times do
Rails.cache.fetch("person/name/email/created_at") do
Person.find_all_by_name_and_email_and_created_at(
'username%', '%domain.com', created_at,
:limit => 50, :order => "id desc")
end
end
end
end
end
desc "Benchmarking find with cache-money"
task :cache_money => :environment do
created_at = 2.days.ago
ActiveRecord::Base.is_cached :repository => $cache
Object.class_eval do
remove_const("Person") if const_defined? "Person"
end
load 'person.rb'
Person.index [:name, :email, :created_at]
Benchmark.bm(20) do |bm|
bm.report 'find-by-id (cache-money)' do
5_000.times do
Person.find(100)
end
end
$memcache.flush_all
bm.report 'find-by-attributes (cache-money)' do
5_000.times do
Person.find_all_by_name_and_email_and_created_at(
'username%', '%domain.com', created_at)
end
end
end
end
end
end
user system total real
find-by-id 1.860000 0.210000 2.070000 ( 2.751018)
find-by-id cached 2.630000 0.220000 2.850000 ( 3.586115)
find-by-attributes 2.900000 0.290000 3.190000 ( 9.364337)
find-by-attributes indexed 2.710000 0.270000 2.980000 ( 4.006333)
find-by-attributes cached 1.390000 0.180000 1.570000 ( 2.121709)
find-by-id (cache-money) 3.230000 0.170000 3.400000 ( 4.025392)
find-by-attributes (cache-money) 3.160000 0.170000 3.330000 ( 3.830050)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment