Sha256: 0c0a1d34a895c282e68232295de0f2118d34fbf0a9c3a71a9c30000d80373d05

Contents?: true

Size: 1.03 KB

Versions: 11

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

require 'benchmark'

module PgEventstore
  # This class measures the performance of Subscription's handler and returns the average time required to process an
  # event.
  # @!visibility private
  class SubscriptionHandlerPerformance
    extend Forwardable

    # @return [Integer] the number of measurements to keep
    TIMINGS_TO_KEEP = 100

    def_delegators :@lock, :synchronize

    def initialize
      @lock = Thread::Mutex.new
      @timings = []
    end

    # Yields the given block to measure its execution time
    # @return [Object] the result of yielded block
    def track_exec_time
      result = nil
      time = Benchmark.realtime { result = yield }
      synchronize do
        @timings.shift if @timings.size == TIMINGS_TO_KEEP
        @timings.push(time)
      end
      result
    end

    # The average time required to process an event.
    # @return [Float]
    def average_event_processing_time
      synchronize { @timings.size.zero? ? 0.0 : @timings.sum / @timings.size }
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
pg_eventstore-1.6.0 lib/pg_eventstore/subscriptions/subscription_handler_performance.rb
pg_eventstore-1.5.0 lib/pg_eventstore/subscriptions/subscription_handler_performance.rb
pg_eventstore-1.4.0 lib/pg_eventstore/subscriptions/subscription_handler_performance.rb
pg_eventstore-1.3.4 lib/pg_eventstore/subscriptions/subscription_handler_performance.rb
pg_eventstore-1.3.3 lib/pg_eventstore/subscriptions/subscription_handler_performance.rb
pg_eventstore-1.3.2 lib/pg_eventstore/subscriptions/subscription_handler_performance.rb
pg_eventstore-1.3.1 lib/pg_eventstore/subscriptions/subscription_handler_performance.rb
pg_eventstore-1.3.0 lib/pg_eventstore/subscriptions/subscription_handler_performance.rb
pg_eventstore-1.2.0 lib/pg_eventstore/subscriptions/subscription_handler_performance.rb
pg_eventstore-1.1.5 lib/pg_eventstore/subscriptions/subscription_handler_performance.rb
pg_eventstore-1.1.4 lib/pg_eventstore/subscriptions/subscription_handler_performance.rb