File: profiler.rb

package info (click to toggle)
ruby-test-prof 0.12.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 508 kB
  • sloc: ruby: 4,075; makefile: 4
file content (153 lines) | stat: -rw-r--r-- 3,431 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# frozen_string_literal: true

module TestProf
  module EventProf
    class Profiler # :nodoc:
      attr_reader :event, :total_count, :total_time, :rank_by, :top_count, :per_example,
        :time, :count, :example_time, :example_count, :absolute_run_time

      alias per_example? per_example

      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

      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

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

      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

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

      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

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

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

      def take_time(start_ts)
        TestProf.now - start_ts
      end

      private

      def reset_group!
        @time = 0.0
        @count = 0
        @total_examples = 0
        @group_ts = TestProf.now
      end

      def reset_example!
        @example_count = 0
        @example_time = 0.0
        @example_ts = TestProf.now
      end
    end

    # Multiple profilers wrapper
    class ProfilersGroup
      attr_reader :profilers

      def initialize(event:, **options)
        events = event.split(",")
        @profilers = events.map do |ev|
          Profiler.new(event: ev, **options)
        end
      end

      def each(&block)
        if block_given?
          @profilers.each(&block)
        else
          @profilers.each
        end
      end

      def events
        @profilers.map(&:event)
      end

      %i[group_started group_finished example_started example_finished].each do |name|
        class_eval <<-CODE, __FILE__, __LINE__ + 1
          def #{name}(id)
            @profilers.each { |profiler| profiler.#{name}(id) }
          end
        CODE
      end
    end
  end
end