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



83
84
85
# File 'lib/test_prof/factory_prof.rb', line 83

def config
  @config ||= Configuration.new
end

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

Yields:



87
88
89
# File 'lib/test_prof/factory_prof.rb', line 87

def configure
  yield config
end

.initObject

Patch factory lib, init vars



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

def init
  @running = false

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

  patch!
end

.patch!Object



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

def patch!
  return if @patched

  FACTORY_BUILDERS.each(&:patch)

  @patched = true
end


122
123
124
125
126
# File 'lib/test_prof/factory_prof.rb', line 122

def print(started_at)
  printer = config.printer

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

.resultObject



137
138
139
# File 'lib/test_prof/factory_prof.rb', line 137

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

.runObject

Inits FactoryProf and setups at exit hook, then runs



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

def run
  init

  started_at = TestProf.now

  at_exit do
    print(started_at)
  end

  start
end

.startObject



128
129
130
131
# File 'lib/test_prof/factory_prof.rb', line 128

def start
  reset!
  @running = true
end

.stopObject



133
134
135
# File 'lib/test_prof/factory_prof.rb', line 133

def stop
  @running = false
end

.track(factory, variation:) ⇒ Object



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

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