-
Notifications
You must be signed in to change notification settings - Fork 0
/
histogram.go
91 lines (79 loc) · 1.57 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
package stats
import (
"fmt"
"sync"
)
// thanks zserge/metric!
const maxBins = 1000
type bin struct {
value float64
count float64
}
type Histogram struct {
sync.Mutex
bins []bin
total uint64
}
func NewHistogram() *Histogram {
return &Histogram{
Mutex: sync.Mutex{},
}
}
func (h *Histogram) String() string {
return fmt.Sprintf("min:\t%.2f\np50:\t%.2f\np90:\t%.2f\np95:\t%.2f\np99:\t%.2f\nmax:\t%.2f",
h.Quantile(0.0),
h.Quantile(0.5),
h.Quantile(0.90),
h.Quantile(0.95),
h.Quantile(0.99),
h.Quantile(1.0))
}
func (h *Histogram) Reset() {
h.Lock()
defer h.Unlock()
h.bins = nil
h.total = 0
}
func (h *Histogram) Add(n float64) {
h.Lock()
defer h.Unlock()
defer h.trim()
h.total++
newbin := bin{value: n, count: 1}
for i := range h.bins {
if h.bins[i].value > n {
h.bins = append(h.bins[:i], append([]bin{newbin}, h.bins[i:]...)...)
return
}
}
h.bins = append(h.bins, newbin)
}
func (h *Histogram) trim() {
for len(h.bins) > maxBins {
d := float64(0)
i := 0
for j := 1; j < len(h.bins); j++ {
if dv := h.bins[j].value - h.bins[j-1].value; dv < d || j == 1 {
d = dv
i = j
}
}
count := h.bins[i-1].count + h.bins[i].count
merged := bin{
value: (h.bins[i-1].value*h.bins[i-1].count + h.bins[i].value*h.bins[i].count) / count,
count: count,
}
h.bins = append(h.bins[:i-1], h.bins[i:]...)
h.bins[i-1] = merged
}
}
func (h *Histogram) Quantile(q float64) float64 {
count := q * float64(h.total)
for i := range h.bins {
count -= float64(h.bins[i].count)
if count <= 0 {
return h.bins[i].value
}
}
return 0
}