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



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

def config
  @config ||= Configuration.new
end

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

Yields:



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

def configure
  yield config
end

.initObject

Patch factory lib, init vars



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

def init
  @running = false

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

  FACTORY_BUILDERS.each(&:patch)
end

.resultObject



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

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

.runObject

Inits FactoryProf and setups at exit hook, then runs



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

def run
  init

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

  at_exit { printer.dump(result) }

  start
end

.startObject



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

def start
  reset!
  @running = true
end

.stopObject



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

def stop
  @running = false
end

.track(factory) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/test_prof/factory_prof.rb', line 105

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