forked from caict-benchmark/BDC-TS
/
stats.go
58 lines (47 loc) · 1.03 KB
/
stats.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
package main
import "fmt"
// Stat represents one statistical measurement.
type Stat struct {
Label []byte
Value float64
}
// Init safely initializes a stat while minimizing heap allocations.
func (s *Stat) Init(label []byte, value float64) {
s.Label = s.Label[:0] // clear
s.Label = append(s.Label, label...)
s.Value = value
}
// StatGroup collects simple streaming statistics.
type StatGroup struct {
Min float64
Max float64
Mean float64
Sum float64
Count int64
}
// Push updates a StatGroup with a new value.
func (s *StatGroup) Push(n float64) {
if s.Count == 0 {
s.Min = n
s.Max = n
s.Mean = n
s.Count = 1
s.Sum = n
return
}
if n < s.Min {
s.Min = n
}
if n > s.Max {
s.Max = n
}
s.Sum += n
// constant-space mean update:
sum := s.Mean*float64(s.Count) + n
s.Mean = sum / float64(s.Count+1)
s.Count++
}
// String makes a simple description of a StatGroup.
func (s *StatGroup) String() string {
return fmt.Sprintf("min: %f, max: %f, mean: %f, count: %d, sum: %f", s.Min, s.Max, s.Mean, s.Count, s.Sum)
}