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.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/test_prof/event_prof.rb', line 78

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

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

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

  @groups = Hash.new { |h, k| h[k] = { id: k } }
  @examples = Hash.new { |h, k| h[k] = { id: k } }

  @total_count = 0
  @total_time = 0.0

  reset!
end

Instance Attribute Details

#eventObject (readonly)

Returns the value of attribute event.



76
77
78
# File 'lib/test_prof/event_prof.rb', line 76

def event
  @event
end

#rank_byObject (readonly)

Returns the value of attribute rank_by.



76
77
78
# File 'lib/test_prof/event_prof.rb', line 76

def rank_by
  @rank_by
end

#top_countObject (readonly)

Returns the value of attribute top_count.



76
77
78
# File 'lib/test_prof/event_prof.rb', line 76

def top_count
  @top_count
end

#total_countObject (readonly)

Returns the value of attribute total_count.



76
77
78
# File 'lib/test_prof/event_prof.rb', line 76

def total_count
  @total_count
end

#total_timeObject (readonly)

Returns the value of attribute total_time.



76
77
78
# File 'lib/test_prof/event_prof.rb', line 76

def total_time
  @total_time
end

Instance Method Details

#example_finished(id) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/test_prof/event_prof.rb', line 122

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

  @examples[id][:time] = @example_time
  @examples[id][:count] = @example_count
end

#example_started(_id) ⇒ Object



118
119
120
# File 'lib/test_prof/event_prof.rb', line 118

def example_started(_id)
  reset_example!
end

#group_finished(id) ⇒ Object



111
112
113
114
115
116
# File 'lib/test_prof/event_prof.rb', line 111

def group_finished(id)
  @groups[id][:time] = @time
  @groups[id][:count] = @count
  @groups[id][:examples] = @total_examples
  @current_group = nil
end

#group_started(id) ⇒ Object



106
107
108
109
# File 'lib/test_prof/event_prof.rb', line 106

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

#resultsObject



130
131
132
133
134
135
136
137
138
# File 'lib/test_prof/event_prof.rb', line 130

def results
  {
    groups: fetch_top(@groups.values)
  }.tap do |data|
    next unless config.per_example?

    data[:examples] = fetch_top(@examples.values)
  end
end

#track(time) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/test_prof/event_prof.rb', line 94

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

  @time += time
  @count += 1

  @example_time += time if config.per_example?
  @example_count += 1 if config.per_example?
end