Class: TestProf::EventProf::RSpecListener

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

Overview

:nodoc:

Constant Summary collapse

NOTIFICATIONS =
i[
  example_group_started
  example_group_finished
  example_started
  example_finished
].freeze

Constants included from Logging

Logging::COLORS

Instance Method Summary collapse

Methods included from Logging

#log

Constructor Details

#initializeRSpecListener

Returns a new instance of RSpecListener.



21
22
23
24
25
# File 'lib/test_prof/event_prof/rspec.rb', line 21

def initialize
  @profiler = EventProf.build

  log :info, "EventProf enabled (#{@profiler.events.join(", ")})"
end

Instance Method Details

#example_finished(notification) ⇒ Object



41
42
43
# File 'lib/test_prof/event_prof/rspec.rb', line 41

def example_finished(notification)
  @profiler.example_finished notification.example
end

#example_group_finished(notification) ⇒ Object



32
33
34
35
# File 'lib/test_prof/event_prof/rspec.rb', line 32

def example_group_finished(notification)
  return unless notification.group.top_level?
  @profiler.group_finished notification.group
end

#example_group_started(notification) ⇒ Object



27
28
29
30
# File 'lib/test_prof/event_prof/rspec.rb', line 27

def example_group_started(notification)
  return unless notification.group.top_level?
  @profiler.group_started notification.group
end

#example_started(notification) ⇒ Object



37
38
39
# File 'lib/test_prof/event_prof/rspec.rb', line 37

def example_started(notification)
  @profiler.example_started notification.example
end


45
46
47
# File 'lib/test_prof/event_prof/rspec.rb', line 45

def print
  @profiler.each(&method(:report))
end

#report(profiler) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
# File 'lib/test_prof/event_prof/rspec.rb', line 49

def report(profiler)
  result = profiler.results
  time_percentage = time_percentage(profiler.total_time, profiler.absolute_run_time)

  msgs = []

  msgs <<
    "      EventProf results for \#{profiler.event}\n\n      Total time: \#{profiler.total_time.duration} of \#{profiler.absolute_run_time.duration} (\#{time_percentage}%)\n      Total events: \#{profiler.total_count}\n\n      Top \#{profiler.top_count} slowest suites (by \#{profiler.rank_by}):\n\n    MSG\n\n  result[:groups].each do |group|\n    description = group[:id].top_level_description\n    location = group[:id].metadata[:location]\n    time = group[:time]\n    run_time = group[:run_time]\n    time_percentage = time_percentage(time, run_time)\n\n    msgs <<\n      <<~GROUP\n        \#{description.truncate} (\#{location}) \u2013 \#{time.duration} (\#{group[:count]} / \#{group[:examples]}) of \#{run_time.duration} (\#{time_percentage}%)\n      GROUP\n  end\n\n  if result[:examples]\n    msgs << \"\\nTop \#{profiler.top_count} slowest tests (by \#{profiler.rank_by}):\\n\\n\"\n\n    result[:examples].each do |example|\n      description = example[:id].description\n      location = example[:id].metadata[:location]\n      time = example[:time]\n      run_time = example[:run_time]\n      time_percentage = time_percentage(time, run_time)\n\n      msgs <<\n        <<~GROUP\n          \#{description.truncate} (\#{location}) \u2013 \#{time.duration} (\#{example[:count]}) of \#{run_time.duration} (\#{time_percentage}%)\n        GROUP\n    end\n  end\n\n  log :info, msgs.join\n\n  stamp!(profiler) if EventProf.config.stamp?\nend\n"

#stamp!(profiler) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/test_prof/event_prof/rspec.rb', line 101

def stamp!(profiler)
  result = profiler.results

  stamper = RSpecStamp::Stamper.new

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

  (result[:groups].to_a + result.fetch(:examples, []).to_a)
    .map { |obj| obj[:id].[:location] }.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 <<
    "      RSpec Stamp results\n\n      Total patches: \#{stamper.total}\n      Total files: \#{examples.keys.size}\n\n      Failed patches: \#{stamper.failed}\n      Ignored files: \#{stamper.ignored}\n    MSG\n\n  log :info, msgs.join\nend\n"

#time_percentage(time, total_time) ⇒ Object



134
135
136
# File 'lib/test_prof/event_prof/rspec.rb', line 134

def time_percentage(time, total_time)
  (time / total_time * 100).round(2)
end