Skip to content

Commit 5ba3156

Browse files
committed
all: use testing.B.Loop()
Switch to using b.Loop() which prevents the compiler from optimising away the body. Signed-off-by: Lorenz Bauer <[email protected]>
1 parent 80e6a9d commit 5ba3156

20 files changed

+60
-105
lines changed

asm/instruction_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func TestRead64bitImmediate(t *testing.T) {
3535

3636
func BenchmarkRead64bitImmediate(b *testing.B) {
3737
r := &bytes.Reader{}
38-
for i := 0; i < b.N; i++ {
38+
for b.Loop() {
3939
r.Reset(test64bitImmProg)
4040

4141
var ins Instruction
@@ -64,7 +64,7 @@ func BenchmarkWrite64BitImmediate(b *testing.B) {
6464
ins := LoadImm(R0, math.MinInt32-1, DWord)
6565

6666
var buf bytes.Buffer
67-
for i := 0; i < b.N; i++ {
67+
for b.Loop() {
6868
buf.Reset()
6969

7070
if _, err := ins.Marshal(&buf, binary.LittleEndian); err != nil {

asm/metadata_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func BenchmarkMetadata(b *testing.B) {
5454
b.Run("add first", func(b *testing.B) {
5555
b.ReportAllocs()
5656

57-
for i := 0; i < b.N; i++ {
57+
for b.Loop() {
5858
var v Metadata
5959
v.Set(t{}, 0)
6060
}
@@ -67,9 +67,8 @@ func BenchmarkMetadata(b *testing.B) {
6767
}
6868

6969
b.ReportAllocs()
70-
b.ResetTimer()
7170

72-
for i := 0; i < b.N; i++ {
71+
for b.Loop() {
7372
v := m
7473
v.Set(t{}, 0)
7574
}
@@ -83,9 +82,8 @@ func BenchmarkMetadata(b *testing.B) {
8382
m.Set(t{}, 0)
8483

8584
b.ReportAllocs()
86-
b.ResetTimer()
8785

88-
for i := 0; i < b.N; i++ {
86+
for b.Loop() {
8987
v := m
9088
v.Set(t{}, 0)
9189
}
@@ -98,9 +96,8 @@ func BenchmarkMetadata(b *testing.B) {
9896
}
9997

10098
b.ReportAllocs()
101-
b.ResetTimer()
10299

103-
for i := 0; i < b.N; i++ {
100+
for b.Loop() {
104101
if m.Get(t{}) != nil {
105102
b.Fatal("got result from miss")
106103
}

btf/btf_test.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,8 @@ func TestTypeByName(t *testing.T) {
212212
func BenchmarkParseVmlinux(b *testing.B) {
213213
vmlinux := vmlinuxTestdataBytes(b)
214214
b.ReportAllocs()
215-
b.ResetTimer()
216215

217-
for n := 0; n < b.N; n++ {
216+
for b.Loop() {
218217
if _, err := loadRawSpec(vmlinux, nil); err != nil {
219218
b.Fatal("Can't load BTF:", err)
220219
}
@@ -224,9 +223,8 @@ func BenchmarkParseVmlinux(b *testing.B) {
224223
func BenchmarkIterateVmlinux(b *testing.B) {
225224
vmlinux := vmlinuxTestdataBytes(b)
226225
b.ReportAllocs()
227-
b.ResetTimer()
228226

229-
for range b.N {
227+
for b.Loop() {
230228
spec, err := loadRawSpec(vmlinux, nil)
231229
if err != nil {
232230
b.Fatal("Can't load BTF:", err)
@@ -556,9 +554,8 @@ func TestLoadEmptyRawSpec(t *testing.T) {
556554

557555
func BenchmarkSpecCopy(b *testing.B) {
558556
spec := vmlinuxTestdataSpec(b)
559-
b.ResetTimer()
560557

561-
for i := 0; i < b.N; i++ {
558+
for b.Loop() {
562559
spec.Copy()
563560
}
564561
}
@@ -567,8 +564,7 @@ func BenchmarkSpecTypeByID(b *testing.B) {
567564
spec := vmlinuxTestdataSpec(b)
568565

569566
b.ReportAllocs()
570-
b.ResetTimer()
571-
for i := 0; i < b.N; i++ {
567+
for b.Loop() {
572568
_, err := spec.TypeByID(1)
573569
if err != nil {
574570
b.Fatal(err)
@@ -612,9 +608,7 @@ func BenchmarkInspektorGadget(b *testing.B) {
612608

613609
var rd bytes.Reader
614610

615-
b.ResetTimer()
616-
617-
for range b.N {
611+
for b.Loop() {
618612
rd.Reset(vmlinux)
619613
spec, err := LoadSpecFromReader(&rd)
620614
if err != nil {

btf/core_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ func BenchmarkCORESkBuff(b *testing.B) {
695695
b.Run(relo.kind.String(), func(b *testing.B) {
696696
b.ReportAllocs()
697697

698-
for i := 0; i < b.N; i++ {
698+
for b.Loop() {
699699
_, err = CORERelocate([]*CORERelocation{relo}, []*Spec{spec}, spec.byteOrder, spec.TypeID)
700700
if err != nil {
701701
b.Fatal(err)

btf/ext_info_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ func BenchmarkParseLineInfoRecords(b *testing.B) {
3333
buf := make([]byte, size*count)
3434

3535
b.ReportAllocs()
36-
b.ResetTimer()
3736

38-
for i := 0; i < b.N; i++ {
37+
for b.Loop() {
3938
parseLineInfoRecords(bytes.NewReader(buf), internal.NativeEndian, size, count, true)
4039
}
4140
}

btf/marshal_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,8 @@ func BenchmarkMarshaler(b *testing.B) {
210210
types := typesFromSpec(b, vmlinuxTestdataSpec(b))[:100]
211211

212212
b.ReportAllocs()
213-
b.ResetTimer()
214213

215-
for i := 0; i < b.N; i++ {
214+
for b.Loop() {
216215
var b Builder
217216
for _, typ := range types {
218217
_, _ = b.Add(typ)
@@ -225,9 +224,8 @@ func BenchmarkBuildVmlinux(b *testing.B) {
225224
types := typesFromSpec(b, vmlinuxTestdataSpec(b))
226225

227226
b.ReportAllocs()
228-
b.ResetTimer()
229227

230-
for i := 0; i < b.N; i++ {
228+
for b.Loop() {
231229
var b Builder
232230
for _, typ := range types {
233231
_, _ = b.Add(typ)

btf/strings_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ func TestStringTableBuilder(t *testing.T) {
101101
func BenchmarkStringTableZeroLookup(b *testing.B) {
102102
strings := vmlinuxTestdataSpec(b).strings
103103

104-
b.ResetTimer()
105-
for i := 0; i < b.N; i++ {
104+
for b.Loop() {
106105
s, err := strings.Lookup(0)
107106
if err != nil {
108107
b.Fatal(err)

btf/traversal_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ func BenchmarkPostorderTraversal(b *testing.B) {
105105
} {
106106
b.Run(test.name, func(b *testing.B) {
107107
b.ReportAllocs()
108-
b.ResetTimer()
109-
for range b.N {
108+
for b.Loop() {
110109
for range postorder(test.typ, nil) {
111110
}
112111
}

btf/types_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,8 @@ func BenchmarkCopy(b *testing.B) {
111111
typ := newCyclicalType(10)
112112

113113
b.ReportAllocs()
114-
b.ResetTimer()
115114

116-
for i := 0; i < b.N; i++ {
115+
for b.Loop() {
117116
Copy(typ)
118117
}
119118
}
@@ -493,7 +492,7 @@ func BenchmarkWalk(b *testing.B) {
493492
b.Run(fmt.Sprint(typ), func(b *testing.B) {
494493
b.ReportAllocs()
495494

496-
for i := 0; i < b.N; i++ {
495+
for b.Loop() {
497496
var dq typeDeque
498497
for child := range children(typ) {
499498
dq.Push(child)
@@ -563,19 +562,17 @@ func BenchmarkUnderlyingType(b *testing.B) {
563562
b.Run("no unwrapping", func(b *testing.B) {
564563
v := &Int{}
565564
b.ReportAllocs()
566-
b.ResetTimer()
567565

568-
for i := 0; i < b.N; i++ {
566+
for b.Loop() {
569567
UnderlyingType(v)
570568
}
571569
})
572570

573571
b.Run("single unwrapping", func(b *testing.B) {
574572
v := &Typedef{Type: &Int{}}
575573
b.ReportAllocs()
576-
b.ResetTimer()
577574

578-
for i := 0; i < b.N; i++ {
575+
for b.Loop() {
579576
UnderlyingType(v)
580577
}
581578
})

collection_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -643,9 +643,8 @@ func BenchmarkNewCollection(b *testing.B) {
643643
spec = fixupCollectionSpec(spec)
644644

645645
b.ReportAllocs()
646-
b.ResetTimer()
647646

648-
for i := 0; i < b.N; i++ {
647+
for b.Loop() {
649648
coll, err := NewCollection(spec)
650649
if err != nil {
651650
b.Fatal(err)
@@ -664,9 +663,8 @@ func BenchmarkNewCollectionManyProgs(b *testing.B) {
664663
spec = fixupCollectionSpec(spec)
665664

666665
b.ReportAllocs()
667-
b.ResetTimer()
668666

669-
for i := 0; i < b.N; i++ {
667+
for b.Loop() {
670668
coll, err := NewCollection(spec)
671669
if err != nil {
672670
b.Fatal(err)
@@ -681,9 +679,8 @@ func BenchmarkLoadCollectionManyProgs(b *testing.B) {
681679
defer file.Close()
682680

683681
b.ReportAllocs()
684-
b.ResetTimer()
685682

686-
for i := 0; i < b.N; i++ {
683+
for b.Loop() {
687684
_, err := file.Seek(0, io.SeekStart)
688685
if err != nil {
689686
b.Fatal(err)

0 commit comments

Comments
 (0)