Module: TestProf::StackProf

Extended by:
Logging
Defined in:
lib/test_prof/stack_prof.rb

Overview

StackProf wrapper.

Has 2 modes: global and per-example.

Example:

# To activate global profiling you can use env variable
TEST_STACK_PROF=1 rspec ...

# or in your code
TestProf::StackProf.run

To profile a specific examples add :sprof tag to it:

it "is doing heavy stuff", :sprof do
  ...
end

Defined Under Namespace

Classes: Configuration

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



39
40
41
# File 'lib/test_prof/stack_prof.rb', line 39

def config
  @config ||= Configuration.new
end

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

Yields:



43
44
45
# File 'lib/test_prof/stack_prof.rb', line 43

def configure
  yield config
end

.dump(name) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/test_prof/stack_prof.rb', line 81

def dump(name)
  ::StackProf.stop

  path = build_path(name)

  ::StackProf.results(path)

  log :info, "StackProf report generated: #{path}"

  return unless config.raw

  html_path = path.gsub(/\.dump$/, '.html')

  log :info, <<-MSG.strip_heredoc
    Run the following command to generate a flame graph report:

    stackprof --flamegraph #{path} > #{html_path} && stackprof --flamegraph-viewer=#{html_path}
  MSG
end

.profile(name = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/test_prof/stack_prof.rb', line 61

def profile(name = nil)
  return if locked?
  return unless init_stack_prof

  options = {
    mode: config.mode,
    raw: config.raw
  }

  options[:interval] = config.interval if config.interval

  if block_given?
    options[:out] = build_path(name)
    ::StackProf.run(options) { yield }
  else
    ::StackProf.start(options)
  end
  true
end

.runObject

Run StackProf and automatically dump a report when the process exits.

Use this method to profile the whole run.



51
52
53
54
55
56
57
58
59
# File 'lib/test_prof/stack_prof.rb', line 51

def run
  return unless profile

  @locked = true

  log :info, "StackProf enabled"

  at_exit { dump("total") }
end