Class: TestProf::RSpecDissect::Listener

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/test_prof/rspec_dissect/rspec.rb

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

NOTIFICATIONS =
%i[
  example_group_finished
  example_passed
  example_failed
].freeze

Constants included from Logging

Logging::COLORS

Instance Method Summary collapse

Methods included from Logging

#build_log_msg, #colorize, #log

Constructor Details

#initializeListener

Returns a new instance of Listener.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/test_prof/rspec_dissect/rspec.rb', line 23

def initialize
  @before_results = Utils::SizedOrderedSet.new(
    top_count, sort_by: :before
  )
  @memo_results = Utils::SizedOrderedSet.new(
    top_count, sort_by: :memo
  )
  @examples_count = 0
  @examples_time = 0.0
  @total_examples_time = 0.0
end

Instance Method Details

#example_finished(notification) ⇒ Object Also known as: example_passed, example_failed



35
36
37
38
# File 'lib/test_prof/rspec_dissect/rspec.rb', line 35

def example_finished(notification)
  @examples_count += 1
  @examples_time += notification.example.execution_result.run_time
end

#example_group_finished(notification) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/test_prof/rspec_dissect/rspec.rb', line 43

def example_group_finished(notification)
  return unless notification.group.top_level?

  data = {}
  data[:total] = @examples_time
  data[:count] = @examples_count
  data[:before] = RSpecDissect.before_time
  data[:memo] = RSpecDissect.memo_time
  data[:desc] = notification.group.top_level_description
  data[:loc] = notification.group.[:location]

  @before_results << data
  @memo_results << data

  @total_examples_time += @examples_time
  @examples_count = 0
  @examples_time = 0.0

  RSpecDissect.reset!
end


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/test_prof/rspec_dissect/rspec.rb', line 64

def print
  msgs = []

  msgs <<
    <<-MSG.strip_heredoc
      RSpecDissect report

      Total time: #{@total_examples_time.duration}
      Total `before(:each)` time: #{RSpecDissect.total_before_time.duration}
    MSG

  msgs <<
    if RSpecDissect.memoization_available?
      "Total `let` time: #{RSpecDissect.total_memo_time.duration}\n\n"
    else
      "Total `let` time: NOT SUPPORTED (requires RSpec >= 3.3.0)\n\n"
    end

  msgs <<
    <<-MSG.strip_heredoc
      Top #{top_count} slowest suites (by `before(:each)` time):

    MSG

  @before_results.each do |group|
    msgs <<
      <<-GROUP.strip_heredoc
        #{group[:desc].truncate} (#{group[:loc]}) – #{group[:before].duration} of #{group[:total].duration} (#{group[:count]})
      GROUP
  end

  if RSpecDissect.memoization_available?
    msgs <<
      <<-MSG.strip_heredoc

        Top #{top_count} slowest suites (by `let` time):

      MSG

    @memo_results.each do |group|
      msgs <<
        <<-GROUP.strip_heredoc
          #{group[:desc].truncate} (#{group[:loc]}) – #{group[:memo].duration} of #{group[:total].duration} (#{group[:count]})
        GROUP
    end
  end

  log :info, msgs.join

  stamp! if RSpecDissect.config.stamp?
end

#stamp!Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/test_prof/rspec_dissect/rspec.rb', line 116

def stamp!
  stamper = RSpecStamp::Stamper.new

  examples = Hash.new { |h, k| h[k] = [] }

  (@before_results.to_a + @memo_results.to_a)
    .map { |obj| obj[:loc] }.each do |location|
    file, line = location.split(":")
    examples[file] << line.to_i
  end

  examples.each do |file, lines|
    stamper.stamp_file(file, lines.uniq)
  end

  msgs = []

  msgs <<
    <<-MSG.strip_heredoc
      RSpec Stamp results

      Total patches: #{stamper.total}
      Total files: #{examples.keys.size}

      Failed patches: #{stamper.failed}
      Ignored files: #{stamper.ignored}
    MSG

  log :info, msgs.join
end