-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathhistogram.go
71 lines (60 loc) · 1.19 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
package nettrace
import (
"time"
)
type histogram struct {
count [nBuckets]uint64
totalQ uint64
totalT time.Duration
min time.Duration
max time.Duration
}
func (h *histogram) Add(bucket int, latency time.Duration) {
if h.totalQ == 0 || h.min > latency {
h.min = latency
}
if h.max < latency {
h.max = latency
}
h.count[bucket]++
h.totalQ++
h.totalT += latency
}
type histSnapshot struct {
Counts map[time.Duration]line
Count uint64
Avg, Min, Max time.Duration
}
type line struct {
Start time.Duration
BucketIdx int
Count uint64
Percent float32
CumPct float32
}
func (h *histogram) Snapshot() *histSnapshot {
s := &histSnapshot{
Counts: map[time.Duration]line{},
Count: h.totalQ,
Min: h.min,
Max: h.max,
}
if h.totalQ > 0 {
s.Avg = time.Duration(uint64(h.totalT) / h.totalQ)
}
var cumCount uint64
for i := 0; i < nBuckets; i++ {
cumCount += h.count[i]
l := line{
Start: buckets[i],
BucketIdx: i,
Count: h.count[i],
}
if h.totalQ > 0 {
l.Percent = float32(h.count[i]) / float32(h.totalQ) * 100
l.CumPct = float32(cumCount) / float32(h.totalQ) * 100
}
s.Counts[buckets[i]] = l
}
return s
}