forked from rcrowley/go-metrics
/
histogram.go
209 lines (182 loc) · 4.79 KB
/
histogram.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package metrics
import (
"math"
"sort"
"sync"
"sync/atomic"
)
// Histograms calculate distribution statistics from an int64 value.
//
// This is an interface so as to encourage other structs to implement
// the Histogram API as appropriate.
type Histogram interface {
Clear()
Count() int64
Max() int64
Mean() float64
Min() int64
Percentile(float64) float64
Percentiles([]float64) []float64
StdDev() float64
Update(int64)
Variance() float64
}
// Create a new Histogram with the given Sample. The initial values compare
// so that the first value will be both min and max and the variance is flagged
// for special treatment on its first iteration.
func NewHistogram(s Sample) Histogram {
if UseNilMetrics {
return NilHistogram{}
}
return &StandardHistogram{
max: math.MinInt64,
min: math.MaxInt64,
s: s,
variance: [2]float64{-1.0, 0.0},
}
}
// No-op Histogram.
type NilHistogram struct{}
// No-op.
func (h NilHistogram) Clear() {}
// No-op.
func (h NilHistogram) Count() int64 { return 0 }
// No-op.
func (h NilHistogram) Max() int64 { return 0 }
// No-op.
func (h NilHistogram) Mean() float64 { return 0.0 }
// No-op.
func (h NilHistogram) Min() int64 { return 0 }
// No-op.
func (h NilHistogram) Percentile(p float64) float64 { return 0.0 }
// No-op.
func (h NilHistogram) Percentiles(ps []float64) []float64 {
return make([]float64, len(ps))
}
// No-op.
func (h NilHistogram) StdDev() float64 { return 0.0 }
// No-op.
func (h NilHistogram) Update(v int64) {}
// No-op.
func (h NilHistogram) Variance() float64 { return 0.0 }
// The standard implementation of a Histogram uses a Sample and a goroutine
// to synchronize its calculations.
type StandardHistogram struct {
count, sum, min, max int64
mutex sync.Mutex
s Sample
variance [2]float64
}
// Clear the histogram.
func (h *StandardHistogram) Clear() {
h.mutex.Lock()
defer h.mutex.Unlock()
h.count = 0
h.max = math.MinInt64
h.min = math.MaxInt64
h.s.Clear()
h.sum = 0
h.variance = [...]float64{-1.0, 0.0}
}
// Return the count of inputs since the histogram was last cleared.
func (h *StandardHistogram) Count() int64 {
return atomic.LoadInt64(&h.count)
}
// Return the maximal value seen since the histogram was last cleared.
func (h *StandardHistogram) Max() int64 {
h.mutex.Lock()
defer h.mutex.Unlock()
if 0 == h.count {
return 0
}
return h.max
}
// Return the mean of all values seen since the histogram was last cleared.
func (h *StandardHistogram) Mean() float64 {
h.mutex.Lock()
defer h.mutex.Unlock()
if 0 == h.count {
return 0
}
return float64(h.sum) / float64(h.count)
}
// Return the minimal value seen since the histogram was last cleared.
func (h *StandardHistogram) Min() int64 {
h.mutex.Lock()
defer h.mutex.Unlock()
if 0 == h.count {
return 0
}
return h.min
}
// Return an arbitrary percentile of all values seen since the histogram was
// last cleared.
func (h *StandardHistogram) Percentile(p float64) float64 {
return h.Percentiles([]float64{p})[0]
}
// Return a slice of arbitrary percentiles of all values seen since the
// histogram was last cleared.
func (h *StandardHistogram) Percentiles(ps []float64) []float64 {
scores := make([]float64, len(ps))
values := int64Slice(h.s.Values())
size := len(values)
if size > 0 {
sort.Sort(values)
for i, p := range ps {
pos := p * float64(size+1)
if pos < 1.0 {
scores[i] = float64(values[0])
} else if pos >= float64(size) {
scores[i] = float64(values[size-1])
} else {
lower := float64(values[int(pos)-1])
upper := float64(values[int(pos)])
scores[i] = lower + (pos-math.Floor(pos))*(upper-lower)
}
}
}
return scores
}
// Return the standard deviation of all values seen since the histogram was
// last cleared.
func (h *StandardHistogram) StdDev() float64 {
return math.Sqrt(h.Variance())
}
// Update the histogram with a new value.
func (h *StandardHistogram) Update(v int64) {
h.mutex.Lock()
defer h.mutex.Unlock()
h.s.Update(v)
h.count++
if v < h.min {
h.min = v
}
if v > h.max {
h.max = v
}
h.sum += v
fv := float64(v)
if -1.0 == h.variance[0] {
h.variance[0] = fv
h.variance[1] = 0.0
} else {
m := h.variance[0]
s := h.variance[1]
h.variance[0] = m + (fv-m)/float64(h.count)
h.variance[1] = s + (fv-m)*(fv-h.variance[0])
}
}
// Return the variance of all values seen since the histogram was last cleared.
func (h *StandardHistogram) Variance() float64 {
h.mutex.Lock()
defer h.mutex.Unlock()
if 1 >= h.count {
return 0.0
}
return h.variance[1] / float64(h.count-1)
}
// Cribbed from the standard library's `sort` package.
type int64Slice []int64
func (p int64Slice) Len() int { return len(p) }
func (p int64Slice) Less(i, j int) bool { return p[i] < p[j] }
func (p int64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }