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/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

.config ⇒ Object



73
74
75
# File 'lib/test_prof/factory_prof.rb', line 73

def config
  @config ||= Configuration.new
end

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

Yields:



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

def configure
  yield config
end

.init ⇒ Object

Patch factory lib, init vars



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

def init
  @running = false

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

  patch!
end

.patch! ⇒ Object



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

def patch!
  return if @patched

  FACTORY_BUILDERS.each(&:patch)

  @patched = true
end


112
113
114
115
116
# File 'lib/test_prof/factory_prof.rb', line 112

def print(started_at)
  printer = config.printer

  printer.dump(result, start_time: started_at)
end

.result ⇒ Object



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

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

.run ⇒ Object

Inits FactoryProf and setups at exit hook, then runs



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

def run
  init

  started_at = TestProf.now

  at_exit do
    print(started_at)
  end

  start
end

.start ⇒ Object



118
119
120
121
# File 'lib/test_prof/factory_prof.rb', line 118

def start
  reset!
  @running = true
end

.stop ⇒ Object



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

def stop
  @running = false
end

.track(factory) ⇒ Object



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

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