Class: Metrics::Instruments::Histogram
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods inherited from Instrument
#tag, #tags
Constructor Details
#initialize(type = :uniform) ⇒ Histogram
Returns a new instance of Histogram.
Instance Attribute Details
#count ⇒ Object
Returns the value of attribute count.
8
9
10
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 8
def count
@count
end
|
Instance Method Details
#as_json(*_) ⇒ Object
125
126
127
128
129
130
131
132
133
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 125
def as_json(*_)
{
:min => min,
:max => max,
:mean => mean,
:variance => variance,
:percentiles => quantiles(Timer::DEFAULT_PERCENTILES)
}
end
|
#clear ⇒ Object
33
34
35
36
37
38
39
40
41
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 33
def clear
@sample.clear
@min = nil
@max = nil
@sum = 0
@count = 0
@variance_m = -1
@variance_s = 0
end
|
#max ⇒ Object
97
98
99
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 97
def max
@max || 0.0
end
|
#mean ⇒ Object
105
106
107
108
109
110
111
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 105
def mean
if @count > 0
@sum / @count
else
0.0
end
end
|
#min ⇒ Object
101
102
103
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 101
def min
@min || 0.0
end
|
#quantiles(percentiles) ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 43
def quantiles(percentiles)
sorted_values = @sample.values[0...@count].sort
scores = { }
percentiles.each do |pct|
scores[pct] =
if @count == 0
0.0
else
index = pct * (sorted_values.length - 1) + 1.0
if index <= 1
sorted_values.first
elsif index >= sorted_values.length
sorted_values.last
else
lower = sorted_values[index.to_i - 1]
upper = sorted_values[index.to_i]
lower + (index - index.floor) * (upper - lower)
end
end
end
scores
end
|
#std_dev ⇒ Object
113
114
115
116
117
118
119
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 113
def std_dev
if @count > 0
Math.sqrt(variance)
else
0.0
end
end
|
#to_json(*_) ⇒ Object
135
136
137
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 135
def to_json(*_)
as_json.to_json
end
|
#update(value) ⇒ Object
24
25
26
27
28
29
30
31
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 24
def update(value)
@count += 1
@sum += value
@sample.update(value)
update_max(value)
update_min(value)
update_variance(value)
end
|
#update_max(value) ⇒ Object
75
76
77
78
79
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 75
def update_max(value)
if @max.nil? || value > @max
@max = value
end
end
|
#update_min(value) ⇒ Object
69
70
71
72
73
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 69
def update_min(value)
if @min.nil? || value < @min
@min = value
end
end
|
#update_variance(value) ⇒ Object
81
82
83
84
85
86
87
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 81
def update_variance(value)
new_m = @variance_m + ((value - @variance_m) / @count)
new_s = @variance_s + ((value - @variance_m) * (value - new_m))
@variance_m = new_m
@variance_s = new_s
end
|
#values ⇒ Object
121
122
123
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 121
def values
@sample.values
end
|
#variance ⇒ Object
89
90
91
92
93
94
95
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 89
def variance
if @count <= 1
0.0
else
@variance_s.to_f / (@count - 1)
end
end
|