-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuckets.go
107 lines (93 loc) · 2.26 KB
/
buckets.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
package lpfloat
import (
"bytes"
"errors"
"fmt"
"sort"
"strings"
)
type Buckets interface {
Insert(float64)
InsertN(float64, uint64)
Total() uint64
Sum() float64
Count(float64) uint64
Range(func(Bucket))
ReverseRange(func(Bucket))
Buckets() []Bucket
Summary([]float32) Summary
Reset()
}
var (
_ Buckets = &UnSyncBuckets{}
_ Buckets = &SyncBuckets{}
emptyBuckets = [256]uint64{}
)
type Bucket struct {
Value LPFloat
Count uint64
}
type Summary struct {
Min LPFloat
Max LPFloat
Avg LPFloat
Sum LPFloat
Total uint64
Percentiles []PercentilePair
}
func makeSummary(p []float32) Summary {
s := Summary{
Min: _NaN,
Max: _NaN,
Avg: _NaN,
Sum: _Zero,
Total: 0,
Percentiles: make([]PercentilePair, len(p)),
}
for i := range p {
s.Percentiles[i].Percentile = p[i]
s.Percentiles[i].LessThan = _NaN
}
return s
}
func (s Summary) String() string {
buf := bytes.NewBuffer(nil)
_, _ = fmt.Fprintf(buf, "Summary{Total: %d, Sum: %v, Avg: %v, Max: %v, Min: %v, Percentiles: %v}",
s.Total, s.Sum, s.Avg, s.Max, s.Min, s.Percentiles)
return buf.String()
}
func (s Summary) Format(f fmt.State, c rune) {
fmtCode := toFormatCode(f, c)
fmtStr := "Summary{Total: %d, Sum: _CODE_, Avg: _CODE_, Max: _CODE_, Min: _CODE_, Percentiles: _CODE_}"
fmtStr = strings.Replace(fmtStr, "_CODE_", fmtCode, -1)
_, _ = f.Write([]byte(fmt.Sprintf(fmtStr, s.Total, s.Sum, s.Avg, s.Max, s.Min, s.Percentiles)))
}
type PercentilePair struct {
Percentile float32 // [0, 100]
LessThan LPFloat
}
func (p PercentilePair) String() string {
return fmt.Sprintf("P%g: %v", p.Percentile, p.LessThan)
}
func (p PercentilePair) Format(f fmt.State, c rune) {
fmtStr := "P%g: " + toFormatCode(f, c)
_, _ = f.Write([]byte(fmt.Sprintf(fmtStr, p.Percentile, p.LessThan)))
}
func DefaultPercentilesCfg() []float32 {
defaultCfg := []float32{50, 80, 90, 95, 99, 99.9}
ret := make([]float32, len(defaultCfg))
copy(ret, defaultCfg)
return ret
}
func CheckPercentilesCfg(cfg []float32) error {
if len(cfg) == 0 {
return nil
}
sort.Slice(cfg, func(i, j int) bool {
return cfg[i] < cfg[j]
})
if cfg[0] <= 0 || cfg[len(cfg)-1] >= 100 {
return errors.New("the percentile should be between 0 and 100")
}
return nil
}