-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbenchmarks_test.go
317 lines (267 loc) · 9.29 KB
/
benchmarks_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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package rest
import (
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestBenchmark_Stats(t *testing.T) {
bench := NewBenchmarks()
bench.update(time.Millisecond * 50)
bench.update(time.Millisecond * 150)
bench.update(time.Millisecond * 250)
bench.update(time.Millisecond * 100)
{
res := bench.Stats(time.Minute)
t.Logf("%+v", res)
assert.Equal(t, BenchmarkStats{Requests: 4, RequestsSec: 4, AverageRespTime: 137500,
MinRespTime: (time.Millisecond * 50).Microseconds(), MaxRespTime: (time.Millisecond * 250).Microseconds()}, res)
}
{
res := bench.Stats(time.Second * 5)
t.Logf("%+v", res)
assert.Equal(t, BenchmarkStats{Requests: 4, RequestsSec: 4, AverageRespTime: 137500,
MinRespTime: (time.Millisecond * 50).Microseconds(), MaxRespTime: (time.Millisecond * 250).Microseconds()}, res)
}
{
res := bench.Stats(time.Millisecond * 999)
t.Logf("%+v", res)
assert.Equal(t, BenchmarkStats{}, res)
}
}
func TestBenchmark_Stats2s(t *testing.T) {
bench := NewBenchmarks()
bench.update(time.Millisecond * 50)
bench.update(time.Millisecond * 150)
bench.update(time.Millisecond * 250)
time.Sleep(time.Second)
bench.update(time.Millisecond * 100)
res := bench.Stats(time.Minute)
t.Logf("%+v", res)
assert.Equal(t, BenchmarkStats{Requests: 4, RequestsSec: 2, AverageRespTime: 137500,
MinRespTime: (time.Millisecond * 50).Microseconds(), MaxRespTime: (time.Millisecond * 250).Microseconds()}, res)
}
func TestBenchmark_WithTimeRange(t *testing.T) {
nowFn := func(dt time.Time) func() time.Time {
return func() time.Time { return dt }
}
{
bench := NewBenchmarks().WithTimeRange(time.Minute)
bench.nowFn = nowFn(time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC))
bench.update(time.Millisecond * 50)
bench.update(time.Millisecond * 150)
bench.update(time.Millisecond * 250)
bench.update(time.Millisecond * 100)
bench.nowFn = nowFn(time.Date(2018, time.January, 1, 1, 0, 0, 0, time.UTC)) // 1 hour later
bench.update(time.Millisecond * 1000)
res := bench.Stats(time.Minute)
t.Logf("%+v", res)
assert.Equal(t, BenchmarkStats{Requests: 1, RequestsSec: 1, AverageRespTime: 1000000,
MinRespTime: (time.Millisecond * 1000).Microseconds(), MaxRespTime: (time.Millisecond * 1000).Microseconds()}, res)
res = bench.Stats(time.Hour)
t.Logf("%+v", res)
assert.Equal(t, BenchmarkStats{Requests: 1, RequestsSec: 1, AverageRespTime: 1000000,
MinRespTime: (time.Millisecond * 1000).Microseconds(), MaxRespTime: (time.Millisecond * 1000).Microseconds()}, res)
}
{
bench := NewBenchmarks().WithTimeRange(time.Hour * 2)
bench.nowFn = nowFn(time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC))
bench.update(time.Millisecond * 50)
bench.update(time.Millisecond * 150)
bench.update(time.Millisecond * 250)
bench.update(time.Millisecond * 100)
bench.nowFn = nowFn(time.Date(2018, time.January, 1, 1, 0, 0, 0, time.UTC)) // 1 hour later
bench.update(time.Millisecond * 1000)
res := bench.Stats(time.Minute)
t.Logf("%+v", res)
assert.Equal(t, BenchmarkStats{Requests: 1, RequestsSec: 1, AverageRespTime: 1000000,
MinRespTime: (time.Millisecond * 1000).Microseconds(), MaxRespTime: (time.Millisecond * 1000).Microseconds()}, res)
res = bench.Stats(time.Hour)
t.Logf("%+v", res)
assert.Equal(t, BenchmarkStats{Requests: 5, RequestsSec: 0.0013885031935573452, AverageRespTime: 310000,
MinRespTime: (time.Millisecond * 50).Microseconds(), MaxRespTime: (time.Millisecond * 1000).Microseconds()}, res)
}
}
func TestBenchmark_Cleanup(t *testing.T) {
bench := NewBenchmarks()
for i := 0; i < 1000; i++ {
bench.nowFn = func() time.Time {
return time.Date(2022, 5, 15, 0, 0, 0, 0, time.UTC).Add(time.Duration(i) * time.Second) // every 2s fake time
}
bench.update(time.Millisecond * 50)
}
{
res := bench.Stats(time.Hour)
t.Logf("%+v", res)
assert.Equal(t, BenchmarkStats{Requests: 900, RequestsSec: 1, AverageRespTime: 50000,
MinRespTime: (time.Millisecond * 50).Microseconds(), MaxRespTime: (time.Millisecond * 50).Microseconds()}, res)
}
{
res := bench.Stats(time.Minute)
t.Logf("%+v", res)
assert.Equal(t, BenchmarkStats{Requests: 60, RequestsSec: 1, AverageRespTime: 50000,
MinRespTime: (time.Millisecond * 50).Microseconds(), MaxRespTime: (time.Millisecond * 50).Microseconds()}, res)
}
assert.Equal(t, 900, bench.data.Len())
}
func TestBenchmarks_Handler(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, err := w.Write([]byte("blah blah"))
time.Sleep(time.Millisecond * 50)
require.NoError(t, err)
})
bench := NewBenchmarks()
ts := httptest.NewServer(bench.Handler(handler))
defer ts.Close()
for i := 0; i < 100; i++ {
resp, err := ts.Client().Get(ts.URL)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
}
{
res := bench.Stats(time.Minute)
t.Logf("%+v", res)
assert.Equal(t, 100, res.Requests)
assert.True(t, res.RequestsSec <= 20 && res.RequestsSec >= 10)
assert.InDelta(t, 50000, res.AverageRespTime, 10000)
assert.InDelta(t, 50000, res.MinRespTime, 10000)
assert.InDelta(t, 50000, res.MaxRespTime, 10000)
assert.True(t, res.MaxRespTime >= res.MinRespTime)
}
{
res := bench.Stats(time.Minute * 15)
t.Logf("%+v", res)
assert.Equal(t, 100, res.Requests)
assert.True(t, res.RequestsSec <= 20 && res.RequestsSec >= 10, res.RequestsSec)
assert.InDelta(t, 50000, res.AverageRespTime, 10000)
assert.InDelta(t, 50000, res.MinRespTime, 10000)
assert.InDelta(t, 50000, res.MaxRespTime, 10000)
assert.True(t, res.MaxRespTime >= res.MinRespTime)
}
}
func TestBenchmark_ConcurrentAccess(t *testing.T) {
bench := NewBenchmarks()
var wg sync.WaitGroup
// simulate concurrent updates
for i := 0; i < 100; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
bench.update(time.Duration(i) * time.Millisecond)
}(i)
}
// simulate concurrent stats reads while updating
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
stats := bench.Stats(time.Minute)
require.GreaterOrEqual(t, stats.Requests, 0)
}()
}
wg.Wait()
stats := bench.Stats(time.Minute)
assert.Equal(t, 100, stats.Requests)
}
func TestBenchmark_EdgeCases(t *testing.T) {
bench := NewBenchmarks()
t.Run("zero duration", func(t *testing.T) {
bench.update(0)
stats := bench.Stats(time.Minute)
assert.Equal(t, int64(0), stats.MinRespTime)
assert.Equal(t, int64(0), stats.MaxRespTime)
})
t.Run("very large duration", func(t *testing.T) {
bench.update(time.Hour)
stats := bench.Stats(time.Minute)
assert.Equal(t, time.Hour.Microseconds(), stats.MaxRespTime)
})
t.Run("negative stats interval", func(t *testing.T) {
stats := bench.Stats(-time.Minute)
assert.Equal(t, BenchmarkStats{}, stats)
})
}
func TestBenchmark_TimeWindowBoundaries(t *testing.T) {
bench := NewBenchmarks()
now := time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC)
bench.nowFn = func() time.Time { return now }
// add data points exactly at minute boundaries
for i := 0; i < 120; i++ {
bench.nowFn = func() time.Time {
return now.Add(time.Duration(i) * time.Second)
}
bench.update(time.Millisecond * 50)
}
tests := []struct {
name string
interval time.Duration
want int // expected number of requests
}{
{"exact minute", time.Minute, 60},
{"30 seconds", time.Second * 30, 30},
{"90 seconds", time.Second * 90, 90},
{"2 minutes", time.Minute * 2, 120},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
stats := bench.Stats(tt.interval)
assert.Equal(t, tt.want, stats.Requests, "interval %v should have %d requests", tt.interval, tt.want)
})
}
}
func TestBenchmark_CustomTimeRange(t *testing.T) {
tests := []struct {
name string
timeRange time.Duration
dataPoints int
wantKept int
}{
{"1 minute range", time.Minute, 120, 60},
{"5 minute range", time.Minute * 5, 400, 300},
{"custom 45s range", time.Second * 45, 100, 45},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bench := NewBenchmarks().WithTimeRange(tt.timeRange)
now := time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC)
// Add data points
for i := 0; i < tt.dataPoints; i++ {
bench.nowFn = func() time.Time {
return now.Add(time.Duration(i) * time.Second)
}
bench.update(time.Millisecond * 50)
}
assert.Equal(t, tt.wantKept, bench.data.Len(),
"should keep only %d data points for %v time range",
tt.wantKept, tt.timeRange)
})
}
}
func TestBenchmark_VariableLoad(t *testing.T) {
bench := NewBenchmarks()
now := time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC)
// simulate variable load pattern
patterns := []struct {
count int
duration time.Duration
}{
{10, time.Millisecond * 10}, // fast responses
{5, time.Millisecond * 100}, // medium responses
{2, time.Millisecond * 1000}, // slow responses
}
for i, p := range patterns {
bench.nowFn = func() time.Time {
return now.Add(time.Duration(i) * time.Second)
}
for j := 0; j < p.count; j++ {
bench.update(p.duration)
}
}
stats := bench.Stats(time.Minute)
assert.Equal(t, 17, stats.Requests) // total requests across all patterns
assert.Equal(t, int64(1000*1000), stats.MaxRespTime) // should be the max (1000ms = 1_000_000 microseconds)
assert.Equal(t, int64(10*1000), stats.MinRespTime) // should be the min (10ms = 10_000 microseconds)
}