From e0ea1e3196ac0aac60eccd7670662049875d8b7b Mon Sep 17 00:00:00 2001 From: Caleb Spare Date: Thu, 19 Nov 2020 18:07:25 -0800 Subject: [PATCH] Rewrite benchmarks not to use indirect calls Move the important benchmarks back into the xxhash package itself. Updates #22 --- bench_test.go | 74 +++++++++++++++++++++++++++++++++ xxhashbench/xxhashbench_test.go | 4 ++ 2 files changed, 78 insertions(+) create mode 100644 bench_test.go diff --git a/bench_test.go b/bench_test.go new file mode 100644 index 0000000..4dfeb91 --- /dev/null +++ b/bench_test.go @@ -0,0 +1,74 @@ +package xxhash + +import ( + "strings" + "testing" +) + +var benchmarks = []struct { + name string + n int64 +}{ + {"4B", 4}, + {"100B", 100}, + {"4KB", 4e3}, + {"10MB", 10e6}, +} + +func BenchmarkSum64(b *testing.B) { + for _, bb := range benchmarks { + in := make([]byte, bb.n) + for i := range in { + in[i] = byte(i) + } + b.Run(bb.name, func(b *testing.B) { + b.SetBytes(bb.n) + for i := 0; i < b.N; i++ { + _ = Sum64(in) + } + }) + } +} + +func BenchmarkSum64String(b *testing.B) { + for _, bb := range benchmarks { + s := strings.Repeat("a", int(bb.n)) + b.Run(bb.name, func(b *testing.B) { + b.SetBytes(bb.n) + for i := 0; i < b.N; i++ { + _ = Sum64String(s) + } + }) + } +} + +func BenchmarkDigestBytes(b *testing.B) { + for _, bb := range benchmarks { + in := make([]byte, bb.n) + for i := range in { + in[i] = byte(i) + } + b.Run(bb.name, func(b *testing.B) { + b.SetBytes(bb.n) + for i := 0; i < b.N; i++ { + h := New() + h.Write(in) + _ = h.Sum64() + } + }) + } +} + +func BenchmarkDigestString(b *testing.B) { + for _, bb := range benchmarks { + s := strings.Repeat("a", int(bb.n)) + b.Run(bb.name, func(b *testing.B) { + b.SetBytes(bb.n) + for i := 0; i < b.N; i++ { + h := New() + h.WriteString(s) + _ = h.Sum64() + } + }) + } +} diff --git a/xxhashbench/xxhashbench_test.go b/xxhashbench/xxhashbench_test.go index a73f69e..72e3398 100644 --- a/xxhashbench/xxhashbench_test.go +++ b/xxhashbench/xxhashbench_test.go @@ -11,6 +11,10 @@ import ( "github.com/spaolacci/murmur3" ) +// TODO: The main benchmarks live in the xxhash package now, so the only purpose +// of this is to compare different hash functions. Consider deleting xxhashbench +// or replacing it with a more minimal comparison. + var sink uint64 var benchmarks = []struct {