Skip to content

Commit 241a5b3

Browse files
authored
all: using testing.B.Loop (#1554)
* p2p: using testing.B.Loop * core/state: using testing.B.Loop * eth: using testing.B.Loop * log: using testing.B.Loop * core: using testing.B.Loop * core/vm: using testing.B.Loop * core/types: using testing.B.Loop * crypto: using testing.B.Loop
1 parent d20f910 commit 241a5b3

File tree

19 files changed

+40
-63
lines changed

19 files changed

+40
-63
lines changed

core/bench_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ func makeChainForBench(db ethdb.Database, full bool, count uint64) {
245245
}
246246

247247
func benchWriteChain(b *testing.B, full bool, count uint64) {
248-
for i := 0; i < b.N; i++ {
248+
for b.Loop() {
249249
dir := b.TempDir()
250250
db, err := rawdb.NewLevelDBDatabase(dir, 128, 1024, "", false)
251251
if err != nil {
@@ -268,9 +268,7 @@ func benchReadChain(b *testing.B, full bool, count uint64) {
268268
db.Close()
269269

270270
b.ReportAllocs()
271-
b.ResetTimer()
272-
273-
for i := 0; i < b.N; i++ {
271+
for b.Loop() {
274272
db, err := rawdb.NewLevelDBDatabase(dir, 128, 1024, "", false)
275273
if err != nil {
276274
b.Fatalf("error opening database at %v: %v", dir, err)

core/state/state_object_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,22 @@ import (
2525

2626
func BenchmarkCutOriginal(b *testing.B) {
2727
value := common.HexToHash("0x01")
28-
for i := 0; i < b.N; i++ {
28+
for b.Loop() {
2929
bytes.TrimLeft(value[:], "\x00")
3030
}
3131
}
3232

3333
func BenchmarkCutsetterFn(b *testing.B) {
3434
value := common.HexToHash("0x01")
3535
cutSetFn := func(r rune) bool { return r == 0 }
36-
for i := 0; i < b.N; i++ {
36+
for b.Loop() {
3737
bytes.TrimLeftFunc(value[:], cutSetFn)
3838
}
3939
}
4040

4141
func BenchmarkCutCustomTrim(b *testing.B) {
4242
value := common.HexToHash("0x01")
43-
for i := 0; i < b.N; i++ {
43+
for b.Loop() {
4444
common.TrimLeftZeroes(value[:])
4545
}
4646
}

core/types/block_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,8 @@ var benchBuffer = bytes.NewBuffer(make([]byte, 0, 32000))
139139

140140
func BenchmarkEncodeBlock(b *testing.B) {
141141
block := makeBenchBlock()
142-
b.ResetTimer()
143142

144-
for i := 0; i < b.N; i++ {
143+
for b.Loop() {
145144
benchBuffer.Reset()
146145
if err := rlp.Encode(benchBuffer, block); err != nil {
147146
b.Fatal(err)

core/types/hashing_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,16 @@ func BenchmarkDeriveSha200(b *testing.B) {
6767
var exp common.Hash
6868
var got common.Hash
6969
b.Run("std_trie", func(b *testing.B) {
70-
b.ResetTimer()
7170
b.ReportAllocs()
72-
for i := 0; i < b.N; i++ {
71+
for b.Loop() {
7372
exp = types.DeriveSha(txs, trie.NewEmpty(trie.NewDatabase(rawdb.NewMemoryDatabase())))
7473
}
7574
})
7675

7776
b.Run("stack_trie", func(b *testing.B) {
7877
b.ResetTimer()
7978
b.ReportAllocs()
80-
for i := 0; i < b.N; i++ {
79+
for b.Loop() {
8180
got = types.DeriveSha(txs, trie.NewStackTrie(nil))
8281
}
8382
})

core/vm/contracts_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -605,12 +605,10 @@ func benchmarkPrecompiled(addr string, test precompiledTest, bench *testing.B) {
605605
)
606606

607607
bench.Run(fmt.Sprintf("%s-Gas=%d", test.name, reqGas), func(bench *testing.B) {
608-
bench.ResetTimer()
609-
for i := 0; i < bench.N; i++ {
608+
for bench.Loop() {
610609
copy(data, in)
611610
res, _, err = RunPrecompiledContract(nil, p, data, reqGas, nil)
612611
}
613-
bench.StopTimer()
614612
//Check if it is correct
615613
if err != nil {
616614
bench.Error(err)

core/vm/instructions_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -313,15 +313,13 @@ func opBenchmark(bench *testing.B, op executionFunc, args ...string) {
313313
intArgs[i] = new(uint256.Int).SetBytes(common.Hex2Bytes(arg))
314314
}
315315
pc := uint64(0)
316-
bench.ResetTimer()
317-
for i := 0; i < bench.N; i++ {
316+
for bench.Loop() {
318317
for _, arg := range intArgs {
319318
stack.push(arg)
320319
}
321320
op(&pc, evmInterpreter, scope)
322321
stack.pop()
323322
}
324-
bench.StopTimer()
325323

326324
for i, arg := range args {
327325
want := new(uint256.Int).SetBytes(common.Hex2Bytes(arg))
@@ -579,8 +577,7 @@ func BenchmarkOpMstore(bench *testing.B) {
579577
memStart := new(uint256.Int)
580578
value := new(uint256.Int).SetUint64(0x1337)
581579

582-
bench.ResetTimer()
583-
for i := 0; i < bench.N; i++ {
580+
for bench.Loop() {
584581
stack.push(value)
585582
stack.push(memStart)
586583
opMstore(&pc, evmInterpreter, &ScopeContext{mem, stack, nil})
@@ -642,8 +639,7 @@ func BenchmarkOpKeccak256(bench *testing.B) {
642639
pc := uint64(0)
643640
start := new(uint256.Int)
644641

645-
bench.ResetTimer()
646-
for i := 0; i < bench.N; i++ {
642+
for bench.Loop() {
647643
stack.push(uint256.NewInt(32))
648644
stack.push(start)
649645
opKeccak256(&pc, evmInterpreter, &ScopeContext{mem, stack, nil})

core/vm/runtime/runtime_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,7 @@ func BenchmarkCall(b *testing.B) {
149149
b.Fatal(err)
150150
}
151151

152-
b.ResetTimer()
153-
for i := 0; i < b.N; i++ {
152+
for b.Loop() {
154153
for j := 0; j < 400; j++ {
155154
Execute(code, cpurchase, nil)
156155
Execute(code, creceived, nil)
@@ -190,11 +189,9 @@ func benchmarkEVM_Create(bench *testing.B, code string) {
190189
EVMConfig: vm.Config{},
191190
}
192191
// Warm up the intpools and stuff
193-
bench.ResetTimer()
194-
for i := 0; i < bench.N; i++ {
192+
for bench.Loop() {
195193
Call(receiver, []byte{}, &runtimeConfig)
196194
}
197-
bench.StopTimer()
198195
}
199196

200197
func BenchmarkEVM_CREATE_500(bench *testing.B) {

crypto/blake2b/blake2b_test.go

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

304304
data := make([]byte, size)
305305
b.SetBytes(int64(size))
306-
b.ResetTimer()
307-
for i := 0; i < b.N; i++ {
306+
for b.Loop() {
308307
Sum512(data)
309308
}
310309
}
@@ -319,8 +318,7 @@ func benchmarkWrite(b *testing.B, size int, sse4, avx, avx2 bool) {
319318
data := make([]byte, size)
320319
h, _ := New512(nil)
321320
b.SetBytes(int64(size))
322-
b.ResetTimer()
323-
for i := 0; i < b.N; i++ {
321+
for b.Loop() {
324322
h.Write(data)
325323
}
326324
}

crypto/crypto_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func TestToECDSAErrors(t *testing.T) {
5959

6060
func BenchmarkSha3(b *testing.B) {
6161
a := []byte("hello world")
62-
for i := 0; i < b.N; i++ {
62+
for b.Loop() {
6363
Keccak256(a)
6464
}
6565
}

crypto/ecies/ecies_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func TestTooBigSharedKey(t *testing.T) {
164164

165165
// Benchmark the generation of P256 keys.
166166
func BenchmarkGenerateKeyP256(b *testing.B) {
167-
for i := 0; i < b.N; i++ {
167+
for b.Loop() {
168168
if _, err := GenerateKey(rand.Reader, elliptic.P256(), nil); err != nil {
169169
b.Fatal(err)
170170
}
@@ -177,8 +177,7 @@ func BenchmarkGenSharedKeyP256(b *testing.B) {
177177
if err != nil {
178178
b.Fatal(err)
179179
}
180-
b.ResetTimer()
181-
for i := 0; i < b.N; i++ {
180+
for b.Loop() {
182181
_, err := prv.GenerateShared(&prv.PublicKey, 16, 16)
183182
if err != nil {
184183
b.Fatal(err)
@@ -192,8 +191,7 @@ func BenchmarkGenSharedKeyS256(b *testing.B) {
192191
if err != nil {
193192
b.Fatal(err)
194193
}
195-
b.ResetTimer()
196-
for i := 0; i < b.N; i++ {
194+
for b.Loop() {
197195
_, err := prv.GenerateShared(&prv.PublicKey, 16, 16)
198196
if err != nil {
199197
b.Fatal(err)

0 commit comments

Comments
 (0)