views:

11

answers:

2

I'm looking to benchmark a couple of my ActiveRecord requests in my app. What's the simplest way in the console to benchmark something like

User.find_by_name("Joe").id

versus

User.find(:first, :select => :id, :conditions => ["name = ?","Joe"]).id

Thanks

+1  A: 

In development mode each query is timed and logged in log/development.log. You'll have lines like:

Ad Load (1.4ms)  SELECT "ads".* FROM "ads" ORDER BY created_at DESC
Slobodan Kovacevic
A: 

Use script/performance/benchmarker:

script/performance/benchmarker 2000 "User.find_by_name('Joe').id" "User.first(:conditions => {:name => 'Joe'}, :select => 'id').id"

On my dev machine, this reports:

            user     system      total        real
#1      1.110000   0.070000   1.180000 (  1.500366)
#2      0.800000   0.050000   0.850000 (  1.078444)

Thus, the 2nd method appears to be faster, since it has less work to do. Of course, you should benchmark this on your production machine, using the production environment:

RAILS_ENV=production script/performance/benchmarker 2000 "User.find_by_name('Joe').id" "User.first(:conditions => {:name => 'Joe'}, :select => 'id').id"

It might change conditions a bit for you.

François Beausoleil