Class: Sqreen::PerformanceNotifications::LogPerformance

Inherits:
Log
  • Object
show all
Defined in:
lib/sqreen/performance_notifications/log_performance.rb

Overview

Log performances on the console

Class Method Summary collapse

Methods inherited from Log

event_name

Class Method Details

.disableObject



65
66
67
68
69
# 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) ⇒ Object



59
60
61
62
63
# 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?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/sqreen/performance_notifications/log_performance.rb', line 34

def enabled?
  !@subid.nil?
end

.log(rule, cb, start, finish, _meta) ⇒ Object



29
30
31
32
# 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_requestObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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

.timingsObject



16
17
18
19
20
21
22
23
# 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) ⇒ Object



25
26
27
# File 'lib/sqreen/performance_notifications/log_performance.rb', line 25

def timings=(value)
  SharedStorage.set(:log_performance_timings, value)
end