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
|