Class: TestProf::EventProf::Profiler

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

Overview

:nodoc:

Constant Summary

Constants included from Logging

Logging::COLORS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#build_log_msg, #colorize, #log

Constructor Details

#initialize(event:, instrumenter:) ⇒ Profiler

Returns a new instance of Profiler.



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

def initialize(event:, instrumenter:)
  @event = event

  log :info, "EventProf enabled (#{@event})"

  instrumenter.subscribe(event) { |time| track(time) }

  @groups = Utils::SizedOrderedSet.new(
    top_count, sort_by: rank_by
  )

  @examples = Utils::SizedOrderedSet.new(
    top_count, sort_by: rank_by
  )

  @total_count = 0
  @total_time = 0.0
end

Instance Attribute Details

#eventObject (readonly)

Returns the value of attribute event.



79
80
81
# File 'lib/test_prof/event_prof.rb', line 79

def event
  @event
end

#rank_byObject (readonly)

Returns the value of attribute rank_by.



79
80
81
# File 'lib/test_prof/event_prof.rb', line 79

def rank_by
  @rank_by
end

#top_countObject (readonly)

Returns the value of attribute top_count.



79
80
81
# File 'lib/test_prof/event_prof.rb', line 79

def top_count
  @top_count
end

#total_countObject (readonly)

Returns the value of attribute total_count.



79
80
81
# File 'lib/test_prof/event_prof.rb', line 79

def total_count
  @total_count
end

#total_timeObject (readonly)

Returns the value of attribute total_time.



79
80
81
# File 'lib/test_prof/event_prof.rb', line 79

def total_time
  @total_time
end

Instance Method Details

#example_finished(id) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/test_prof/event_prof.rb', line 133

def example_finished(id)
  @total_examples += 1
  return unless config.per_example?

  data = { id: id, time: @example_time, count: @example_count }
  @examples << data unless data[rank_by].zero?
  @current_example = nil
end

#example_started(id) ⇒ Object



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

def example_started(id)
  return unless config.per_example?
  reset_example!
  @current_example = id
end

#group_finished(id) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/test_prof/event_prof.rb', line 119

def group_finished(id)
  data = { id: id, time: @time, count: @count, examples: @total_examples }

  @groups << data unless data[rank_by].zero?

  @current_group = nil
end

#group_started(id) ⇒ Object



114
115
116
117
# File 'lib/test_prof/event_prof.rb', line 114

def group_started(id)
  reset_group!
  @current_group = id
end

#resultsObject



142
143
144
145
146
147
148
149
150
# File 'lib/test_prof/event_prof.rb', line 142

def results
  {
    groups: @groups.to_a
  }.tap do |data|
    next unless config.per_example?

    data[:examples] = @examples.to_a
  end
end

#track(time) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/test_prof/event_prof.rb', line 100

def track(time)
  return if @current_group.nil?
  @total_time += time
  @total_count += 1

  @time += time
  @count += 1

  return if @current_example.nil?

  @example_time += time
  @example_count += 1
end