File: rspec.rb

package info (click to toggle)
ruby-test-prof 1.5.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 660 kB
  • sloc: ruby: 6,064; makefile: 4
file content (63 lines) | stat: -rw-r--r-- 1,541 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
# frozen_string_literal: true

module TestProf
  module TPSProf
    class RSpecListener # :nodoc:
      include Logging

      NOTIFICATIONS = %i[
        example_group_started
        example_group_finished
        example_started
        example_finished
      ].freeze

      attr_reader :reporter, :profiler

      def initialize
        @profiler = Profiler.new(TPSProf.config.top_count, threshold: TPSProf.config.threshold)
        @reporter = TPSProf.config.reporter

        log :info, "TPSProf enabled (top-#{TPSProf.config.top_count})"
      end

      def example_group_started(notification)
        return unless notification.group.top_level?
        profiler.group_started notification.group
      end

      def example_group_finished(notification)
        return unless notification.group.top_level?
        profiler.group_finished notification.group
      end

      def example_started(notification)
        profiler.example_started notification.example
      end

      def example_finished(notification)
        profiler.example_finished notification.example
      end

      def print
        reporter.print(profiler)
      end
    end
  end
end

# Register TPSProf listener
TestProf.activate("TPS_PROF") do
  RSpec.configure do |config|
    listener = nil

    config.before(:suite) do
      listener = TestProf::TPSProf::RSpecListener.new
      config.reporter.register_listener(
        listener, *TestProf::TPSProf::RSpecListener::NOTIFICATIONS
      )
    end

    config.after(:suite) { listener&.print }
  end
end