Skip to content

Commit 0aeedde

Browse files
Consistently reset benchmark timers
1 parent a340844 commit 0aeedde

8 files changed

+28
-29
lines changed

internal/pkg/api/handleAck_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ import (
2828
)
2929

3030
func BenchmarkMakeUpdatePolicyBody(b *testing.B) {
31-
b.ReportAllocs()
32-
3331
const policyID = "ed110be4-c2a0-42b8-adc0-94c2f0569207"
3432
const newRev = 2
3533
const coord = 1
3634

35+
b.ResetTimer()
36+
b.ReportAllocs()
37+
3738
for n := 0; n < b.N; n++ {
3839
makeUpdatePolicyBody(policyID, newRev, coord)
3940
}
4041
}
4142

4243
func TestMakeUpdatePolicyBody(t *testing.T) {
43-
4444
const policyID = "ed110be4-c2a0-42b8-adc0-94c2f0569207"
4545
const newRev = 2
4646
const coord = 1

internal/pkg/bulk/bulk_integration_test.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -380,11 +380,9 @@ func benchmarkCreate(n int, b *testing.B) {
380380
}
381381

382382
func BenchmarkCreate(b *testing.B) {
383-
384383
benchmarks := []int{1, 64, 8192, 16384, 32768, 65536}
385384

386385
for _, n := range benchmarks {
387-
388386
bindFunc := func(n int) func(b *testing.B) {
389387
return func(b *testing.B) {
390388
benchmarkCreate(n, b)
@@ -398,8 +396,6 @@ func BenchmarkCreate(b *testing.B) {
398396
// Not a particularly useful benchmark, but gives some idea of memory overhead.
399397

400398
func benchmarkCRUD(n int, b *testing.B) {
401-
b.ReportAllocs()
402-
403399
ctx, cn := context.WithCancel(context.Background())
404400
defer cn()
405401
ctx = testlog.SetLogger(b).WithContext(ctx)
@@ -415,8 +411,11 @@ func benchmarkCRUD(n int, b *testing.B) {
415411
ch := make(chan error, n)
416412
var wait sync.WaitGroup
417413
wait.Add(n)
418-
for i := 0; i < n; i++ {
419414

415+
b.ResetTimer()
416+
b.ReportAllocs()
417+
418+
for i := 0; i < n; i++ {
420419
go func() {
421420
defer wait.Done()
422421

@@ -466,11 +465,9 @@ func benchmarkCRUD(n int, b *testing.B) {
466465
}
467466

468467
func BenchmarkCRUD(b *testing.B) {
469-
470468
benchmarks := []int{1, 64, 8192, 16384, 32768, 65536}
471469

472470
for _, n := range benchmarks {
473-
474471
bindFunc := func(n int) func(b *testing.B) {
475472
return func(b *testing.B) {
476473
benchmarkCRUD(n, b)

internal/pkg/bulk/bulk_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,6 @@ func TestCancelCtxChildBulker(t *testing.T) {
305305
}
306306

307307
func benchmarkMockBulk(b *testing.B, samples [][]byte) {
308-
b.ReportAllocs()
309308
mock := &mockBulkTransport{}
310309

311310
ctx, cancelF := context.WithCancel(context.Background())
@@ -334,8 +333,10 @@ func benchmarkMockBulk(b *testing.B, samples [][]byte) {
334333

335334
var wait sync.WaitGroup
336335
wait.Add(n)
337-
for i := 0; i < n; i++ {
338336

337+
b.ResetTimer()
338+
b.ReportAllocs()
339+
for i := 0; i < n; i++ {
339340
go func(sampleData []byte) {
340341
defer wait.Done()
341342

@@ -373,7 +374,6 @@ func benchmarkMockBulk(b *testing.B, samples [][]byte) {
373374
}
374375

375376
func BenchmarkMockBulk(b *testing.B) {
376-
377377
benchmarks := []int{1, 8, 64, 4096, 32768}
378378

379379
// Create the samples outside the loop to avoid accounting
@@ -391,7 +391,6 @@ func BenchmarkMockBulk(b *testing.B) {
391391
}
392392

393393
for _, n := range benchmarks {
394-
395394
bindFunc := func(n int) func(b *testing.B) {
396395
return func(b *testing.B) {
397396
benchmarkMockBulk(b, samples[:n])

internal/pkg/bulk/opMulti_integration_test.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,9 @@ import (
1515
testlog "github.com/elastic/fleet-server/v7/internal/pkg/testing/log"
1616
)
1717

18-
// This runs a series of CRUD operations through elastic.
18+
// benchmarkMultiUpdate runs a series of CRUD operations through elastic.
1919
// Not a particularly useful benchmark, but gives some idea of memory overhead.
20-
2120
func benchmarkMultiUpdate(n int, b *testing.B) {
22-
b.ReportAllocs()
23-
2421
ctx, cn := context.WithCancel(context.Background())
2522
defer cn()
2623
ctx = testlog.SetLogger(b).WithContext(ctx)
@@ -42,6 +39,9 @@ func benchmarkMultiUpdate(n int, b *testing.B) {
4239
b.Fatal(err)
4340
}
4441

42+
b.ResetTimer()
43+
b.ReportAllocs()
44+
4545
for j := 0; j < b.N; j++ {
4646
fields := UpdateFields{
4747
"dateval": time.Now().Format(time.RFC3339),
@@ -64,8 +64,9 @@ func benchmarkMultiUpdate(n int, b *testing.B) {
6464
}
6565
}
6666

67-
func BenchmarkMultiUpdate(b *testing.B) {
68-
67+
// BenchmarkMultiUpdateIntegration runs a benchmark for CRUD operations on a live ES instance
68+
// The results may be inconsistent due to the ES requirement.
69+
func BenchmarkMultiUpdateIntegration(b *testing.B) {
6970
benchmarks := []int{1, 64, 8192, 37268, 131072}
7071

7172
for _, n := range benchmarks {

internal/pkg/bulk/opMulti_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ func BenchmarkMultiUpdateMock(b *testing.B) {
5050

5151
for _, n := range benchmarks {
5252
b.Run(strconv.Itoa(n), func(b *testing.B) {
53-
b.ReportAllocs()
5453
ctx := testlog.SetLogger(b).WithContext(context.Background())
54+
b.ResetTimer()
55+
b.ReportAllocs()
5556
for i := 0; i < b.N; i++ {
5657
if _, err := bulker.MUpdate(ctx, ops[:n]); err != nil {
5758
b.Fatal(err)

internal/pkg/checkin/bulk_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,6 @@ func validateTimestamp(tb testing.TB, start time.Time, ts string) {
207207

208208
func benchmarkBulk(n int, flush bool, b *testing.B) {
209209
ctx := testlog.SetLogger(b).WithContext(context.Background())
210-
b.ReportAllocs()
211210

212211
mockBulk := ftesting.NewMockBulk()
213212
mockBulk.On("MUpdate", mock.Anything, mock.Anything, []bulk.Opt(nil)).Return([]bulk.BulkIndexerResponseItem{}, nil)
@@ -220,8 +219,10 @@ func benchmarkBulk(n int, flush bool, b *testing.B) {
220219
ids = append(ids, id)
221220
}
222221

223-
for i := 0; i < b.N; i++ {
222+
b.ResetTimer()
223+
b.ReportAllocs()
224224

225+
for i := 0; i < b.N; i++ {
225226
for _, id := range ids {
226227
err := bc.CheckIn(id, "", "", nil, nil, nil, "")
227228
if err != nil {

internal/pkg/dsl/tmpl_test.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ func makeQuery2(leaf1 interface{}, leaf2 interface{}) *Node {
3636
}
3737

3838
func BenchmarkRenderOne(b *testing.B) {
39-
4039
const kName = "api_key"
4140
tmpl := NewTmpl()
4241
token := tmpl.Bind(kName)
@@ -47,6 +46,7 @@ func BenchmarkRenderOne(b *testing.B) {
4746
b.Fatal(err)
4847
}
4948

49+
b.ResetTimer()
5050
// run the RenderOne function b.N times
5151
for n := 0; n < b.N; n++ {
5252
if _, err := tmpl.RenderOne(kName, "2Ye0F3UByTc0c1e9OeMO"); err != nil {
@@ -56,7 +56,6 @@ func BenchmarkRenderOne(b *testing.B) {
5656
}
5757

5858
func BenchmarkRender(b *testing.B) {
59-
6059
const kName = "api_key"
6160
tmpl := NewTmpl()
6261
token := tmpl.Bind(kName)
@@ -69,6 +68,7 @@ func BenchmarkRender(b *testing.B) {
6968

7069
v := "2Ye0F3UByTc0c1e9OeMO"
7170

71+
b.ResetTimer()
7272
// run the RenderOne function b.N times
7373
for n := 0; n < b.N; n++ {
7474
if _, err := tmpl.Render(map[string]interface{}{
@@ -80,7 +80,6 @@ func BenchmarkRender(b *testing.B) {
8080
}
8181

8282
func BenchmarkMarshalNode(b *testing.B) {
83-
8483
// run the RenderOne function b.N times
8584
for n := 0; n < b.N; n++ {
8685
query := makeQuery("2Ye0F3UByTc0c1e9OeMO")
@@ -89,7 +88,6 @@ func BenchmarkMarshalNode(b *testing.B) {
8988
}
9089

9190
func BenchmarkMarshalNode2(b *testing.B) {
92-
9391
// run the RenderOne function b.N times
9492
for n := 0; n < b.N; n++ {
9593
query := makeQuery2("27e58fc0-09a2-11eb-a8cd-57e98f140de5", 3)
@@ -106,14 +104,14 @@ func BenchmarkSprintf(b *testing.B) {
106104
policyRev := 3
107105

108106
var s string
107+
b.ResetTimer()
109108
for n := 0; n < b.N; n++ {
110109
s = fmt.Sprintf(queryTmpl, policyID, policyRev)
111110
}
112111
ssprintres = s
113112
}
114113

115114
func BenchmarkRender2(b *testing.B) {
116-
117115
const kName1 = "policyId"
118116
const kName2 = "policyRev"
119117

@@ -128,6 +126,7 @@ func BenchmarkRender2(b *testing.B) {
128126
}
129127

130128
// run the RenderOne function b.N times
129+
b.ResetTimer()
131130
for n := 0; n < b.N; n++ {
132131
m := map[string]interface{}{
133132
kName1: "27e58fc0-09a2-11eb-a8cd-57e98f140de5",

internal/pkg/policy/sub_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,9 @@ func TestSub_UnlinkRandomN(t *testing.T) {
193193
}
194194

195195
func BenchmarkSubsSimple(b *testing.B) {
196-
197196
head := makeHead()
198197
nn := NewSub("", "", 0, 0)
198+
b.ResetTimer()
199199
for i := 0; i < b.N; i++ {
200200
head.pushBack(nn)
201201
head.popFront()
@@ -225,6 +225,7 @@ func BenchmarkSubs(b *testing.B) {
225225

226226
for _, bm := range benchmarks {
227227
b.Run(fmt.Sprintf("%d", bm), func(b *testing.B) {
228+
b.ResetTimer()
228229

229230
for i := 0; i < b.N; i++ {
230231
for j := 0; j < bm; j++ {

0 commit comments

Comments
 (0)