Skip to content

Instantly share code, notes, and snippets.

@murbanski
Last active December 15, 2015 17:49
Show Gist options
  • Save murbanski/5299475 to your computer and use it in GitHub Desktop.
Save murbanski/5299475 to your computer and use it in GitHub Desktop.
FactoryGirl bottlenecks
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