forked from codesenberg/bombardier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.go
95 lines (86 loc) · 1.77 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
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
package main
import (
"fmt"
"math"
"sync/atomic"
)
type stats struct {
count uint64
limit uint64
min uint64
max uint64
data []uint64
}
func newStats(max uint64) *stats {
s := new(stats)
s.limit = max + 1
s.min = math.MaxUint64
s.data = make([]uint64, s.limit)
return s
}
func (s *stats) record(val uint64) bool {
if val >= s.limit {
return false
}
atomic.AddUint64(&s.data[val], 1)
atomic.AddUint64(&s.count, 1)
min := atomic.LoadUint64(&s.min)
for ; val < min; min = atomic.LoadUint64(&s.min) {
atomic.CompareAndSwapUint64(&s.min, min, val)
}
max := atomic.LoadUint64(&s.max)
for ; val > max; max = atomic.LoadUint64(&s.max) {
atomic.CompareAndSwapUint64(&s.max, max, val)
}
return true
}
func (s *stats) mean() float64 {
if s.count == 0 {
return 0.0
}
sum := uint64(0)
for i := s.min; i <= s.max; i++ {
sum += s.data[i] * i
}
return float64(sum) / float64(s.count)
}
func (s *stats) stdev(mean float64) float64 {
if s.count < 2 {
return 0.0
}
sum := 0.0
for i := s.min; i <= s.max; i++ {
if s.data[i] != 0 {
sum += math.Pow(float64(i)-mean, 2) * float64(s.data[i])
}
}
return math.Sqrt(sum / float64(s.count-1))
}
func (s *stats) percentile(p float64) uint64 {
rank := uint64((p/100.0)*float64(s.count) + 0.5)
total := uint64(0)
for i := s.min; i <= s.max; i++ {
total += s.data[i]
if total >= rank {
return i
}
}
return 0
}
func rpsString(s *stats) string {
mean := s.mean()
stdev := s.stdev(mean)
max := s.max
return fmt.Sprintf(" %-10v %10.2f %10.2f %10d",
"Reqs/sec", mean, stdev, max)
}
func latenciesString(s *stats) string {
mean := s.mean()
stdev := s.stdev(mean)
max := s.max
return fmt.Sprintf(" %-10v %10v %10v %10v",
"Latency",
formatTimeUs(mean),
formatTimeUs(stdev),
formatTimeUs(float64(max)))
}