Class: Metrics::Instruments::Timer

Inherits:
Object
  • Object
show all
Includes:
TimeConversion
Defined in:
lib/ruby-metrics/instruments/timer.rb

Direct Known Subclasses

PostProcessTimer

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TimeConversion

#convert_to_ns

Constructor Details

#initialize(options = {}) ⇒ Timer

Returns a new instance of Timer.



10
11
12
13
14
15
16
17
18
# File 'lib/ruby-metrics/instruments/timer.rb', line 10

def initialize(options = {})
  @meter          = Meter.new
  @histogram      = ExponentialHistogram.new

  @duration_unit  = options[:duration_unit] || :seconds
  @rate_unit      = options[:rate_unit] || :seconds

  clear
end

Instance Attribute Details

#duration_unitObject (readonly)

Returns the value of attribute duration_unit.



8
9
10
# File 'lib/ruby-metrics/instruments/timer.rb', line 8

def duration_unit
  @duration_unit
end

#rate_unitObject (readonly)

Returns the value of attribute rate_unit.



8
9
10
# File 'lib/ruby-metrics/instruments/timer.rb', line 8

def rate_unit
  @rate_unit
end

Instance Method Details

#as_json(*_) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ruby-metrics/instruments/timer.rb', line 101

def as_json(*_)
  percentiles = self.quantiles([0.5, 0.75, 0.95, 0.98, 0.99, 0.999])
  {
    :type => 'timer',
    :rate => {
      :count => count,
      :m1 => one_minute_rate,
      :m5 => five_minute_rate,
      :m15 => fifteen_minute_rate,
      :mean => mean_rate,
      :unit => @rate_unit
    },
    :duration => {
      :min => self.min,
      :max => self.max,
      :mean => self.mean,
      :median => percentiles[0.5],
      :std_dev => self.std_dev,
      :p75 => percentiles[0.75],
      :p95 => percentiles[0.95],
      :p98 => percentiles[0.98],
      :p99 => percentiles[0.99],
      :p999 => percentiles[0.999],
      :unit => @duration_unit
    }
  }
end

#clearObject



20
21
22
# File 'lib/ruby-metrics/instruments/timer.rb', line 20

def clear
  @histogram.clear
end

#countObject



38
39
40
# File 'lib/ruby-metrics/instruments/timer.rb', line 38

def count
  @histogram.count
end

#fifteen_minute_rateObject



42
43
44
# File 'lib/ruby-metrics/instruments/timer.rb', line 42

def fifteen_minute_rate
  @meter.fifteen_minute_rate(@rate_unit)
end

#five_minute_rateObject



46
47
48
# File 'lib/ruby-metrics/instruments/timer.rb', line 46

def five_minute_rate
  @meter.five_minute_rate(@rate_unit)
end

#maxObject



58
59
60
# File 'lib/ruby-metrics/instruments/timer.rb', line 58

def max
  scale_duration_to_ns @histogram.max, @duration_unit
end

#meanObject



66
67
68
# File 'lib/ruby-metrics/instruments/timer.rb', line 66

def mean
  scale_duration_to_ns @histogram.mean, @duration_unit
end

#mean_rateObject



54
55
56
# File 'lib/ruby-metrics/instruments/timer.rb', line 54

def mean_rate
  @meter.mean_rate(@rate_unit)
end

#minObject



62
63
64
# File 'lib/ruby-metrics/instruments/timer.rb', line 62

def min
  scale_duration_to_ns @histogram.min, @duration_unit
end

#one_minute_rateObject



50
51
52
# File 'lib/ruby-metrics/instruments/timer.rb', line 50

def one_minute_rate
  @meter.one_minute_rate(@rate_unit)
end

#quantiles(percentiles = [0.99,0.97,0.95,0.75,0.5,0.25]) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/ruby-metrics/instruments/timer.rb', line 74

def quantiles(percentiles = [0.99,0.97,0.95,0.75,0.5,0.25])
  result = {}

  @histogram.quantiles(percentiles).each do |k,v|
    result[k] = scale_duration_to_ns v, @duration_unit
  end

  result
end

#std_devObject



70
71
72
# File 'lib/ruby-metrics/instruments/timer.rb', line 70

def std_dev
  scale_duration_to_ns @histogram.std_dev, @duration_unit
end

#time(&block) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/ruby-metrics/instruments/timer.rb', line 29

def time(&block)
  start_time = Time.now.to_f
  result = block.call
  time_diff = Time.now.to_f - start_time
  time_in_ns = convert_to_ns time_diff, :seconds
  update_timer(time_in_ns)
  result
end

#to_json(*_) ⇒ Object



129
130
131
# File 'lib/ruby-metrics/instruments/timer.rb', line 129

def to_json(*_)
  as_json.to_json
end

#update(duration, unit) ⇒ Object



24
25
26
27
# File 'lib/ruby-metrics/instruments/timer.rb', line 24

def update(duration, unit)
  mult = convert_to_ns(1, unit)
  self.update_timer(duration * mult)
end

#update_timer(duration) ⇒ Object



94
95
96
97
98
99
# File 'lib/ruby-metrics/instruments/timer.rb', line 94

def update_timer(duration)
  if duration >= 0
    @histogram.update(duration)
    @meter.mark
  end
end

#valuesObject



84
85
86
87
88
89
90
91
92
# File 'lib/ruby-metrics/instruments/timer.rb', line 84

def values
  result = []

  @histogram.values.each do |value|
    result << (scale_duration_to_ns value, @duration_unit)
  end

  result
end