Class: ActionDispatch::ServerTiming
| Relationships & Source Files | |
| Namespace Children | |
|
Classes:
| |
| Inherits: | Object |
| Defined in: | actionpack/lib/action_dispatch/middleware/server_timing.rb |
Class Method Summary
- .new(app) ⇒ ServerTiming constructor
- .unsubscribe Internal use only
Instance Method Summary
- #call(env)
-
#exclusive_duration_sum(events)
private
Sum durations for one event name without double-counting nested events.
Constructor Details
.new(app) ⇒ ServerTiming
# File 'actionpack/lib/action_dispatch/middleware/server_timing.rb', line 52
def initialize(app) @app = app @subscriber = Subscriber.instance @subscriber.ensure_subscribed end
Class Method Details
.unsubscribe
This method is for internal use only.
[ GitHub ]
# File 'actionpack/lib/action_dispatch/middleware/server_timing.rb', line 48
def self.unsubscribe # :nodoc: Subscriber.instance.unsubscribe end
Instance Method Details
#call(env)
[ GitHub ]# File 'actionpack/lib/action_dispatch/middleware/server_timing.rb', line 58
def call(env) response = nil events = @subscriber.collect_events do response = @app.call(env) end headers = response[1] header_info = events.group_by(&:name).map do |event_name, events_collection| "%s;dur=%.2f" % [event_name, exclusive_duration_sum(events_collection)] end if headers[ActionDispatch::Constants::SERVER_TIMING].present? header_info.prepend(headers[ActionDispatch::Constants::SERVER_TIMING]) end headers[ActionDispatch::Constants::SERVER_TIMING] = header_info.join(", ") response end
#exclusive_duration_sum(events) (private)
Sum durations for one event name without double-counting nested events. Nested notifications (e.g. a partial that renders another partial) report inclusive durations; subtracting direct same-name children yields exclusive time so the metric stays within wall-clock bounds.
# File 'actionpack/lib/action_dispatch/middleware/server_timing.rb', line 83
def exclusive_duration_sum(events) return 0.0 if events.empty? return events.first.duration if events.one? sorted = events.sort_by { |event| [event.time, -event.duration] } children_ms = Hash.new(0.0) stack = [] sorted.each do |event| while stack.any? && stack.last.end <= event.time stack.pop end if stack.any? && event.end <= stack.last.end children_ms[stack.last] += event.duration end stack << event end events.sum { |event| event.duration - children_ms[event] } end