-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstat.go
More file actions
49 lines (40 loc) · 780 Bytes
/
stat.go
File metadata and controls
49 lines (40 loc) · 780 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
47
48
49
package querydigest
import (
"fmt"
"math"
"sort"
"strings"
)
type Histogram []float64
var divider = []float64{1, 10, 100, 1000, 10000, 100000, 1000000, math.Inf(0)}
var dividerLabel = []string{
" 1us",
" 10us",
"100us",
" 1ms",
" 10ms",
"100ms",
" 1s",
" 10s~",
}
const maxLength = 75
func (h Histogram) String() string {
tmp := make([]float64, len(h))
copy(tmp, h)
sort.Float64Slice(tmp).Sort()
max := tmp[len(h)-1]
unit := maxLength / max
var b strings.Builder
for i := 0; i < len(divider); i++ {
if len(h) <= i {
fmt.Fprintf(&b, "%s:\t\n", dividerLabel[i])
continue
}
length := int(unit * h[i])
if length <= 0 {
length = 0
}
fmt.Fprintf(&b, "%s:\t%s\n", dividerLabel[i], strings.Repeat("#", length))
}
return b.String()
}