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

#build_log_msg, #colorize, #log

Constructor Details

#initializeRSpecListener

Returns a new instance of RSpecListener.



20
21
22
# File 'lib/test_prof/event_prof/rspec.rb', line 20

def initialize
  @profiler = EventProf.build
end

Instance Method Details

#example_finished(notification) ⇒ Object



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

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

#example_group_finished(notification) ⇒ Object



29
30
31
32
# File 'lib/test_prof/event_prof/rspec.rb', line 29

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

#example_group_started(notification) ⇒ Object



24
25
26
27
# File 'lib/test_prof/event_prof/rspec.rb', line 24

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

#example_started(notification) ⇒ Object



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

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


42
43
44
45
46
47
48
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
# File 'lib/test_prof/event_prof/rspec.rb', line 42

def print
  result = @profiler.results

  msgs = []

  msgs <<
    <<~MSG
      EventProf results for #{@profiler.event}

      Total time: #{@profiler.total_time.duration}
      Total events: #{@profiler.total_count}

      Top #{@profiler.top_count} slowest suites (by #{@profiler.rank_by}):

    MSG

  result[:groups].each do |group|
    description = group[:id].top_level_description
    location = group[:id].[:location]

    msgs <<
      <<~GROUP
        #{description.truncate} (#{location}) – #{group[:time].duration} (#{group[:count]} / #{group[:examples]})
      GROUP
  end

  if result[:examples]
    msgs << "\nTop #{@profiler.top_count} slowest tests (by #{@profiler.rank_by}):\n\n"

    result[:examples].each do |example|
      description = example[:id].description
      location = example[:id].[:location]
      msgs <<
        <<~GROUP
          #{description.truncate} (#{location}) – #{example[:time].duration} (#{example[:count]})
        GROUP
    end
  end

  log :info, msgs.join
end