File: active_support.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 (40 lines) | stat: -rw-r--r-- 931 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
# frozen_string_literal: true

module TestProf::EventProf
  module Instrumentations
    # Wrapper over ActiveSupport::Notifications
    module ActiveSupport
      class Subscriber
        attr_reader :block, :started_at

        def initialize(block)
          @block = block
        end

        def start(*)
          @started_at = TestProf.now
        end

        def publish(_name, started_at, finished_at, *)
          block.call(finished_at - started_at)
        end

        def finish(*)
          block.call(TestProf.now - started_at)
        end
      end

      class << self
        def subscribe(event, &block)
          raise ArgumentError, "Block is required!" unless block_given?

          ::ActiveSupport::Notifications.subscribe(event, Subscriber.new(block))
        end

        def instrument(event)
          ::ActiveSupport::Notifications.instrument(event) { yield }
        end
      end
    end
  end
end