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/factory_girl_patch.rb,
lib/test_prof/factory_prof/printers/flamegraph.rb

Overview

FactoryProf collects “factory stacks” that can be used to build flamegraphs or detect most popular factories

Defined Under Namespace

Modules: FactoryGirlPatch, Printers Classes: Configuration, Result

Constant Summary

Constants included from Logging

Logging::COLORS

Class Method Summary collapse

Methods included from Logging

build_log_msg, colorize, log

Class Method Details

.configObject



58
59
60
# File 'lib/test_prof/factory_prof.rb', line 58

def config
  @config ||= Configuration.new
end

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

Yields:



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

def configure
  yield config
end

.initObject

Patch factory lib, init vars



67
68
69
70
71
72
73
74
75
# File 'lib/test_prof/factory_prof.rb', line 67

def init
  @running = false

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

  # Monkey-patch FactoryGirl
  ::FactoryGirl::FactoryRunner.prepend(FactoryGirlPatch) if
    defined?(::FactoryGirl)
end

.resultObject



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

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

.runObject

Inits FactoryProf and setups at exit hook, then runs



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

def run
  init

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

  at_exit { printer.dump(result) }

  start
end

.startObject



89
90
91
92
# File 'lib/test_prof/factory_prof.rb', line 89

def start
  reset!
  @running = true
end

.stopObject



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

def stop
  @running = false
end

.track(strategy, factory) ⇒ Object



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

def track(strategy, factory)
  return yield if !running? || (strategy != :create)
  begin
    @depth += 1
    @current_stack << factory if config.flamegraph?
    @stats[factory][:total] += 1
    @stats[factory][:top_level] += 1 if @depth == 1
    yield
  ensure
    @depth -= 1
    flush_stack if @depth.zero?
  end
end