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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
# frozen_string_literal: true
require "test_prof/factory_prof/printers/simple"
require "test_prof/factory_prof/printers/flamegraph"
require "test_prof/factory_prof/factory_builders/factory_bot"
require "test_prof/factory_prof/factory_builders/fabrication"
module TestProf
# FactoryProf collects "factory stacks" that can be used to build
# flamegraphs or detect most popular factories
module FactoryProf
FACTORY_BUILDERS = [FactoryBuilders::FactoryBot,
FactoryBuilders::Fabrication].freeze
# FactoryProf configuration
class Configuration
attr_accessor :mode
def initialize
@mode = ENV["FPROF"] == "flamegraph" ? :flamegraph : :simple
end
# Whether we want to generate flamegraphs
def flamegraph?
@mode == :flamegraph
end
end
class Result # :nodoc:
attr_reader :stacks, :raw_stats
def initialize(stacks, raw_stats)
@stacks = stacks
@raw_stats = raw_stats
end
# Returns sorted stats
def stats
@stats ||= @raw_stats.values
.sort_by { |el| -el[:total_count] }
end
def total_count
@total_count ||= @raw_stats.values.sum { |v| v[:total_count] }
end
def total_time
@total_time ||= @raw_stats.values.sum { |v| v[:total_time] }
end
private
def sorted_stats(key)
@raw_stats.values
.map { |el| [el[:name], el[key]] }
.sort_by { |el| -el[1] }
end
end
class << self
include TestProf::Logging
def config
@config ||= Configuration.new
end
def configure
yield config
end
# Patch factory lib, init vars
def init
@running = false
log :info, "FactoryProf enabled (#{config.mode} mode)"
FACTORY_BUILDERS.each(&:patch)
end
# Inits FactoryProf and setups at exit hook,
# then runs
def run
init
printer = config.flamegraph? ? Printers::Flamegraph : Printers::Simple
at_exit { printer.dump(result) }
start
end
def start
reset!
@running = true
end
def stop
@running = false
end
def result
Result.new(@stacks, @stats)
end
def track(factory)
return yield unless running?
@depth += 1
@current_stack << factory if config.flamegraph?
@stats[factory][:total_count] += 1
@stats[factory][:top_level_count] += 1 if @depth == 1
t1 = TestProf.now
begin
yield
ensure
t2 = TestProf.now
elapsed = t2 - t1
@stats[factory][:total_time] += elapsed
@stats[factory][:top_level_time] += elapsed if @depth == 1
@depth -= 1
flush_stack if @depth.zero?
end
end
private
def reset!
@stacks = [] if config.flamegraph?
@depth = 0
@stats = Hash.new do |h, k|
h[k] = {
name: k,
total_count: 0,
top_level_count: 0,
total_time: 0.0,
top_level_time: 0.0
}
end
flush_stack
end
def flush_stack
return unless config.flamegraph?
@stacks << @current_stack unless @current_stack.nil? || @current_stack.empty?
@current_stack = []
end
def running?
@running == true
end
end
end
end
TestProf.activate("FPROF") do
TestProf::FactoryProf.run
end
|