class FactoryGirlProfiling::Profiler
Attributes
output_buffer[W]
Public Class Methods
instance()
click to toggle source
# File lib/factory_girl_profiling/profiler.rb, line 4 def self.instance @instance ||= new end
new()
click to toggle source
# File lib/factory_girl_profiling/profiler.rb, line 16 def initialize reset end
Public Instance Methods
add(name, start, finish, id, payload)
click to toggle source
# File lib/factory_girl_profiling/profiler.rb, line 25 def add(name, start, finish, id, payload) add_counting(payload[:name], payload[:strategy]) add_timing(payload[:name], payload[:strategy], finish - start) end
add_counting(name, strategy)
click to toggle source
# File lib/factory_girl_profiling/profiler.rb, line 41 def add_counting(name, strategy) @factory_girl_results[name] ||= {} @factory_girl_results[name][strategy] ||= 0 @factory_girl_results[name][strategy] += 1 end
add_timing(name, strategy, timing)
click to toggle source
# File lib/factory_girl_profiling/profiler.rb, line 30 def add_timing(name, strategy, timing) @factory_girl_timing_results[name] ||= {} @factory_girl_timing_results[name][strategy] ||= [] @factory_girl_timing_results[name][strategy] << timing #if timing >= 0.5 #output_string "Slow factory: #{name} using strategy #{strategy}" #end end
output_buffer()
click to toggle source
# File lib/factory_girl_profiling/profiler.rb, line 84 def output_buffer @output_buffer ||= $stdout end
output_string(str)
click to toggle source
# File lib/factory_girl_profiling/profiler.rb, line 79 def output_string(str) output_buffer.puts str end
print_counting_results()
click to toggle source
# File lib/factory_girl_profiling/profiler.rb, line 52 def print_counting_results output_string 'FactoryGirl counting:' sort_data_hash(@factory_girl_results).each do |factory, values| counts_for_strategies = values.inject([]){|s, (strategy, count)| s << "#{strategy} = #{count}"} output_string "#{"#{factory}:".ljust(30)} #{"%15s \t" * counts_for_strategies.size}" % counts_for_strategies end output_string "\n" end
print_results()
click to toggle source
# File lib/factory_girl_profiling/profiler.rb, line 47 def print_results print_counting_results print_timing_results end
print_timing_results()
click to toggle source
# File lib/factory_girl_profiling/profiler.rb, line 61 def print_timing_results output_string 'FactoryGirl timing:' sort_value_proc = ->(val) { val.values.map{|e| e.reduce(0, &:+)}.reduce(0, &:+) / val.values.map(&:size).reduce(0, &:+) } sort_data_hash(@factory_girl_timing_results, sort_value_proc).each do |factory, data| output_string factory data.each do |strategy, times| output_string "#{"#{strategy}:".ljust(10)} avg=#{(times.reduce(0, &:+) / times.size).round(2)} \t(min=#{times.min.round(2)},\tmax=#{times.max.round(2)})" end output_string "" end output_string "\n" end
reset()
click to toggle source
# File lib/factory_girl_profiling/profiler.rb, line 20 def reset @factory_girl_results = {} @factory_girl_timing_results = {} end
sort_data_hash(hash, sort_value_proc = nil)
click to toggle source
# File lib/factory_girl_profiling/profiler.rb, line 74 def sort_data_hash(hash, sort_value_proc = nil) sort_value_proc ||= Proc.new {|val| val.values.reduce(0, &:+) } hash.to_a.sort {|(_, v1), (_, v2)| sort_value_proc.call(v2) <=> sort_value_proc.call(v1)} end