From 810691809711abd05ff5fb3a7d974f7981c5ec86 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Tue, 2 Jan 2018 18:10:20 +0100 Subject: [PATCH] Add benchmarks for hash functions --- benchmarks/hash_test.go | 47 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 benchmarks/hash_test.go diff --git a/benchmarks/hash_test.go b/benchmarks/hash_test.go new file mode 100644 index 000000000..83491a9f7 --- /dev/null +++ b/benchmarks/hash_test.go @@ -0,0 +1,47 @@ +package benchmarks + +import ( + "crypto" + "fmt" + "hash" + "testing" + + _ "crypto/sha256" + + _ "golang.org/x/crypto/ripemd160" + _ "golang.org/x/crypto/sha3" +) + +func BenchmarkHash(b *testing.B) { + 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) + } +}