Skip to content

Commit

Permalink
Write benchmarks using sub-benchmarks (#20)
Browse files Browse the repository at this point in the history
* Write benchmarks using sub-benchmarks

While I'm at it, also add benchmarks for Sum64.
  • Loading branch information
cespare authored and spaolacci committed Jan 18, 2018
1 parent 9f5d223 commit f09979e
Showing 1 changed file with 56 additions and 136 deletions.
192 changes: 56 additions & 136 deletions murmur_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package murmur3

import (
"fmt"
"strconv"
"testing"
)

Expand Down Expand Up @@ -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)
}
})
}
}

//---

0 comments on commit f09979e

Please sign in to comment.