Sha256: c935466c55ef690b3c9371ebce9fb0858ade5cee9b42d9745c357c5e476bddc1

Contents?: true

Size: 1.95 KB

Versions: 12

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.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

12 entries across 12 versions & 1 rubygems

Version Path
sqreen-1.18.6-java lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.18.6 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.18.5-java lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.18.5 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.18.4-java lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.18.4 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.18.3-java lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.18.3 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.18.3.beta2 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.18.3.beta1 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.18.2-java lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.18.2 lib/sqreen/performance_notifications/log_performance.rb