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
30
# 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
  @absolute_run_time = 0.0
end

Instance Attribute Details

#absolute_run_timeObject (readonly)

Returns the value of attribute absolute_run_time.



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

def absolute_run_time
  @absolute_run_time
end

#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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/test_prof/event_prof/profiler.rb', line 74

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

  example_run_time = take_time(@example_ts)
  data = {
    id: id,
    time: @example_time,
    run_time: example_run_time,
    count: @example_count
  }

  @examples << data unless data[rank_by].zero?
  @current_example = nil
end

#example_started(id) ⇒ Object



68
69
70
71
72
# File 'lib/test_prof/event_prof/profiler.rb', line 68

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

#group_finished(id) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/test_prof/event_prof/profiler.rb', line 51

def group_finished(id)
  group_run_time = take_time(@group_ts)
  @absolute_run_time += group_run_time

  data = {
    id: id,
    time: @time,
    run_time: group_run_time,
    count: @count,
    examples: @total_examples
  }

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

  @current_group = nil
end

#group_started(id) ⇒ Object



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

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

#resultsObject



90
91
92
93
94
95
96
97
98
99
# File 'lib/test_prof/event_prof/profiler.rb', line 90

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

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

#take_time(start_ts) ⇒ Object



101
102
103
# File 'lib/test_prof/event_prof/profiler.rb', line 101

def take_time(start_ts)
  TestProf.now - start_ts
end

#track(time) ⇒ Object



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

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