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



76
77
78
# File 'lib/test_prof/factory_prof.rb', line 76

def config
  @config ||= Configuration.new
end

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

Yields:



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

def configure
  yield config
end

.initObject

Patch factory lib, init vars



85
86
87
88
89
90
91
# File 'lib/test_prof/factory_prof.rb', line 85

def init
  @running = false

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

  patch!
end

.patch!Object



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

def patch!
  return if @patched

  FACTORY_BUILDERS.each(&:patch)

  @patched = true
end


115
116
117
118
119
# File 'lib/test_prof/factory_prof.rb', line 115

def print(started_at)
  printer = config.printer

  printer.dump(result, start_time: started_at)
end

.resultObject



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

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

.runObject

Inits FactoryProf and setups at exit hook, then runs



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

def run
  init

  started_at = TestProf.now

  at_exit do
    print(started_at)
  end

  start
end

.startObject



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

def start
  reset!
  @running = true
end

.stopObject



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

def stop
  @running = false
end

.track(factory) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/test_prof/factory_prof.rb', line 134

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