Last active
December 15, 2015 17:49
-
-
Save murbanski/5299475 to your computer and use it in GitHub Desktop.
FactoryGirl bottlenecks
This file contains hidden or 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
require 'rspec/rails' | |
# ... | |
$factories_speed = Hash.new {|h, k| h[k] = {calls: 0, duration: 0} } | |
ActiveSupport::Notifications.subscribe("factory_girl.run_factory") do |*args| | |
event = ActiveSupport::Notifications::Event.new(*args) | |
key = "#{event.payload[:strategy]}_#{event.payload[:name]}" | |
$factories_speed[key][:calls] += 1 | |
$factories_speed[key][:duration] += event.duration | |
end | |
RSpec.configure do |config| | |
config.after(:suite) do | |
STDERR.puts Hash[$factories_speed.sort_by { |k, v| v[:duration] }.reverse] | |
total_duration = $factories_speed.sum {|k, v| v[:duration] } | |
STDERR.puts "Total FactoryGirl duration: #{total_duration}" | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment