Skip to content

Commit a499a11

Browse files
authored
crypto: using testing.B.Loop (#32645)
before: go test -run=^$ -bench=. ./crypto/... 94.83s user 2.68s system 138% cpu 1:10.55 tota after: go test -run=^$ -bench=. ./crypto/... 75.43s user 2.58s system 123% cpu 1:03.01 total
1 parent e35c628 commit a499a11

File tree

6 files changed

+18
-31
lines changed

6 files changed

+18
-31
lines changed

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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func TestToECDSAErrors(t *testing.T) {
6060

6161
func BenchmarkSha3(b *testing.B) {
6262
a := []byte("hello world")
63-
for i := 0; i < b.N; i++ {
63+
for b.Loop() {
6464
Keccak256(a)
6565
}
6666
}
@@ -310,7 +310,7 @@ func BenchmarkKeccak256Hash(b *testing.B) {
310310
rand.Read(input[:])
311311

312312
b.ReportAllocs()
313-
for i := 0; i < b.N; i++ {
313+
for b.Loop() {
314314
Keccak256Hash(input[:])
315315
}
316316
}
@@ -329,7 +329,7 @@ func BenchmarkHashData(b *testing.B) {
329329
rand.Read(input[:])
330330

331331
b.ReportAllocs()
332-
for i := 0; i < b.N; i++ {
332+
for b.Loop() {
333333
HashData(buffer, input[:])
334334
}
335335
}

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)

crypto/kzg4844/kzg4844_test.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ func benchmarkBlobToCommitment(b *testing.B, ckzg bool) {
105105

106106
blob := randBlob()
107107

108-
b.ResetTimer()
109-
for i := 0; i < b.N; i++ {
108+
for b.Loop() {
110109
BlobToCommitment(blob)
111110
}
112111
}
@@ -125,8 +124,7 @@ func benchmarkComputeProof(b *testing.B, ckzg bool) {
125124
point = randFieldElement()
126125
)
127126

128-
b.ResetTimer()
129-
for i := 0; i < b.N; i++ {
127+
for b.Loop() {
130128
ComputeProof(blob, point)
131129
}
132130
}
@@ -147,8 +145,7 @@ func benchmarkVerifyProof(b *testing.B, ckzg bool) {
147145
proof, claim, _ = ComputeProof(blob, point)
148146
)
149147

150-
b.ResetTimer()
151-
for i := 0; i < b.N; i++ {
148+
for b.Loop() {
152149
VerifyProof(commitment, point, claim, proof)
153150
}
154151
}
@@ -167,8 +164,7 @@ func benchmarkComputeBlobProof(b *testing.B, ckzg bool) {
167164
commitment, _ = BlobToCommitment(blob)
168165
)
169166

170-
b.ResetTimer()
171-
for i := 0; i < b.N; i++ {
167+
for b.Loop() {
172168
ComputeBlobProof(blob, commitment)
173169
}
174170
}
@@ -188,8 +184,7 @@ func benchmarkVerifyBlobProof(b *testing.B, ckzg bool) {
188184
proof, _ = ComputeBlobProof(blob, commitment)
189185
)
190186

191-
b.ResetTimer()
192-
for i := 0; i < b.N; i++ {
187+
for b.Loop() {
193188
VerifyBlobProof(blob, commitment, proof)
194189
}
195190
}

crypto/secp256k1/secp256_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,7 @@ func TestRecoverSanity(t *testing.T) {
221221
func BenchmarkSign(b *testing.B) {
222222
_, seckey := generateKeyPair()
223223
msg := csprngEntropy(32)
224-
b.ResetTimer()
225-
226-
for i := 0; i < b.N; i++ {
224+
for b.Loop() {
227225
Sign(msg, seckey)
228226
}
229227
}
@@ -232,9 +230,7 @@ func BenchmarkRecover(b *testing.B) {
232230
msg := csprngEntropy(32)
233231
_, seckey := generateKeyPair()
234232
sig, _ := Sign(msg, seckey)
235-
b.ResetTimer()
236-
237-
for i := 0; i < b.N; i++ {
233+
for b.Loop() {
238234
RecoverPubkey(msg, sig)
239235
}
240236
}

crypto/signature_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func TestPubkeyRandom(t *testing.T) {
135135
}
136136

137137
func BenchmarkEcrecoverSignature(b *testing.B) {
138-
for i := 0; i < b.N; i++ {
138+
for b.Loop() {
139139
if _, err := Ecrecover(testmsg, testsig); err != nil {
140140
b.Fatal("ecrecover error", err)
141141
}
@@ -144,15 +144,15 @@ func BenchmarkEcrecoverSignature(b *testing.B) {
144144

145145
func BenchmarkVerifySignature(b *testing.B) {
146146
sig := testsig[:len(testsig)-1] // remove recovery id
147-
for i := 0; i < b.N; i++ {
147+
for b.Loop() {
148148
if !VerifySignature(testpubkey, testmsg, sig) {
149149
b.Fatal("verify error")
150150
}
151151
}
152152
}
153153

154154
func BenchmarkDecompressPubkey(b *testing.B) {
155-
for i := 0; i < b.N; i++ {
155+
for b.Loop() {
156156
if _, err := DecompressPubkey(testpubkeyc); err != nil {
157157
b.Fatal(err)
158158
}

0 commit comments

Comments
 (0)