Module: TestProf::RSpecDissect

Extended by:
Logging
Defined in:
lib/test_prof/rspec_dissect.rb,
lib/test_prof/rspec_dissect/rspec.rb,
lib/test_prof/rspec_dissect/collector.rb

Overview

RSpecDissect tracks how much time do you spend in before hooks and memoization helpers (i.e. let) in your tests.

Defined Under Namespace

Modules: ExampleInstrumentation, MemoizedInstrumentation Classes: Collector, Configuration, Listener, Span

Constant Summary

Constants included from Logging

Logging::COLORS

Class Method Summary collapse

Methods included from Logging

log

Class Method Details

.configObject



64
65
66
# File 'lib/test_prof/rspec_dissect.rb', line 64

def config
  @config ||= Configuration.new
end

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

Yields:



68
69
70
# File 'lib/test_prof/rspec_dissect.rb', line 68

def configure
  yield config
end

.current_spanObject



88
89
90
# File 'lib/test_prof/rspec_dissect.rb', line 88

def current_span
  Thread.current[:_rspec_dissect_spans_stack].last
end

.initObject



72
73
74
75
76
77
78
79
80
81
# File 'lib/test_prof/rspec_dissect.rb', line 72

def init
  RSpec::Core::Example.prepend(ExampleInstrumentation)

  RSpec::Core::MemoizedHelpers::ThreadsafeMemoized.prepend(MemoizedInstrumentation)
  RSpec::Core::MemoizedHelpers::NonThreadSafeMemoized.prepend(MemoizedInstrumentation)

  reset!

  log :info, "RSpecDissect enabled"
end

.nextidObject



83
84
85
86
# File 'lib/test_prof/rspec_dissect.rb', line 83

def nextid
  @last_id += 1
  @last_id.to_s
end

.populate_from_spans!(data) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/test_prof/rspec_dissect.rb', line 112

def populate_from_spans!(data)
  data[:total_setup] = @spans.select { !_1.parent_id }.sum(&:duration)
  data[:total_before_let] = @spans.select { _1.type == :let && _1.parent_id }.sum(&:duration).to_f
  data[:total_lazy_let] = @spans.select { _1.type == :let && !_1.parent_id }.sum(&:duration).to_f

  data[:top_lets] = @spans.select { _1.type == :let }
    .group_by { _1.meta[:name] }
    .transform_values! do |spans|
      {name: spans.first.meta[:name], duration: spans.sum(&:duration), size: spans.size}
    end
    .values
    .sort_by { -_1[:duration] }
    .take(RSpecDissect.config.let_top_count)
end

.reset!Object



127
128
129
130
131
# File 'lib/test_prof/rspec_dissect.rb', line 127

def reset!
  @last_id = 1
  @spans = []
  Thread.current[:_rspec_dissect_spans_stack] = []
end

.span_stackObject



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

def span_stack
  Thread.current[:_rspec_dissect_spans_stack]
end

.track(type, id: nextid, **meta) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/test_prof/rspec_dissect.rb', line 96

def track(type, id: nextid, **meta)
  span = Span.new(id, current_span&.id, type, 0.0, meta)
  span_stack << span

  begin
    start = TestProf.now
    res = yield
    delta = (TestProf.now - start)
    span.duration = delta
    @spans << span
    res
  ensure
    span_stack.pop
  end
end