Sha256: 405b6910c91316ea79af4003f3edebe1d1f34370a816ca07cb2497246d53533b

Contents?: true

Size: 1.96 KB

Versions: 29

Compression:

Stored size: 1.96 KB

Contents

# typed: false

# Copyright (c) 2015 Sqreen. All Rights Reserved.
# Please refer to our terms for more information: https://www.sqreen.com/terms.html

require 'sqreen/performance_notifications'
require 'sqreen/performance_notifications/log'

module Sqreen
  module PerformanceNotifications
    # Log performances on the console
    class LogPerformance < Log
      @subid = nil
      @facility = nil
      class << self
        def timings
          v = SharedStorage.get(:log_performance_timings)
          if v.nil?
            v = []
            self.timings = v
          end
          v
        end

        def timings=(value)
          SharedStorage.set(:log_performance_timings, value)
        end

        def log(rule, cb, start, finish, _meta)
          event = event_name(rule, cb)
          timings << [event, start, finish]
        end

        def enabled?
          !@subid.nil?
        end

        def next_request
          return unless enabled?
          (@facility || Sqreen.log).warn do
            output = timings.map do |evt, start, finish|
              [evt.split('/')[1], (finish - start) * 1000]
            end
            self.timings = []
            total = output.map(&:last).inject(0, &:+)
            rules = output.inject({}) do |acc, (e, t)|
              tt, cc = (acc[e] || [0, 0])
              acc[e] = [tt + t, cc + 1]
              acc
            end
            format(
              "Sqreen request overhead:\n" +
              ("%s: %.2fms (%d calls)\n" * rules.size) +
              'Total: %.2fms', *rules.to_a.sort_by { |e| e[1] }.flatten, total
            )
          end
        end

        def enable(facility = nil)
          return unless @subid.nil?
          @facility = facility
          @subid = Sqreen::PerformanceNotifications.subscribe(&method(:log))
        end

        def disable
          return if @subid.nil?
          Sqreen::PerformanceNotifications.unsubscribe(@subid)
          @subid = nil
        end
      end
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
sqreen-1.22.0-java lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.22.0 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.21.1-java lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.21.1 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.21.0-java lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.21.0 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.20.4-java lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.20.4 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.21.0.beta3-java lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.21.0.beta3 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.20.4.beta1 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.19.4 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.20.3 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.20.2 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.21.0.beta2 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.21.0.beta1 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.20.1-java lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.20.1 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.20.0-java lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.20.0 lib/sqreen/performance_notifications/log_performance.rb