Class: Metrics::Instruments::Histogram
- Inherits:
-
Object
- Object
- Metrics::Instruments::Histogram
show all
- Defined in:
- lib/ruby-metrics/instruments/histogram.rb
Instance Method Summary
collapse
Constructor Details
#initialize(type = :uniform) ⇒ Histogram
Returns a new instance of Histogram.
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 8
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
155
156
157
158
159
160
161
162
163
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 155
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
32
33
34
35
36
37
38
39
40
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 32
def clear
@sample.clear
@min = nil
@max = nil
@sum = 0
@count = 0
@variance_m = -1
@variance_s = 0
end
|
#count ⇒ Object
105
106
107
108
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 105
def count
count = @count
return count
end
|
#max ⇒ Object
111
112
113
114
115
116
117
118
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 111
def max
max = @max
if max != nil
return max
else
return 0.0
end
end
|
#mean ⇒ Object
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 129
def mean
count = @count
sum = @sum
if count > 0
return sum / count
else
return 0.0
end
end
|
#min ⇒ Object
120
121
122
123
124
125
126
127
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 120
def min
min = @min
if min != nil
return min
else
return 0.0
end
end
|
#quantiles(percentiles) ⇒ Object
42
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
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 42
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
140
141
142
143
144
145
146
147
148
149
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 140
def std_dev
count = @count
variance = self.variance()
if count > 0
return Math.sqrt(variance)
else
return 0.0
end
end
|
#to_json(*_) ⇒ Object
165
166
167
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 165
def to_json(*_)
as_json.to_json
end
|
#update(value) ⇒ Object
23
24
25
26
27
28
29
30
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 23
def update(value)
@count += 1
@sum += value
@sample.update(value)
update_max(value)
update_min(value)
update_variance(value);
end
|
#update_max(value) ⇒ Object
78
79
80
81
82
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 78
def update_max(value)
if (@max == nil || value > @max)
@max = value
end
end
|
#update_min(value) ⇒ Object
72
73
74
75
76
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 72
def update_min(value)
if (@min == nil || value < @min)
@min = value
end
end
|
#update_variance(value) ⇒ Object
84
85
86
87
88
89
90
91
92
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 84
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
151
152
153
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 151
def values
@sample.values
end
|
#variance ⇒ Object
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/ruby-metrics/instruments/histogram.rb', line 94
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
|