From f09979ecbc725b9e6d41a297405f65e7e8804acc Mon Sep 17 00:00:00 2001 From: Caleb Spare Date: Thu, 18 Jan 2018 12:28:30 -0800 Subject: [PATCH] Write benchmarks using sub-benchmarks (#20) * Write benchmarks using sub-benchmarks While I'm at it, also add benchmarks for Sum64. --- murmur_test.go | 192 +++++++++++++++---------------------------------- 1 file changed, 56 insertions(+), 136 deletions(-) diff --git a/murmur_test.go b/murmur_test.go index fe564d0..94e2ee1 100644 --- a/murmur_test.go +++ b/murmur_test.go @@ -2,6 +2,7 @@ package murmur3 import ( "fmt" + "strconv" "testing" ) @@ -113,153 +114,72 @@ func TestIncremental(t *testing.T) { } } -//--- - -func bench32(b *testing.B, length int) { - buf := make([]byte, length) - b.SetBytes(int64(length)) - b.ResetTimer() - for i := 0; i < b.N; i++ { - Sum32(buf) +func Benchmark32(b *testing.B) { + buf := make([]byte, 8192) + for length := 1; length <= cap(buf); length *= 2 { + b.Run(strconv.Itoa(length), func(b *testing.B) { + buf = buf[:length] + b.SetBytes(int64(length)) + b.ResetTimer() + for i := 0; i < b.N; i++ { + Sum32(buf) + } + }) } } -func Benchmark32_1(b *testing.B) { - bench32(b, 1) -} -func Benchmark32_2(b *testing.B) { - bench32(b, 2) -} -func Benchmark32_4(b *testing.B) { - bench32(b, 4) -} -func Benchmark32_8(b *testing.B) { - bench32(b, 8) -} -func Benchmark32_16(b *testing.B) { - bench32(b, 16) -} -func Benchmark32_32(b *testing.B) { - bench32(b, 32) -} -func Benchmark32_64(b *testing.B) { - bench32(b, 64) -} -func Benchmark32_128(b *testing.B) { - bench32(b, 128) -} -func Benchmark32_256(b *testing.B) { - bench32(b, 256) -} -func Benchmark32_512(b *testing.B) { - bench32(b, 512) -} -func Benchmark32_1024(b *testing.B) { - bench32(b, 1024) -} -func Benchmark32_2048(b *testing.B) { - bench32(b, 2048) -} -func Benchmark32_4096(b *testing.B) { - bench32(b, 4096) -} -func Benchmark32_8192(b *testing.B) { - bench32(b, 8192) -} - -//--- +func BenchmarkPartial32(b *testing.B) { + buf := make([]byte, 128) + for length := 8; length <= cap(buf); length *= 2 { + b.Run(strconv.Itoa(length), func(b *testing.B) { + buf = buf[:length] + b.SetBytes(int64(length)) -func benchPartial32(b *testing.B, length int) { - buf := make([]byte, length) - b.SetBytes(int64(length)) + start := (32 / 8) / 2 + chunks := 7 + k := length / chunks + tail := (length - start) % k - start := (32 / 8) / 2 - chunks := 7 - k := length / chunks - tail := (length - start) % k + b.ResetTimer() + for i := 0; i < b.N; i++ { + hasher := New32() + hasher.Write(buf[0:start]) - b.ResetTimer() - for i := 0; i < b.N; i++ { - hasher := New32() - hasher.Write(buf[0:start]) + for j := start; j+k <= length; j += k { + hasher.Write(buf[j : j+k]) + } - for j := start; j+k <= length; j += k { - hasher.Write(buf[j : j+k]) - } - - hasher.Write(buf[length-tail:]) - hasher.Sum32() + hasher.Write(buf[length-tail:]) + hasher.Sum32() + } + }) } } -func BenchmarkPartial32_8(b *testing.B) { - benchPartial32(b, 8) -} -func BenchmarkPartial32_16(b *testing.B) { - benchPartial32(b, 16) -} -func BenchmarkPartial32_32(b *testing.B) { - benchPartial32(b, 32) -} -func BenchmarkPartial32_64(b *testing.B) { - benchPartial32(b, 64) -} -func BenchmarkPartial32_128(b *testing.B) { - benchPartial32(b, 128) -} - -//--- - -func bench128(b *testing.B, length int) { - buf := make([]byte, length) - b.SetBytes(int64(length)) - b.ResetTimer() - for i := 0; i < b.N; i++ { - Sum128(buf) +func Benchmark64(b *testing.B) { + buf := make([]byte, 8192) + for length := 1; length <= cap(buf); length *= 2 { + b.Run(strconv.Itoa(length), func(b *testing.B) { + buf = buf[:length] + b.SetBytes(int64(length)) + b.ResetTimer() + for i := 0; i < b.N; i++ { + Sum64(buf) + } + }) } } -func Benchmark128_1(b *testing.B) { - bench128(b, 1) -} -func Benchmark128_2(b *testing.B) { - bench128(b, 2) -} -func Benchmark128_4(b *testing.B) { - bench128(b, 4) -} -func Benchmark128_8(b *testing.B) { - bench128(b, 8) -} -func Benchmark128_16(b *testing.B) { - bench128(b, 16) -} -func Benchmark128_32(b *testing.B) { - bench128(b, 32) -} -func Benchmark128_64(b *testing.B) { - bench128(b, 64) -} -func Benchmark128_128(b *testing.B) { - bench128(b, 128) -} -func Benchmark128_256(b *testing.B) { - bench128(b, 256) -} -func Benchmark128_512(b *testing.B) { - bench128(b, 512) -} -func Benchmark128_1024(b *testing.B) { - bench128(b, 1024) -} -func Benchmark128_2048(b *testing.B) { - bench128(b, 2048) -} -func Benchmark128_4096(b *testing.B) { - bench128(b, 4096) -} -func Benchmark128_8192(b *testing.B) { - bench128(b, 8192) +func Benchmark128(b *testing.B) { + buf := make([]byte, 8192) + for length := 1; length <= cap(buf); length *= 2 { + b.Run(strconv.Itoa(length), func(b *testing.B) { + buf = buf[:length] + b.SetBytes(int64(length)) + b.ResetTimer() + for i := 0; i < b.N; i++ { + Sum128(buf) + } + }) + } } - -//---