Module: TestProf::FactoryProf

Extended by:
Logging
Defined in:
lib/test_prof/factory_prof.rb,
lib/test_prof/factory_prof/printers/json.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/printers/nate_heckler.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

log

Class Method Details

.configObject



81
82
83
# File 'lib/test_prof/factory_prof.rb', line 81

def config
  @config ||= Configuration.new
end

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

Yields:



85
86
87
# File 'lib/test_prof/factory_prof.rb', line 85

def configure
  yield config
end

.initObject

Patch factory lib, init vars



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

def init
  @running = false

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

  patch!
end

.patch!Object



98
99
100
101
102
103
104
# File 'lib/test_prof/factory_prof.rb', line 98

def patch!
  return if @patched

  FACTORY_BUILDERS.each(&:patch)

  @patched = true
end


120
121
122
123
124
# File 'lib/test_prof/factory_prof.rb', line 120

def print(started_at)
  printer = config.printer

  printer.dump(result, start_time: started_at, threshold: config.threshold)
end

.resultObject



135
136
137
# File 'lib/test_prof/factory_prof.rb', line 135

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

.runObject

Inits FactoryProf and setups at exit hook, then runs



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/test_prof/factory_prof.rb', line 108

def run
  init

  started_at = TestProf.now

  at_exit do
    print(started_at)
  end

  start
end

.startObject



126
127
128
129
# File 'lib/test_prof/factory_prof.rb', line 126

def start
  reset!
  @running = true
end

.stopObject



131
132
133
# File 'lib/test_prof/factory_prof.rb', line 131

def stop
  @running = false
end

.track(factory, variation:) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/test_prof/factory_prof.rb', line 139

def track(factory, variation:)
  return yield unless running?
  @depth += 1
  @current_stack << factory if config.flamegraph?
  track_count(@stats[factory])
  track_count(@stats[factory][:variations][variation_name(variation)]) if config.include_variations?
  t1 = TestProf.now
  begin
    yield
  ensure
    t2 = TestProf.now
    track_time(@stats[factory], t1, t2)
    track_time(@stats[factory][:variations][variation_name(variation)], t1, t2) if config.include_variations?
    @depth -= 1
    flush_stack if @depth.zero?
  end
end