Sha256: 57316382bb4692c2a53c13a4d49d48a99fd32404f36294d7fd5572be50088875

Contents?: true

Size: 1.96 KB

Versions: 10

Compression:

Stored size: 1.96 KB

Contents

# typed: ignore

# 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

10 entries across 10 versions & 1 rubygems

Version Path
sqreen-1.25.1 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.25.0 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.24.3 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.24.2 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.24.1 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.24.0 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.23.2 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.23.1 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.23.0 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.22.1 lib/sqreen/performance_notifications/log_performance.rb