Module: TestProf::FactoryProf

Extended by:
Logging
Defined in:
lib/test_prof/factory_prof.rb,
lib/test_prof/factory_prof/printers/simple.rb,
lib/test_prof/factory_prof/fabrication_patch.rb,
lib/test_prof/factory_prof/factory_bot_patch.rb,
lib/test_prof/factory_prof/printers/flamegraph.rb,
lib/test_prof/factory_prof/factory_builders/fabrication.rb,
lib/test_prof/factory_prof/factory_builders/factory_bot.rb

Overview

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

Defined Under Namespace

Modules: FabricationPatch, FactoryBotPatch, FactoryBuilders, Printers Classes: Configuration, Result

Constant Summary collapse

FACTORY_BUILDERS =
[FactoryBuilders::FactoryBot,
FactoryBuilders::Fabrication].freeze

Constants included from Logging

Logging::COLORS

Class Method Summary collapse

Methods included from Logging

build_log_msg, colorize, log

Class Method Details

.configObject



62
63
64
# File 'lib/test_prof/factory_prof.rb', line 62

def config
  @config ||= Configuration.new
end

.configure {|config| ... } ⇒ Object

Yields:



66
67
68
# File 'lib/test_prof/factory_prof.rb', line 66

def configure
  yield config
end

.initObject

Patch factory lib, init vars



71
72
73
74
75
76
77
# File 'lib/test_prof/factory_prof.rb', line 71

def init
  @running = false

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

  FACTORY_BUILDERS.each(&:patch)
end

.resultObject



100
101
102
# File 'lib/test_prof/factory_prof.rb', line 100

def result
  Result.new(@stacks, @stats)
end

.runObject

Inits FactoryProf and setups at exit hook, then runs



81
82
83
84
85
86
87
88
89
# File 'lib/test_prof/factory_prof.rb', line 81

def run
  init

  printer = config.flamegraph? ? Printers::Flamegraph : Printers::Simple

  at_exit { printer.dump(result) }

  start
end

.startObject



91
92
93
94
# File 'lib/test_prof/factory_prof.rb', line 91

def start
  reset!
  @running = true
end

.stopObject



96
97
98
# File 'lib/test_prof/factory_prof.rb', line 96

def stop
  @running = false
end

.track(factory) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/test_prof/factory_prof.rb', line 104

def track(factory)
  return yield unless running?
  begin
    @depth += 1
    @current_stack << factory if config.flamegraph?
    @stats[factory][:total] += 1
    @stats[factory][:top_level] += 1 if @depth == 1
    yield
  ensure
    @depth -= 1
    flush_stack if @depth.zero?
  end
end