diff --git a/src/hash/crc32/crc32_test.go b/src/hash/crc32/crc32_test.go index 1356734d504..cbd676f82fb 100644 --- a/src/hash/crc32/crc32_test.go +++ b/src/hash/crc32/crc32_test.go @@ -5,6 +5,7 @@ package crc32 import ( + "fmt" "hash" "math/rand" "testing" @@ -196,68 +197,28 @@ func TestGolden(t *testing.T) { } } -func BenchmarkIEEECrc40B(b *testing.B) { - benchmark(b, NewIEEE(), 40, 0) +func BenchmarkCRC32(b *testing.B) { + b.Run("poly=IEEE", benchmarkAll(NewIEEE())) + b.Run("poly=Castagnoli", benchmarkAll(New(MakeTable(Castagnoli)))) + b.Run("poly=Koopman", benchmarkAll(New(MakeTable(Koopman)))) } -func BenchmarkIEEECrc1KB(b *testing.B) { - benchmark(b, NewIEEE(), 1<<10, 0) -} - -func BenchmarkIEEECrc4KB(b *testing.B) { - benchmark(b, NewIEEE(), 4<<10, 0) -} - -func BenchmarkIEEECrc32KB(b *testing.B) { - benchmark(b, NewIEEE(), 32<<10, 0) -} - -func BenchmarkCastagnoliCrc15B(b *testing.B) { - benchmark(b, New(MakeTable(Castagnoli)), 15, 0) -} - -func BenchmarkCastagnoliCrc15BMisaligned(b *testing.B) { - benchmark(b, New(MakeTable(Castagnoli)), 15, 1) -} - -func BenchmarkCastagnoliCrc40B(b *testing.B) { - benchmark(b, New(MakeTable(Castagnoli)), 40, 0) -} - -func BenchmarkCastagnoliCrc40BMisaligned(b *testing.B) { - benchmark(b, New(MakeTable(Castagnoli)), 40, 1) -} - -func BenchmarkCastagnoliCrc512(b *testing.B) { - benchmark(b, New(MakeTable(Castagnoli)), 512, 0) -} - -func BenchmarkCastagnoliCrc512Misaligned(b *testing.B) { - benchmark(b, New(MakeTable(Castagnoli)), 512, 1) -} - -func BenchmarkCastagnoliCrc1KB(b *testing.B) { - benchmark(b, New(MakeTable(Castagnoli)), 1<<10, 0) -} - -func BenchmarkCastagnoliCrc1KBMisaligned(b *testing.B) { - benchmark(b, New(MakeTable(Castagnoli)), 1<<10, 1) -} - -func BenchmarkCastagnoliCrc4KB(b *testing.B) { - benchmark(b, New(MakeTable(Castagnoli)), 4<<10, 0) -} - -func BenchmarkCastagnoliCrc4KBMisaligned(b *testing.B) { - benchmark(b, New(MakeTable(Castagnoli)), 4<<10, 1) -} - -func BenchmarkCastagnoliCrc32KB(b *testing.B) { - benchmark(b, New(MakeTable(Castagnoli)), 32<<10, 0) -} - -func BenchmarkCastagnoliCrc32KBMisaligned(b *testing.B) { - benchmark(b, New(MakeTable(Castagnoli)), 32<<10, 1) +func benchmarkAll(h hash.Hash32) func(b *testing.B) { + return func(b *testing.B) { + for _, size := range []int{15, 40, 512, 1 << 10, 4 << 10, 32 << 10} { + name := fmt.Sprint(size) + if size >= 1024 { + name = fmt.Sprintf("%dkB", size/1024) + } + b.Run("size="+name, func(b *testing.B) { + for align := 0; align <= 1; align++ { + b.Run(fmt.Sprintf("align=%d", align), func(b *testing.B) { + benchmark(b, h, int64(size), int64(align)) + }) + } + }) + } + } } func benchmark(b *testing.B, h hash.Hash32, n, alignment int64) {