Class: Sqreen::PerformanceNotifications::LogPerformance
- Defined in:
- lib/sqreen/performance_notifications/log_performance.rb
Overview
Log performances on the console
Class Method Summary collapse
- .disable ⇒ Object
- .enable(facility = nil) ⇒ Object
- .enabled? ⇒ Boolean
- .log(rule, cb, start, finish, _meta) ⇒ Object
- .next_request ⇒ Object
- .timings ⇒ Object
- .timings=(value) ⇒ Object
Methods inherited from Log
Class Method Details
.disable ⇒ Object
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
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, ) event = event_name(rule, cb) timings << [event, start, finish] end |
.next_request ⇒ Object
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 |
.timings ⇒ Object
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 |