File: base.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 (70 lines) | stat: -rw-r--r-- 1,528 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
# frozen_string_literal: true

require "test_prof/utils/sized_ordered_set"
require "test_prof/ext/float_duration"
require "test_prof/ext/string_truncate"

module TestProf # :nodoc: all
  using FloatDuration
  using StringTruncate

  module RSpecDissect
    module Collectors
      class Base
        attr_reader :results, :name, :top_count

        def initialize(name:, top_count:)
          @name = name
          @top_count = top_count
          @results = Utils::SizedOrderedSet.new(
            top_count, sort_by: name
          )
        end

        def populate!(data)
          data[name] = RSpecDissect.time_for(name)
        end

        def <<(data)
          results << data
        end

        def total_time
          RSpecDissect.total_time_for(name)
        end

        def total_time_message
          "\nTotal `#{print_name}` time: #{total_time.duration}"
        end

        def print_name
          name
        end

        def print_result_header
          <<~MSG

            Top #{top_count} slowest suites (by `#{print_name}` time):

          MSG
        end

        def print_group_result(group)
          <<~GROUP
            #{group[:desc].truncate} (#{group[:loc]}) – #{group[name].duration} of #{group[:total].duration} (#{group[:count]})
          GROUP
        end

        def print_results
          msgs = [print_result_header]

          results.each do |group|
            msgs << print_group_result(group)
          end

          msgs.join
        end
      end
    end
  end
end