Class: Metrics::Instruments::Histogram
Instance Method Summary
collapse
Methods inherited from Instrument
#tag, #tags
Constructor Details
#initialize(type = :uniform) ⇒ Histogram
Returns a new instance of Histogram.
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 9
def initialize(type = :uniform)
@count = 0
case type
when :uniform
@sample = Metrics::Statistics::UniformSample.new
when :exponential
@sample = Metrics::Statistics::ExponentialSample.new
end
@min = nil
@max = nil
@sum = 0
@variance_s = 0
@variance_m = -1
end
|
Instance Method Details
#as_json(*_) ⇒ Object
156
157
158
159
160
161
162
163
164
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 156
def as_json(*_)
{
:min => self.min,
:max => self.max,
:mean => self.mean,
:variance => self.variance,
:percentiles => self.quantiles([0.25, 0.50, 0.75, 0.95, 0.97, 0.98, 0.99])
}
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
|
#count ⇒ Object
106
107
108
109
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 106
def count
count = @count
return count
end
|
#max ⇒ Object
112
113
114
115
116
117
118
119
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 112
def max
max = @max
if max != nil
return max
else
return 0.0
end
end
|
#mean ⇒ Object
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 130
def mean
count = @count
sum = @sum
if count > 0
return sum / count
else
return 0.0
end
end
|
#min ⇒ Object
121
122
123
124
125
126
127
128
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 121
def min
min = @min
if min != nil
return min
else
return 0.0
end
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
68
69
70
71
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 43
def quantiles(percentiles)
count = @count
scores = {}
values = @sample.values[0..count-1]
percentiles.each do |pct|
scores[pct] = 0.0
end
if count > 0
values.sort!
percentiles.each do |pct|
idx = pct * (values.length - 1) + 1.0
if idx <= 1
scores[pct] = values[0]
elsif idx >= values.length
scores[pct] = values[values.length-1]
else
lower = values[idx.to_i - 1]
upper = values[idx.to_i]
scores[pct] = lower + (idx - idx.floor) * (upper - lower)
end
end
end
return scores
end
|
#std_dev ⇒ Object
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 141
def std_dev
count = @count
variance = self.variance()
if count > 0
return Math.sqrt(variance)
else
return 0.0
end
end
|
#to_json(*_) ⇒ Object
166
167
168
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 166
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
79
80
81
82
83
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 79
def update_max(value)
if (@max == nil || value > @max)
@max = value
end
end
|
#update_min(value) ⇒ Object
73
74
75
76
77
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 73
def update_min(value)
if (@min == nil || value < @min)
@min = value
end
end
|
#update_variance(value) ⇒ Object
85
86
87
88
89
90
91
92
93
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 85
def update_variance(value)
count = @count
old_m = @variance_m
new_m = @variance_m + ((value - old_m) / count)
new_s = @variance_s + ((value - old_m) * (value - new_m))
@variance_m = new_m
@variance_s = new_s
end
|
#values ⇒ Object
152
153
154
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 152
def values
@sample.values
end
|
#variance ⇒ Object
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 95
def variance
count = @count
variance_s = @variance_s
if count <= 1
return 0.0
else
return variance_s.to_f / (count - 1).to_i
end
end
|