Sha256: 86861edaa96fadc4ee3adf421e6f766b48a0717cd9f6d5227108eb884d47f46c

Contents?: true

Size: 1.95 KB

Versions: 38

Compression:

Stored size: 1.95 KB

Contents

# Copyright (c) 2015 Sqreen. All Rights Reserved.
# Please refer to our terms for more information: https://www.sqreen.io/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

38 entries across 38 versions & 1 rubygems

Version Path
sqreen-1.18.1-java lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.18.1 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.18.0-java lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.18.0 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.17.2-java lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.17.2 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.17.2.rc1-java lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.17.2.rc1 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.17.2.beta4-java lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.17.2.beta4 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.17.2.beta3-java lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.17.2.beta3 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.17.2.beta2-java lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.17.2.beta2 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.17.2.beta1-java lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.17.2.beta1 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.17.0-java lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.17.0 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.16.2-java lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.16.2 lib/sqreen/performance_notifications/log_performance.rb