-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathpercentiles.go
More file actions
46 lines (40 loc) · 770 Bytes
/
percentiles.go
File metadata and controls
46 lines (40 loc) · 770 Bytes
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
package gohalt
import (
"math"
"sort"
"sync"
)
type percentiles struct {
buf []uint64
cap uint8
lock sync.Mutex
}
func (p *percentiles) Len() int {
p.lock.Lock()
defer p.lock.Unlock()
return len(p.buf)
}
func (p *percentiles) Push(dim uint64) {
p.lock.Lock()
defer p.lock.Unlock()
if len(p.buf) >= int(p.cap) {
p.buf = p.buf[1:]
}
p.buf = append(p.buf, dim)
}
func (p *percentiles) At(pval float64) uint64 {
p.lock.Lock()
defer p.lock.Unlock()
buf := make([]uint64, len(p.buf))
_ = copy(buf, p.buf)
sort.Slice(buf, func(i, j int) bool {
return buf[i] < buf[j]
})
at := int(math.Round(float64(len(buf)-1) * pval))
return buf[at]
}
func (p *percentiles) Prune() {
p.lock.Lock()
defer p.lock.Unlock()
p.buf = make([]uint64, 0, p.cap)
}