Skip to content

Commit d11cd5c

Browse files
kolyshkingopherbot
authored andcommitted
all: use b.Loop in tests
Generated by modernize. Change-Id: Ie17ccdcc10f818ca4c214a9c5c3bf166ea77bb18 Reviewed-on: https://go-review.googlesource.com/c/tools/+/703079 Auto-Submit: Kirill Kolyshkin <[email protected]> Reviewed-by: Mark Freeman <[email protected]> Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 7ebabfe commit d11cd5c

File tree

13 files changed

+49
-63
lines changed

13 files changed

+49
-63
lines changed

container/intsets/sparse_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -646,9 +646,8 @@ func benchmarkInsertProbeSparse(b *testing.B, size, spread int) {
646646
probe[i] = prng.Int() % spread
647647
}
648648

649-
b.ResetTimer()
650649
var x intsets.Sparse
651-
for tries := 0; tries < b.N; tries++ {
650+
for b.Loop() {
652651
x.Clear()
653652
for _, n := range insert {
654653
x.Insert(n)
@@ -688,7 +687,7 @@ func BenchmarkInsertProbeSparse_100_10000(b *testing.B) {
688687

689688
func BenchmarkUnionDifferenceSparse(b *testing.B) {
690689
prng := rand.New(rand.NewSource(0))
691-
for tries := 0; tries < b.N; tries++ {
690+
for b.Loop() {
692691
var x, y, z intsets.Sparse
693692
for i := range 1000 {
694693
n := int(prng.Int()) % 100000
@@ -705,7 +704,7 @@ func BenchmarkUnionDifferenceSparse(b *testing.B) {
705704

706705
func BenchmarkUnionDifferenceHashTable(b *testing.B) {
707706
prng := rand.New(rand.NewSource(0))
708-
for tries := 0; tries < b.N; tries++ {
707+
for b.Loop() {
709708
x, y, z := make(map[int]bool), make(map[int]bool), make(map[int]bool)
710709
for i := range 1000 {
711710
n := int(prng.Int()) % 100000
@@ -739,7 +738,7 @@ func BenchmarkAppendTo(b *testing.B) {
739738
x.Insert(int(prng.Int()) % 10000)
740739
}
741740
var space [1000]int
742-
for tries := 0; tries < b.N; tries++ {
741+
for b.Loop() {
743742
x.AppendTo(space[:0])
744743
}
745744
}

cover/profile_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ func stringifyProfileArray(profiles []*Profile) string {
241241
func BenchmarkParseLine(b *testing.B) {
242242
const line = "k8s.io/kubernetes/cmd/kube-controller-manager/app/options/ttlafterfinishedcontroller.go:31.73,32.14 1 1"
243243
b.SetBytes(int64(len(line)))
244-
for n := 0; n < b.N; n++ {
244+
for b.Loop() {
245245
parseLine(line)
246246
}
247247
}

go/ast/astutil/rewrite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ var sink ast.Node
270270
func BenchmarkRewrite(b *testing.B) {
271271
for _, test := range rewriteTests {
272272
b.Run(test.name, func(b *testing.B) {
273-
for i := 0; i < b.N; i++ {
273+
for b.Loop() {
274274
b.StopTimer()
275275
fset := token.NewFileSet()
276276
f, err := parser.ParseFile(fset, test.name, test.orig, parser.ParseComments)

go/ast/inspector/cursor_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ func BenchmarkInspectCalls(b *testing.B) {
407407

408408
b.Run("Preorder", func(b *testing.B) {
409409
var ncalls int
410-
for range b.N {
410+
for b.Loop() {
411411
inspect.Preorder(callExprs, func(n ast.Node) {
412412
_ = n.(*ast.CallExpr)
413413
ncalls++
@@ -417,7 +417,7 @@ func BenchmarkInspectCalls(b *testing.B) {
417417

418418
b.Run("WithStack", func(b *testing.B) {
419419
var ncalls int
420-
for range b.N {
420+
for b.Loop() {
421421
inspect.WithStack(callExprs, func(n ast.Node, push bool, stack []ast.Node) (proceed bool) {
422422
_ = n.(*ast.CallExpr)
423423
if push {
@@ -430,7 +430,7 @@ func BenchmarkInspectCalls(b *testing.B) {
430430

431431
b.Run("Cursor", func(b *testing.B) {
432432
var ncalls int
433-
for range b.N {
433+
for b.Loop() {
434434
for cur := range inspect.Root().Preorder(callExprs...) {
435435
_ = cur.Node().(*ast.CallExpr)
436436
ncalls++
@@ -440,7 +440,7 @@ func BenchmarkInspectCalls(b *testing.B) {
440440

441441
b.Run("CursorEnclosing", func(b *testing.B) {
442442
var ncalls int
443-
for range b.N {
443+
for b.Loop() {
444444
for cur := range inspect.Root().Preorder(callExprs...) {
445445
_ = cur.Node().(*ast.CallExpr)
446446
for range cur.Enclosing() {
@@ -480,7 +480,7 @@ func BenchmarkCursor_FindNode(b *testing.B) {
480480

481481
b.Run("Cursor.Preorder", func(b *testing.B) {
482482
needleNode := needle.Node()
483-
for range b.N {
483+
for b.Loop() {
484484
var found inspector.Cursor
485485
for c := range root.Preorder(callExprs...) {
486486
if c.Node() == needleNode {
@@ -496,7 +496,7 @@ func BenchmarkCursor_FindNode(b *testing.B) {
496496

497497
// This method is about 10-15% faster than Cursor.Preorder.
498498
b.Run("Cursor.FindNode", func(b *testing.B) {
499-
for range b.N {
499+
for b.Loop() {
500500
found, ok := root.FindNode(needle.Node())
501501
if !ok || found != needle {
502502
b.Errorf("FindNode search failed: got %v, want %v", found, needle)
@@ -507,7 +507,7 @@ func BenchmarkCursor_FindNode(b *testing.B) {
507507
// This method is about 100x (!) faster than Cursor.Preorder.
508508
b.Run("Cursor.FindPos", func(b *testing.B) {
509509
needleNode := needle.Node()
510-
for range b.N {
510+
for b.Loop() {
511511
found, ok := root.FindByPos(needleNode.Pos(), needleNode.End())
512512
if !ok || found != needle {
513513
b.Errorf("FindPos search failed: got %v, want %v", found, needle)

go/ast/inspector/inspector_test.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -253,19 +253,18 @@ func typeOf(n ast.Node) string {
253253

254254
func BenchmarkNewInspector(b *testing.B) {
255255
// Measure one-time construction overhead.
256-
for i := 0; i < b.N; i++ {
256+
for b.Loop() {
257257
inspector.New(netFiles)
258258
}
259259
}
260260

261261
func BenchmarkInspect(b *testing.B) {
262-
b.StopTimer()
262+
263263
inspect := inspector.New(netFiles)
264-
b.StartTimer()
265264

266265
// Measure marginal cost of traversal.
267266
var ndecls, nlits int
268-
for i := 0; i < b.N; i++ {
267+
for b.Loop() {
269268
inspect.Preorder(nil, func(n ast.Node) {
270269
switch n.(type) {
271270
case *ast.FuncDecl:
@@ -278,14 +277,13 @@ func BenchmarkInspect(b *testing.B) {
278277
}
279278

280279
func BenchmarkInspectFilter(b *testing.B) {
281-
b.StopTimer()
280+
282281
inspect := inspector.New(netFiles)
283-
b.StartTimer()
284282

285283
// Measure marginal cost of traversal.
286284
nodeFilter := []ast.Node{(*ast.FuncDecl)(nil), (*ast.FuncLit)(nil)}
287285
var ndecls, nlits int
288-
for i := 0; i < b.N; i++ {
286+
for b.Loop() {
289287
inspect.Preorder(nodeFilter, func(n ast.Node) {
290288
switch n.(type) {
291289
case *ast.FuncDecl:
@@ -299,7 +297,7 @@ func BenchmarkInspectFilter(b *testing.B) {
299297

300298
func BenchmarkASTInspect(b *testing.B) {
301299
var ndecls, nlits int
302-
for i := 0; i < b.N; i++ {
300+
for b.Loop() {
303301
for _, f := range netFiles {
304302
ast.Inspect(f, func(n ast.Node) bool {
305303
switch n.(type) {

go/ast/inspector/iter_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,10 @@ func firstN[T any](n int, seq iter.Seq[T]) (res []T) {
6969
// (The iterator adds about 5-15%.)
7070
func BenchmarkAllCalls(b *testing.B) {
7171
inspect := inspector.New(netFiles)
72-
b.ResetTimer()
7372

7473
// Measure marginal cost of traversal.
7574
var ncalls int
76-
for range b.N {
75+
for b.Loop() {
7776
for range inspector.All[*ast.CallExpr](inspect) {
7877
ncalls++
7978
}

go/callgraph/callgraph_test.go

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -98,68 +98,62 @@ func logStats(b *testing.B, cnd bool, name string, cg *callgraph.Graph, main *ss
9898
}
9999

100100
func BenchmarkStatic(b *testing.B) {
101-
b.StopTimer()
101+
102102
prog, main := example(b)
103-
b.StartTimer()
104103

105-
for i := 0; i < b.N; i++ {
104+
for i := 0; b.Loop(); i++ {
106105
cg := static.CallGraph(prog)
107106
logStats(b, i == 0, "static", cg, main)
108107
}
109108
}
110109

111110
func BenchmarkCHA(b *testing.B) {
112-
b.StopTimer()
111+
113112
prog, main := example(b)
114-
b.StartTimer()
115113

116-
for i := 0; i < b.N; i++ {
114+
for i := 0; b.Loop(); i++ {
117115
cg := cha.CallGraph(prog)
118116
logStats(b, i == 0, "cha", cg, main)
119117
}
120118
}
121119

122120
func BenchmarkRTA(b *testing.B) {
123-
b.StopTimer()
121+
124122
_, main := example(b)
125-
b.StartTimer()
126123

127-
for i := 0; i < b.N; i++ {
124+
for i := 0; b.Loop(); i++ {
128125
res := rta.Analyze([]*ssa.Function{main}, true)
129126
cg := res.CallGraph
130127
logStats(b, i == 0, "rta", cg, main)
131128
}
132129
}
133130

134131
func BenchmarkVTA(b *testing.B) {
135-
b.StopTimer()
132+
136133
prog, main := example(b)
137-
b.StartTimer()
138134

139-
for i := 0; i < b.N; i++ {
135+
for i := 0; b.Loop(); i++ {
140136
cg := vta.CallGraph(ssautil.AllFunctions(prog), cha.CallGraph(prog))
141137
logStats(b, i == 0, "vta", cg, main)
142138
}
143139
}
144140

145141
func BenchmarkVTA2(b *testing.B) {
146-
b.StopTimer()
142+
147143
prog, main := example(b)
148-
b.StartTimer()
149144

150-
for i := 0; i < b.N; i++ {
145+
for i := 0; b.Loop(); i++ {
151146
vta1 := vta.CallGraph(ssautil.AllFunctions(prog), cha.CallGraph(prog))
152147
cg := vta.CallGraph(reaches(main, vta1, true), vta1)
153148
logStats(b, i == 0, "vta2", cg, main)
154149
}
155150
}
156151

157152
func BenchmarkVTA3(b *testing.B) {
158-
b.StopTimer()
153+
159154
prog, main := example(b)
160-
b.StartTimer()
161155

162-
for i := 0; i < b.N; i++ {
156+
for i := 0; b.Loop(); i++ {
163157
vta1 := vta.CallGraph(ssautil.AllFunctions(prog), cha.CallGraph(prog))
164158
vta2 := vta.CallGraph(reaches(main, vta1, true), vta1)
165159
cg := vta.CallGraph(reaches(main, vta2, true), vta2)
@@ -168,23 +162,21 @@ func BenchmarkVTA3(b *testing.B) {
168162
}
169163

170164
func BenchmarkVTAAlt(b *testing.B) {
171-
b.StopTimer()
165+
172166
prog, main := example(b)
173-
b.StartTimer()
174167

175-
for i := 0; i < b.N; i++ {
168+
for i := 0; b.Loop(); i++ {
176169
cha := cha.CallGraph(prog)
177170
cg := vta.CallGraph(reaches(main, cha, true), cha) // start from only functions reachable by CHA.
178171
logStats(b, i == 0, "vta-alt", cg, main)
179172
}
180173
}
181174

182175
func BenchmarkVTAAlt2(b *testing.B) {
183-
b.StopTimer()
176+
184177
prog, main := example(b)
185-
b.StartTimer()
186178

187-
for i := 0; i < b.N; i++ {
179+
for i := 0; b.Loop(); i++ {
188180
cha := cha.CallGraph(prog)
189181
vta1 := vta.CallGraph(reaches(main, cha, true), cha)
190182
cg := vta.CallGraph(reaches(main, vta1, true), vta1)

go/types/typeutil/map_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,9 +448,8 @@ func BenchmarkMap(b *testing.B) {
448448
allTypes[tv.Type] = true
449449
}
450450
})
451-
b.ResetTimer()
452451

453-
for range b.N {
452+
for b.Loop() {
454453
// De-duplicate the logically identical types.
455454
var tmap typeutil.Map
456455
for t := range allTypes {

internal/diff/lcs/old_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func TestDiffAPI(t *testing.T) {
160160

161161
func BenchmarkTwoOld(b *testing.B) {
162162
tests := genBench(rng(b), "abc", 96)
163-
for i := 0; i < b.N; i++ {
163+
for b.Loop() {
164164
for _, tt := range tests {
165165
_, two := compute(stringSeqs{tt.before, tt.after}, twosided, 100)
166166
if !two.valid() {
@@ -172,7 +172,7 @@ func BenchmarkTwoOld(b *testing.B) {
172172

173173
func BenchmarkForwOld(b *testing.B) {
174174
tests := genBench(rng(b), "abc", 96)
175-
for i := 0; i < b.N; i++ {
175+
for b.Loop() {
176176
for _, tt := range tests {
177177
_, two := compute(stringSeqs{tt.before, tt.after}, forward, 100)
178178
if !two.valid() {
@@ -236,23 +236,23 @@ func BenchmarkLargeFileSmallDiff(b *testing.B) {
236236
src := string(data)
237237
dst := src[:n*49/100] + src[n*51/100:] // remove 2% from the middle
238238
b.Run("string", func(b *testing.B) {
239-
for i := 0; i < b.N; i++ {
239+
for b.Loop() {
240240
compute(stringSeqs{src, dst}, twosided, len(src)+len(dst))
241241
}
242242
})
243243

244244
srcBytes := []byte(src)
245245
dstBytes := []byte(dst)
246246
b.Run("bytes", func(b *testing.B) {
247-
for i := 0; i < b.N; i++ {
247+
for b.Loop() {
248248
compute(bytesSeqs{srcBytes, dstBytes}, twosided, len(srcBytes)+len(dstBytes))
249249
}
250250
})
251251

252252
srcRunes := []rune(src)
253253
dstRunes := []rune(dst)
254254
b.Run("runes", func(b *testing.B) {
255-
for i := 0; i < b.N; i++ {
255+
for b.Loop() {
256256
compute(runesSeqs{srcRunes, dstRunes}, twosided, len(srcRunes)+len(dstRunes))
257257
}
258258
})

internal/event/bench_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ func B(ctx context.Context, hooks Hooks, a int, b string) int {
140140
func (hooks Hooks) runBenchmark(b *testing.B) {
141141
ctx := context.Background()
142142
b.ReportAllocs()
143-
b.ResetTimer()
143+
144144
var acc int
145-
for i := 0; i < b.N; i++ {
145+
for b.Loop() {
146146
for _, value := range initialList {
147147
acc += A(ctx, hooks, value)
148148
}

0 commit comments

Comments
 (0)