Module: FactoryGirl::Benchmark
- Defined in:
- lib/factory_girl/benchmark.rb,
lib/factory_girl/benchmark/version.rb
Constant Summary collapse
- VERSION =
'0.0.1'
Class Method Summary collapse
- .benching=(o) ⇒ Object
-
.benchmark! ⇒ Object
Example usage.
- .benchmarks ⇒ Object
- .bm(sym, args) ⇒ Object
- .color ⇒ Object
- .is_benching? ⇒ Boolean
- .print_report(arr) ⇒ Object
- .report ⇒ Object
Class Method Details
.benching=(o) ⇒ Object
63 64 65 |
# File 'lib/factory_girl/benchmark.rb', line 63 def benching=(o) @benching = o end |
.benchmark! ⇒ Object
Example usage
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/factory_girl/benchmark.rb', line 21 def benchmark! # Install FactoryGirl.singleton_class.prepend(FactoryGirl::Benchmark) # Run FactoryGirl.find_definitions if FactoryGirl.factories.none? FactoryGirl.factories.map(&:name).each do |f| begin FactoryGirl.create(f) rescue => e puts "Couldn't benchmark factory #{f} due to #{e}" end end # Report FactoryGirl::Benchmark.report end |
.benchmarks ⇒ Object
39 40 41 |
# File 'lib/factory_girl/benchmark.rb', line 39 def benchmarks @benchmarks ||= {} end |
.bm(sym, args) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/factory_girl/benchmark.rb', line 47 def bm(sym, args) benching = true rv = nil bm = ::Benchmark.measure { rv = yield } record = { sym: sym, bm: bm.real, args: args, bt: caller.select {|x| x[/#{Rails.root}/]} } key = "FactoryGirl.#{sym}##{args.first}" benchmarks[key] ||= [] benchmarks[key] << record rv ensure benching = false end |
.color ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/factory_girl/benchmark.rb', line 99 def color @last_color = case @last_color when nil then :yellow when :yellow then :light_cyan when :light_cyan then :yellow end end |
.is_benching? ⇒ Boolean
43 44 45 |
# File 'lib/factory_girl/benchmark.rb', line 43 def is_benching? @benching end |
.print_report(arr) ⇒ Object
93 94 95 96 97 |
# File 'lib/factory_girl/benchmark.rb', line 93 def print_report(arr) require 'colorize' arr.first(20).map {|h| puts "\t#{h}".colorize(color)} puts end |
.report ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/factory_girl/benchmark.rb', line 67 def report puts 'Most frequent' r = benchmarks.reduce([]) do |m, (k, _)| m << { key: k, count: benchmarks[k].size } end.sort {|a,b| b[:count] <=> a[:count]} print_report(r) puts 'Slowest instances' r = benchmarks.each_value.to_a.flatten.sort {|a,b| b[:bm] <=> a[:bm]} print_report(r) puts 'Slowest total' r = benchmarks.reduce([]) do |m, (k, _)| m << { key: k, count: benchmarks[k].size, total_time: benchmarks[k].reduce(0) {|sum, bm| sum + bm[:bm]} } end.sort {|a,b| b[:total_time] <=> a[:total_time]} print_report(r) puts 'Slowest average' r = benchmarks.reduce([]) do |m, (k, _)| total_time = benchmarks[k].reduce(0) {|sum, bm| sum + bm[:bm]} count = benchmarks[k].size m << { key: k, avg: total_time.to_f/count, count: count, total_time: total_time } end.sort {|a,b| b[:avg] <=> a[:avg]} print_report(r) end |