Created
May 16, 2013 15:32
-
-
Save divineforest/5592598 to your computer and use it in GitHub Desktop.
factory_girl benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
config.before(:suite) do | |
$factory_girl_results = {} | |
$factory_girl_total_times = {} | |
$factory_girl_total_time = 0 | |
ActiveSupport::Notifications.subscribe("factory_girl.run_factory") do |name, start, finish, id, payload| | |
factory_name = payload[:name] | |
strategy_name = payload[:strategy] | |
$factory_girl_results[factory_name] ||= {} | |
$factory_girl_results[factory_name][strategy_name] ||= 0 | |
$factory_girl_results[factory_name][strategy_name] += 1 | |
execution_time_in_seconds = finish - start | |
$factory_girl_total_time += execution_time_in_seconds | |
$factory_girl_total_times[factory_name] ||= 0 | |
$factory_girl_total_times[factory_name] += execution_time_in_seconds | |
end | |
end | |
config.after(:suite) do | |
puts "FactoryGirl — Performance" | |
factory_names_sorted_by_time = $factory_girl_total_times.sort_by { |_, time| time }.map(&:first).reverse[0..9] | |
factory_names_sorted_by_time.each do |factory_name| | |
strategies = $factory_girl_results[factory_name] | |
total_time_in_seconds = $factory_girl_total_times[factory_name] | |
minutes = (total_time_in_seconds / 60.0).to_i | |
seconds = (total_time_in_seconds % 60).to_i | |
time_string = "#{(minutes.to_s + 'm ') if minutes != 0}#{seconds}s" | |
puts "#{factory_name.upcase} — #{time_string}" | |
strategies.each do |strategy_name, strategy_calls| | |
puts " #{strategy_name}: #{strategy_calls}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment