forked from codesenberg/bombardier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats_test.go
202 lines (188 loc) · 3.97 KB
/
stats_test.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"math"
"math/rand"
"runtime"
"testing"
"time"
)
func TestShouldInitializeStats(t *testing.T) {
max := uint64(1000)
limit := max + 1
s := newStats(max)
if s.limit != limit {
t.Error("limit must be equal to max+1")
}
if s.min != math.MaxUint64 {
t.Error("min must be equal to MaxUint64")
}
if s.data == nil {
t.Error("data shouldn't be nil")
}
if len(s.data) != int(limit) {
t.Error("data's length must be equal to limit")
}
}
func TestStatsShouldntRecordValuesHigherThanMax(t *testing.T) {
max := uint64(100)
s := newStats(max)
if s.record(max + 1) {
t.Error("Shouldn't record values higher than max")
}
}
func TestStatsShouldRecordValues(t *testing.T) {
max := uint64(100)
s := newStats(max)
v := max / 2
if !s.record(v) {
t.Fail()
}
if s.count != 1 || s.data[v] != 1 || s.min != v || s.max != v {
t.Fail()
}
}
func TestStatsMean(t *testing.T) {
max := uint64(100)
s := newStats(max)
if s.mean() != 0 {
t.Fail()
}
for i := uint64(0); i <= max; i++ {
s.record(i)
}
if s.mean() != float64(max/2) {
t.Fail()
}
}
func TestStatsStdev(t *testing.T) {
max := uint64(100)
s := newStats(max)
mean := s.mean()
stdev := s.stdev(mean)
if stdev != 0 {
t.Fail()
}
for i := uint64(0); i <= max; i++ {
s.record(max / 2)
}
mean = s.mean()
stdev = s.stdev(mean)
if stdev != 0 {
t.Fail()
}
for i := uint64(0); i <= max; i++ {
s.record(0)
}
mean = s.mean()
stdev = s.stdev(mean)
if !equalFloats(float64(max/4), stdev, 0.5) {
t.Fail()
}
}
func TestEmptyStatsPercentile(t *testing.T) {
max := uint64(10000)
s := newStats(max)
expectations := []struct {
in float64
out uint64
}{
{25, 0},
{50, 0},
{75, 0},
{99, 0},
{99.99, 0},
}
for _, e := range expectations {
if s.percentile(e.in) != e.out {
t.Fail()
}
}
}
func TestStatsPercentile(t *testing.T) {
max := uint64(10000)
s := newStats(max)
for i := uint64(1); i <= max; i++ {
s.record(i)
}
expectations := []struct {
in float64
out uint64
}{
{25, 25 * (max / 100)},
{50, 50 * (max / 100)},
{75, 75 * (max / 100)},
{99, 99 * (max / 100)},
{99.99, uint64(float64(99.99) * float64(max/100))},
}
for _, e := range expectations {
if s.percentile(e.in) != e.out {
t.Fail()
}
}
}
var singleValueStats = func(max, val uint64) *stats {
s := newStats(max)
s.record(val)
return s
}
func TestStatsToStringConversions(t *testing.T) {
s := singleValueStats(100, 50)
expectations := []struct {
actual, expected string
}{
{rpsString(s), " Reqs/sec 50.00 0.00 50"},
{latenciesString(s), " Latency 50.00us 0.00us 50.00us"},
}
for _, exp := range expectations {
if exp.actual != exp.expected {
t.Log("Wanted: \"" + exp.expected + "\"")
t.Log("Got: \"" + exp.actual + "\"")
t.Fail()
}
}
}
func equalFloats(expected, actual, err float64) bool {
return expected-err < actual && actual < expected+err
}
func BenchmarkStatsRecord(b *testing.B) {
vcount := 10000000
mean := uint64(500)
stdev := uint64(200)
m := newStats(uint64(defaultTimeout.Nanoseconds() / 1000))
numCPU := runtime.NumCPU()
randomSequences := randomMeanSeqs(mean, stdev, vcount/10, numCPU)
b.SetParallelism(int(defaultNumberOfConns) / numCPU)
runtime.GC()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
values := randomSequences[rand.Intn(len(randomSequences))]
vlen := len(values)
counter := 0
for pb.Next() {
m.record(values[counter])
counter++
if counter == vlen {
counter = 0
}
}
})
}
func generateKeys(n int, mean, stdev uint64) []uint64 {
res := make([]uint64, n)
src := rand.NewSource(time.Now().UnixNano())
rng := rand.New(src)
for i := 0; i < n; i++ {
sample := rng.NormFloat64()*float64(stdev) + float64(mean)
res[i] = uint64(sample)
}
return res
}
func randomMeanSeqs(
mean, stdev uint64, partitionSize, numPartitions int,
) [][]uint64 {
results := make([][]uint64, numPartitions)
for i := range results {
results[i] = generateKeys(partitionSize, mean, stdev)
}
return results
}