-
Notifications
You must be signed in to change notification settings - Fork 43
/
histogram.go
87 lines (72 loc) · 1.54 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
package main
import "sort"
// TODO: Replace histogram implementation with a sparse bucket based one so
// it's memory-bounded.
type histogram struct {
all []float64
min float64
max float64
total float64
}
func (h *histogram) Add(point float64) {
h.all = append(h.all, point)
h.total += point
if len(h.all) == 1 || h.min > point {
h.min = point
}
if h.max < point {
h.max = point
}
}
func (h *histogram) Total() float64 {
return h.total
}
func (h *histogram) Min() float64 {
return h.min
}
func (h *histogram) Max() float64 {
return h.max
}
func (h *histogram) Average() float64 {
return h.total / float64(len(h.all))
}
func (h *histogram) Variance() float64 {
// Population variance
mean := h.Average()
sum := 0.0
for _, v := range h.all {
delta := v - mean
sum += delta * delta
}
return sum / float64(h.Len())
}
func (h *histogram) Len() int {
return len(h.all)
}
// Percentiles takes buckets in whole percentages (e.g. 95 is 95%) and returns
// a slice with percentile values in the corresponding index. Buckets must be
// in-order.
func (h *histogram) Percentiles(buckets ...int) []float64 {
r := make([]float64, len(buckets))
if len(h.all) == 0 {
return r
}
sort.Float64s(h.all)
for i, j := 0, 0; i < len(h.all) || j < len(buckets); i++ {
if i >= len(h.all) {
// Fill up the remaining buckets with the highest value.
r[j] = h.all[len(h.all)-1]
j++
continue
}
current := i * 100 / len(h.all)
if current >= buckets[j] {
r[j] = h.all[i]
j++
}
if j >= len(buckets) {
break
}
}
return r
}