Class: Coach::RequestBenchmark

Inherits:
Object
  • Object
show all
Defined in:
lib/coach/request_benchmark.rb

Overview

This class is built to aggregate data during the course of the request. It relies on ‘coach.middleware.start’ and ‘coach.middleware.end’ events to register the start/end of each middleware element, and thereby calculate running times for each.

Coach::Notifications makes use of this class to produce benchmark data for requests.

Instance Method Summary collapse

Constructor Details

#initialize(endpoint_name) ⇒ RequestBenchmark

Returns a new instance of RequestBenchmark.



9
10
11
12
# File 'lib/coach/request_benchmark.rb', line 9

def initialize(endpoint_name)
  @endpoint_name = endpoint_name
  @events = []
end

Instance Method Details

#complete(start, finish) ⇒ Object



24
25
26
# File 'lib/coach/request_benchmark.rb', line 24

def complete(start, finish)
  @duration = finish - start
end

#notify(name, start, finish) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/coach/request_benchmark.rb', line 14

def notify(name, start, finish)
  event =  { name: name, start: start, finish: finish }

  duration_of_children = child_events_for(event).
    inject(0) { |total, e| total + e[:duration] }
  event[:duration] = (finish - start) - duration_of_children

  @events.push(event)
end

#statsObject

Serialize the results of the benchmarking



29
30
31
32
33
34
35
36
37
# File 'lib/coach/request_benchmark.rb', line 29

def stats
  {
    endpoint_name: @endpoint_name,
    duration: format_ms(@duration),
    chain: sorted_chain.map do |event|
      { name: event[:name], duration: format_ms(event[:duration]) }
    end
  }
end