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 ‘start_middleware.coach’ and ‘finish_middleware.coach’ 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.



11
12
13
14
# File 'lib/coach/request_benchmark.rb', line 11

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

Instance Method Details

#complete(start, finish) ⇒ Object



26
27
28
29
# File 'lib/coach/request_benchmark.rb', line 26

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

#notify(name, start, finish) ⇒ Object



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

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/coach/request_benchmark.rb', line 32

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