Sha256: ab7ead10e9db650494f1465f8e0b151a888404aa3044c20c002a0931c11fb2bb

Contents?: true

Size: 1.92 KB

Versions: 3

Compression:

Stored size: 1.92 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'

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(event, start, finish, _meta)
          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(nil,
                                                              &method(:log))
        end

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

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
sqreen-1.11.0-java lib/sqreen/performance_notifications/log_performance.rb
sqreen-alt-1.11.0 lib/sqreen/performance_notifications/log_performance.rb
sqreen-1.11.0 lib/sqreen/performance_notifications/log_performance.rb