-
Notifications
You must be signed in to change notification settings - Fork 250
/
hash_test.go
51 lines (43 loc) · 1.13 KB
/
hash_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// nolint: errcheck,scopelint
package benchmarks
import (
"crypto"
"fmt"
"hash"
"testing"
"github.com/cosmos/iavl"
_ "crypto/sha256"
_ "golang.org/x/crypto/ripemd160" // nolint: staticcheck // need to test ripemd160
_ "golang.org/x/crypto/sha3"
)
func BenchmarkHash(b *testing.B) {
fmt.Printf("%s\n", iavl.GetVersionInfo())
hashers := []struct {
name string
size int
hasher hash.Hash
}{
{"ripemd160", 64, crypto.RIPEMD160.New()},
{"ripemd160", 512, crypto.RIPEMD160.New()},
{"sha2-256", 64, crypto.SHA256.New()},
{"sha2-256", 512, crypto.SHA256.New()},
{"sha3-256", 64, crypto.SHA3_256.New()},
{"sha3-256", 512, crypto.SHA3_256.New()},
}
for _, h := range hashers {
prefix := fmt.Sprintf("%s-%d", h.name, h.size)
b.Run(prefix, func(sub *testing.B) {
benchHasher(sub, h.hasher, h.size)
})
}
}
func benchHasher(b *testing.B, hasher hash.Hash, size int) {
// create all random bytes before to avoid timing this
inputs := randBytes(b.N + size + 1)
for i := 0; i < b.N; i++ {
hasher.Reset()
// grab a slice of size bytes from random string
hasher.Write(inputs[i : i+size])
hasher.Sum(nil)
}
}