Class: Rack::Healthz::RequestAccumulator

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/healthz/request_accumulator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(category) ⇒ RequestAccumulator

Returns a new instance of RequestAccumulator.



7
8
9
10
11
12
13
14
# File 'lib/rack/healthz/request_accumulator.rb', line 7

def initialize(category)
  @category = category
  @count = 0
  @sum_of_times = 0.0
  @sum_of_square_times = 0.0
  @min_time = Float::INFINITY
  @max_time = -Float::INFINITY
end

Instance Attribute Details

#categoryObject (readonly)

Returns the value of attribute category.



5
6
7
# File 'lib/rack/healthz/request_accumulator.rb', line 5

def category
  @category
end

#countObject (readonly)

Returns the value of attribute count.



5
6
7
# File 'lib/rack/healthz/request_accumulator.rb', line 5

def count
  @count
end

#max_timeObject (readonly)

Returns the value of attribute max_time.



5
6
7
# File 'lib/rack/healthz/request_accumulator.rb', line 5

def max_time
  @max_time
end

#meanObject (readonly)

Returns the value of attribute mean.



5
6
7
# File 'lib/rack/healthz/request_accumulator.rb', line 5

def mean
  @mean
end

#min_timeObject (readonly)

Returns the value of attribute min_time.



5
6
7
# File 'lib/rack/healthz/request_accumulator.rb', line 5

def min_time
  @min_time
end

#stdObject (readonly)

Returns the value of attribute std.



5
6
7
# File 'lib/rack/healthz/request_accumulator.rb', line 5

def std
  @std
end

Instance Method Details

#<<(elapsed_time) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rack/healthz/request_accumulator.rb', line 16

def <<(elapsed_time)
  @count += 1
  @sum_of_times += elapsed_time
  @sum_of_square_times += elapsed_time ** 2
  @min_time = [@min_time, elapsed_time].min
  @max_time = [@max_time, elapsed_time].max
  @mean = @sum_of_times / @count

  @std = if @count > 1
           Math.sqrt((@sum_of_square_times / @count) - (@sum_of_times / @count) ** 2)
         elsif @count == 1
           0.0
         else
           nil
         end
  self
end

#to_hObject



34
35
36
37
38
39
40
41
42
# File 'lib/rack/healthz/request_accumulator.rb', line 34

def to_h
  {
      'count' => count,
      'min' => min_time,
      'max' => max_time,
      'mean' => mean,
      'std' => std
  }
end