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