Skip to content

Commit 8129b23

Browse files
AskAlexSharovsudeepdino008
authored andcommitted
bench: b.Loop() (#19604)
it making benchmarks much faster (responsive. and AI doesn't stuck on them for long time waiting)
1 parent de01488 commit 8129b23

File tree

24 files changed

+76
-130
lines changed

24 files changed

+76
-130
lines changed

cl/phase1/execution_client/block_collector/interface_bench_test.go

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

3636
b.Run("Optimized", func(b *testing.B) {
3737
b.ReportAllocs()
38-
for i := 0; i < b.N; i++ {
38+
for b.Loop() {
3939
buf := make([]byte, 1+32+32+len(encodedPayload))
4040
buf[0] = version
4141
copy(buf[1:], parentRoot[:])
@@ -47,7 +47,7 @@ func BenchmarkEncodeBlockBuffer(b *testing.B) {
4747

4848
b.Run("Old", func(b *testing.B) {
4949
b.ReportAllocs()
50-
for i := 0; i < b.N; i++ {
50+
for b.Loop() {
5151
buf := append([]byte{version}, append(append(parentRoot[:], requestsHash[:]...), encodedPayload...)...)
5252
_ = buf
5353
}

cl/sentinel/handlers/light_client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ func BenchmarkLightClientPrefixConstruction(b *testing.B) {
409409

410410
b.Run("Optimized", func(b *testing.B) {
411411
b.ReportAllocs()
412-
for i := 0; i < b.N; i++ {
412+
for b.Loop() {
413413
var prefix [5]byte
414414
prefix[0] = SuccessfulResponsePrefix
415415
copy(prefix[1:], forkDigest[:])
@@ -419,7 +419,7 @@ func BenchmarkLightClientPrefixConstruction(b *testing.B) {
419419

420420
b.Run("Old", func(b *testing.B) {
421421
b.ReportAllocs()
422-
for i := 0; i < b.N; i++ {
422+
for b.Loop() {
423423
prefix := append([]byte{SuccessfulResponsePrefix}, forkDigest[:]...)
424424
_ = prefix
425425
}

common/crypto/blake2b/blake2b_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,6 @@ func benchmarkSum(b *testing.B, size int, sse4, avx, avx2 bool) {
315315

316316
data := make([]byte, size)
317317
b.SetBytes(int64(size))
318-
b.ResetTimer()
319318
for b.Loop() {
320319
Sum512(data)
321320
}
@@ -332,7 +331,6 @@ func benchmarkWrite(b *testing.B, size int, sse4, avx, avx2 bool) {
332331
data := make([]byte, size)
333332
h, _ := New512(nil)
334333
b.SetBytes(int64(size))
335-
b.ResetTimer()
336334
for b.Loop() {
337335
h.Write(data)
338336
}

common/lru/basiclru_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func BenchmarkLRU(b *testing.B) {
209209

210210
b.Run("Add/BasicLRU", func(b *testing.B) {
211211
cache := NewBasicLRU[int, int](capacity)
212-
for i := 0; i < b.N; i++ {
212+
for i := 0; b.Loop(); i++ {
213213
cache.Add(i, i)
214214
}
215215
})
@@ -221,7 +221,7 @@ func BenchmarkLRU(b *testing.B) {
221221
}
222222

223223
b.ResetTimer()
224-
for i := 0; i < b.N; i++ {
224+
for i := 0; b.Loop(); i++ {
225225
k := keys[indexes[i%len(indexes)]]
226226
v, ok := cache.Get(k)
227227
if ok {

common/maphash/maphash_test.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,6 @@ func BenchmarkMapSet(b *testing.B) {
289289
m := NewMap[int]()
290290
key := []byte("benchmark-key")
291291

292-
b.ResetTimer()
293292
for b.Loop() {
294293
m.Set(key, 123)
295294
}
@@ -301,7 +300,6 @@ func BenchmarkMapGet(b *testing.B) {
301300
key := []byte("benchmark-key")
302301
m.Set(key, 123)
303302

304-
b.ResetTimer()
305303
for b.Loop() {
306304
m.Get(key)
307305
}
@@ -503,7 +501,6 @@ func BenchmarkLRUSet(b *testing.B) {
503501
l, _ := NewLRU[int](10000)
504502
key := []byte("benchmark-key")
505503

506-
b.ResetTimer()
507504
for b.Loop() {
508505
l.Set(key, 123)
509506
}
@@ -515,7 +512,6 @@ func BenchmarkLRUGet(b *testing.B) {
515512
key := []byte("benchmark-key")
516513
l.Set(key, 123)
517514

518-
b.ResetTimer()
519515
for b.Loop() {
520516
l.Get(key)
521517
}
@@ -553,7 +549,6 @@ func BenchmarkMaphashMapSet(b *testing.B) {
553549
m := NewMap[int]()
554550
key := []byte("benchmark-key-that-is-48-bytes-long-like-pubkey!")
555551

556-
b.ResetTimer()
557552
for b.Loop() {
558553
m.Set(key, 123)
559554
}
@@ -563,7 +558,6 @@ func BenchmarkStringMapSet(b *testing.B) {
563558
m := NewStringMap[int]()
564559
key := []byte("benchmark-key-that-is-48-bytes-long-like-pubkey!")
565560

566-
b.ResetTimer()
567561
for b.Loop() {
568562
m.Set(key, 123)
569563
}
@@ -577,7 +571,6 @@ func BenchmarkMaphashMapGet(b *testing.B) {
577571
key := []byte("benchmark-key-that-is-48-bytes-long-like-pubkey!")
578572
m.Set(key, 123)
579573

580-
b.ResetTimer()
581574
for b.Loop() {
582575
m.Get(key)
583576
}
@@ -588,7 +581,6 @@ func BenchmarkStringMapGet(b *testing.B) {
588581
key := []byte("benchmark-key-that-is-48-bytes-long-like-pubkey!")
589582
m.Set(key, 123)
590583

591-
b.ResetTimer()
592584
for b.Loop() {
593585
m.Get(key)
594586
}
@@ -609,7 +601,6 @@ func BenchmarkMaphashMapSetManyKeys(b *testing.B) {
609601
keys[i][3] = byte(i)
610602
}
611603

612-
b.ResetTimer()
613604
i := 0
614605
for b.Loop() {
615606
m := NewMap[int]()
@@ -631,7 +622,6 @@ func BenchmarkStringMapSetManyKeys(b *testing.B) {
631622
keys[i][3] = byte(i)
632623
}
633624

634-
b.ResetTimer()
635625
i := 0
636626
for b.Loop() {
637627
m := NewStringMap[int]()
@@ -653,7 +643,6 @@ func BenchmarkUniqueHandleMapSetManyKeys(b *testing.B) {
653643
keys[i][3] = byte(i)
654644
}
655645

656-
b.ResetTimer()
657646
i := 0
658647
for b.Loop() {
659648
m := NewUniqueHandleMap[int]()
@@ -681,7 +670,6 @@ func BenchmarkMaphashMapGetManyKeys(b *testing.B) {
681670
m.Set(keys[i], i)
682671
}
683672

684-
b.ResetTimer()
685673
i := 0
686674
for b.Loop() {
687675
m.Get(keys[i%len(keys)])
@@ -703,7 +691,6 @@ func BenchmarkStringMapGetManyKeys(b *testing.B) {
703691
m.Set(keys[i], i)
704692
}
705693

706-
b.ResetTimer()
707694
i := 0
708695
for b.Loop() {
709696
m.Get(keys[i%len(keys)])
@@ -769,7 +756,6 @@ func BenchmarkUniqueHandleMapSet(b *testing.B) {
769756
m := NewUniqueHandleMap[int]()
770757
key := []byte("benchmark-key-that-is-48-bytes-long-like-pubkey!")
771758

772-
b.ResetTimer()
773759
for b.Loop() {
774760
m.Set(key, 123)
775761
}
@@ -780,7 +766,6 @@ func BenchmarkUniqueHandleMapGet(b *testing.B) {
780766
key := []byte("benchmark-key-that-is-48-bytes-long-like-pubkey!")
781767
m.Set(key, 123)
782768

783-
b.ResetTimer()
784769
for b.Loop() {
785770
m.Get(key)
786771
}
@@ -800,7 +785,6 @@ func BenchmarkUniqueHandleMapGetManyKeys(b *testing.B) {
800785
m.Set(keys[i], i)
801786
}
802787

803-
b.ResetTimer()
804788
i := 0
805789
for b.Loop() {
806790
m.Get(keys[i%len(keys)])

db/kv/mdbx/kv_mdbx_test.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,6 @@ func BenchmarkDB_BeginRO(b *testing.B) {
987987
_db := BaseCaseDBForBenchmark(b)
988988
db := _db.(*MdbxKV)
989989

990-
b.ResetTimer()
991990
for b.Loop() {
992991
tx, _ := db.BeginRo(context.Background())
993992
tx.Rollback()
@@ -1010,7 +1009,6 @@ func BenchmarkDB_Get(b *testing.B) {
10101009
// Ensure data is correct.
10111010
if err := db.View(context.Background(), func(tx kv.Tx) error {
10121011
key := u64tob(uint64(1))
1013-
b.ResetTimer()
10141012
for b.Loop() {
10151013
v, err := tx.GetOne(table, key)
10161014
if err != nil {
@@ -1031,13 +1029,12 @@ func BenchmarkDB_Put(b *testing.B) {
10311029
table := "Table"
10321030
db := _db.(*MdbxKV)
10331031

1034-
// Ensure data is correct.
1035-
keys := make([][]byte, b.N)
1036-
for i := 1; i <= b.N; i++ {
1032+
const keyCount = 10000
1033+
keys := make([][]byte, keyCount)
1034+
for i := 1; i <= keyCount; i++ {
10371035
keys[i-1] = u64tob(uint64(i))
10381036
}
10391037

1040-
b.ResetTimer()
10411038
if err := db.Update(context.Background(), func(tx kv.RwTx) error {
10421039
var idx int
10431040
for b.Loop() {
@@ -1082,8 +1079,9 @@ func BenchmarkDB_Delete(b *testing.B) {
10821079
table := "Table"
10831080
db := _db.(*MdbxKV)
10841081

1085-
keys := make([][]byte, b.N)
1086-
for i := 1; i <= b.N; i++ {
1082+
const keyCount = 10000
1083+
keys := make([][]byte, keyCount)
1084+
for i := 1; i <= keyCount; i++ {
10871085
keys[i-1] = u64tob(uint64(i))
10881086
}
10891087

@@ -1099,8 +1097,6 @@ func BenchmarkDB_Delete(b *testing.B) {
10991097
b.Fatal(err)
11001098
}
11011099

1102-
// Ensure data is correct.
1103-
b.ResetTimer()
11041100
if err := db.Update(context.Background(), func(tx kv.RwTx) error {
11051101
var idx int
11061102
for b.Loop() {

db/recsplit/multiencseq/sequence_builder_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,7 @@ func BenchmarkBuilder(b *testing.B) {
193193
vals[i] = baseNum + uint64(i)*2
194194
}
195195

196-
b.ResetTimer()
197-
for i := 0; i < b.N; i++ {
196+
for b.Loop() {
198197
sb := NewBuilder(baseNum, n, vals[n-1])
199198
for _, v := range vals {
200199
sb.AddOffset(v)

db/recsplit/multiencseq/sequence_reader_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,7 @@ func BenchmarkMerge(b *testing.B) {
276276

277277
var s1, s2 SequenceReader
278278
var merged SequenceBuilder
279-
b.ResetTimer()
280-
for i := 0; i < b.N; i++ {
279+
for b.Loop() {
281280
s1.Reset(baseNum, raw1)
282281
s2.Reset(baseNum, raw2)
283282
if err := merged.Merge(&s1, &s2, baseNum); err != nil {

db/recsplit/recsplit_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,6 @@ func BenchmarkFindSplit(b *testing.B) {
306306
salt := uint64(0x6453cec3f7376937) // startSeed[1]
307307
count := make([]uint16, secondaryAggrBound)
308308

309-
b.ResetTimer()
310309
for b.Loop() {
311310
for i := range buckets {
312311
findSplit(buckets[i][:], salt, fanout, unit, count)
@@ -329,7 +328,6 @@ func BenchmarkFindBijection(b *testing.B) {
329328
}
330329
salt := uint64(0x106393c187cae2a) // startSeed[0]
331330

332-
b.ResetTimer()
333331
for b.Loop() {
334332
for i := range buckets {
335333
findBijection(buckets[i][:], salt)
@@ -397,7 +395,7 @@ func BenchmarkAddKeyAndBuild(b *testing.B) {
397395
name = "enums"
398396
}
399397
b.Run(name, func(b *testing.B) {
400-
for i := 0; i < b.N; i++ {
398+
for i := 0; b.Loop(); i++ {
401399
b.StopTimer()
402400
indexFile := filepath.Join(tmpDir, fmt.Sprintf("index_full_%s_%d", name, i))
403401
rs, err := NewRecSplit(RecSplitArgs{

db/version/file_version_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,7 @@ func BenchmarkMatchVersionedFile(b *testing.B) {
414414
)
415415
}
416416

417-
b.ResetTimer()
418-
for i := 0; i < b.N; i++ {
417+
for b.Loop() {
419418
_, _, _, _ = MatchVersionedFile("*-accounts.0-1.kvi", dirEntries, "/tmp")
420419
}
421420
}

0 commit comments

Comments
 (0)