module TestProf::FactoryProf

FactoryProf collects “factory stacks” that can be used to build flamegraphs or detect most popular factories

Constants

FACTORY_BUILDERS

Public Class Methods

config() click to toggle source
# File lib/test_prof/factory_prof.rb, line 73
def config
  @config ||= Configuration.new
end
configure() { |config| ... } click to toggle source
# File lib/test_prof/factory_prof.rb, line 77
def configure
  yield config
end
init() click to toggle source

Patch factory lib, init vars

# File lib/test_prof/factory_prof.rb, line 82
def init
  @running = false

  log :info, "FactoryProf enabled (#{config.mode} mode)"

  patch!
end
patch!() click to toggle source
# File lib/test_prof/factory_prof.rb, line 90
def patch!
  return if @patched

  FACTORY_BUILDERS.each(&:patch)

  @patched = true
end
result() click to toggle source
# File lib/test_prof/factory_prof.rb, line 121
def result
  Result.new(@stacks, @stats)
end
run() click to toggle source

Inits FactoryProf and setups at exit hook, then runs

# File lib/test_prof/factory_prof.rb, line 100
def run
  init

  printer = config.printer

  started_at = TestProf.now

  at_exit { printer.dump(result, start_time: started_at) }

  start
end
start() click to toggle source
# File lib/test_prof/factory_prof.rb, line 112
def start
  reset!
  @running = true
end
stop() click to toggle source
# File lib/test_prof/factory_prof.rb, line 117
def stop
  @running = false
end
track(factory) { || ... } click to toggle source
# File lib/test_prof/factory_prof.rb, line 125
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 Class Methods

flush_stack() click to toggle source
# File lib/test_prof/factory_prof.rb, line 161
def flush_stack
  return unless config.flamegraph?
  @stacks << @current_stack unless @current_stack.nil? || @current_stack.empty?
  @current_stack = []
end
reset!() click to toggle source
# File lib/test_prof/factory_prof.rb, line 146
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
running?() click to toggle source
# File lib/test_prof/factory_prof.rb, line 167
def running?
  @running == true
end