-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathfactory_girl_profiler.rb
50 lines (40 loc) · 1.46 KB
/
factory_girl_profiler.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class FactoryGirlProfiler
attr_accessor :results
def self.setup
profiler = self.new
RSpec.configure do |config|
config.before(:suite) { profiler.subscribe }
config.after(:suite) { profiler.dump }
end
end
def initialize
self.results = {}
end
def subscribe
ActiveSupport::Notifications.subscribe("factory_girl.run_factory") do |name, start, finish, id, payload|
factory, strategy = payload.values_at(:name, :strategy)
factory_result = results[factory] ||= {}
strategy_result = factory_result[strategy] ||= { duration_in_secs: 0.0, count: 0 }
duration_in_secs = finish - start
strategy_result[:duration_in_secs] += duration_in_secs
strategy_result[:count] += 1
end
end
def dump
puts "\nFactoryGirl Profiles"
total_in_secs = 0.0
results.each do |factory_name, factory_profile|
puts "\n #{factory_name}"
factory_profile.each do |strategy, profile|
puts " #{strategy} called #{profile[:count]} times took #{profile[:duration_in_secs].round(2)} seconds total"
total_in_secs += profile[:duration_in_secs]
end
end
puts "\n Total FactoryGirl time #{total_in_secs.round(2)} seconds"
end
end
RSpec.configure do |config|
config.add_setting :profile_factories, default: false
config.profile_factories = config.profile_examples? || ARGV.include?('--profile') || ARGV.include?('-p')
FactoryGirlProfiler.setup if config.profile_factories?
end