Skip to content

Instantly share code, notes, and snippets.

@srt32
Last active August 29, 2015 13:59
Show Gist options
  • Save srt32/10674145 to your computer and use it in GitHub Desktop.
Save srt32/10674145 to your computer and use it in GitHub Desktop.
benchmark_funds.rake_v0
desc 'Benchmark funds_raised methods'
ITERATIONS = 100_000
task benchmark_sql: :environment do
GC.disable
campaign = Campaign.last
Benchmark.bm do |bm|
bm.report('sql') do
ITERATIONS.times do
campaign.funds_raised_sql
Campaign.connection.clear_query_cache
end
end
end
GC.enable
end
task benchmark_ruby: :environment do
GC.disable
campaign = Campaign.last
Benchmark.bm do |bm|
bm.report('ruby') do
ITERATIONS.times do
campaign.funds_raised_ruby
Campaign.connection.clear_query_cache
end
end
end
GC.enable
end
@croaky
Copy link

croaky commented Apr 14, 2014

I don't think you want to execute the campaign.funds_raised_sql and campaign.funds_raised_ruby methods 100,000 times. I think you want to create 100,000+ records in the database (with something logical like 20-100 loans per campaign) and run those two methods once each, benchmarking their one execution time.

@srt32
Copy link
Author

srt32 commented Apr 14, 2014

@croaky, sorry for the delay. I didn't see your comment. The data that is behind the benchmark is as you mentioned (with 1000 loans per campaign, actually)

campaigns = FactoryGirl.create_list(:campaign, 100)

campaigns.each do |campaign|
  FactoryGirl.create_list(:loan, 1000, campaign: campaign)
end

The methods get called 100_000 times (perhaps too many times) to throw out any local blips or hiccups in memory, GC, other processes. The difference in time turned out to be so massive that, in the end, I think a single run would have sufficed.

@srt32
Copy link
Author

srt32 commented Apr 14, 2014

There is an updated gist at https://gist.github.com/srt32/10687302.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment