class Sqreen::PerformanceNotifications::LogPerformance

Log performances on the console

Public Class Methods

disable() click to toggle source
# File lib/sqreen/performance_notifications/log_performance.rb, line 65
def disable
  return if @subid.nil?
  Sqreen::PerformanceNotifications.unsubscribe(@subid)
  @subid = nil
end
enable(facility = nil) click to toggle source
# File lib/sqreen/performance_notifications/log_performance.rb, line 59
def enable(facility = nil)
  return unless @subid.nil?
  @facility = facility
  @subid = Sqreen::PerformanceNotifications.subscribe(&method(:log))
end
enabled?() click to toggle source
# File lib/sqreen/performance_notifications/log_performance.rb, line 34
def enabled?
  !@subid.nil?
end
log(rule, cb, start, finish, _meta) click to toggle source
# File lib/sqreen/performance_notifications/log_performance.rb, line 29
def log(rule, cb, start, finish, _meta)
  event = event_name(rule, cb)
  timings << [event, start, finish]
end
next_request() click to toggle source
# File lib/sqreen/performance_notifications/log_performance.rb, line 38
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
timings() click to toggle source
# File lib/sqreen/performance_notifications/log_performance.rb, line 16
def timings
  v = SharedStorage.get(:log_performance_timings)
  if v.nil?
    v = []
    self.timings = v
  end
  v
end
timings=(value) click to toggle source
# File lib/sqreen/performance_notifications/log_performance.rb, line 25
def timings=(value)
  SharedStorage.set(:log_performance_timings, value)
end