Class: TestProf::EventProf::Profiler

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

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event:, instrumenter:, rank_by: :time, top_count: 5, per_example: false) ⇒ Profiler

Returns a new instance of Profiler.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/test_prof/event_prof/profiler.rb', line 11

def initialize(event:, instrumenter:, rank_by: :time, top_count: 5, per_example: false)
  @event = event
  @rank_by = rank_by
  @top_count = top_count
  @per_example = per_example

  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

#countObject (readonly)

Returns the value of attribute count.



6
7
8
# File 'lib/test_prof/event_prof/profiler.rb', line 6

def count
  @count
end

#eventObject (readonly)

Returns the value of attribute event.



6
7
8
# File 'lib/test_prof/event_prof/profiler.rb', line 6

def event
  @event
end

#example_countObject (readonly)

Returns the value of attribute example_count.



6
7
8
# File 'lib/test_prof/event_prof/profiler.rb', line 6

def example_count
  @example_count
end

#example_timeObject (readonly)

Returns the value of attribute example_time.



6
7
8
# File 'lib/test_prof/event_prof/profiler.rb', line 6

def example_time
  @example_time
end

#per_exampleObject (readonly) Also known as: per_example?

Returns the value of attribute per_example.



6
7
8
# File 'lib/test_prof/event_prof/profiler.rb', line 6

def per_example
  @per_example
end

#rank_byObject (readonly)

Returns the value of attribute rank_by.



6
7
8
# File 'lib/test_prof/event_prof/profiler.rb', line 6

def rank_by
  @rank_by
end

#timeObject (readonly)

Returns the value of attribute time.



6
7
8
# File 'lib/test_prof/event_prof/profiler.rb', line 6

def time
  @time
end

#top_countObject (readonly)

Returns the value of attribute top_count.



6
7
8
# File 'lib/test_prof/event_prof/profiler.rb', line 6

def top_count
  @top_count
end

#total_countObject (readonly)

Returns the value of attribute total_count.



6
7
8
# File 'lib/test_prof/event_prof/profiler.rb', line 6

def total_count
  @total_count
end

#total_timeObject (readonly)

Returns the value of attribute total_time.



6
7
8
# File 'lib/test_prof/event_prof/profiler.rb', line 6

def total_time
  @total_time
end

Instance Method Details

#example_finished(id) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/test_prof/event_prof/profiler.rb', line 64

def example_finished(id)
  @total_examples += 1
  return unless 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



58
59
60
61
62
# File 'lib/test_prof/event_prof/profiler.rb', line 58

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

#group_finished(id) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/test_prof/event_prof/profiler.rb', line 50

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



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

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

#resultsObject



73
74
75
76
77
78
79
80
81
# File 'lib/test_prof/event_prof/profiler.rb', line 73

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

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

#track(time) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/test_prof/event_prof/profiler.rb', line 31

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