From d0a045daaf022515ddf518cdded04f672210f8c4 Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Fri, 10 Feb 2017 12:46:14 -0500 Subject: [PATCH] archive/zip: parallelize benchmarks Add subbenchmarks for BenchmarkZip64Test with different sizes to tease apart construction costs vs. steady-state throughput. Results remain comparable with the non-parallel version with -cpu=1: benchmark old ns/op new ns/op delta BenchmarkCompressedZipGarbage 26832835 27506953 +2.51% BenchmarkCompressedZipGarbage-6 27172377 4321534 -84.10% BenchmarkZip64Test 196758732 197765510 +0.51% BenchmarkZip64Test-6 193850605 192625458 -0.63% benchmark old allocs new allocs delta BenchmarkCompressedZipGarbage 44 44 +0.00% BenchmarkCompressedZipGarbage-6 44 44 +0.00% benchmark old bytes new bytes delta BenchmarkCompressedZipGarbage 5592 5664 +1.29% BenchmarkCompressedZipGarbage-6 5592 21946 +292.45% updates #18177 Change-Id: Icfa359d9b1a8df5e085dacc07d2b9221b284764c Reviewed-on: https://go-review.googlesource.com/36719 Reviewed-by: Brad Fitzpatrick --- src/archive/zip/writer_test.go | 27 +++++++++++++++++---------- src/archive/zip/zip_test.go | 12 ++++++++++++ 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/archive/zip/writer_test.go b/src/archive/zip/writer_test.go index 86841c755f..84b1d9e062 100644 --- a/src/archive/zip/writer_test.go +++ b/src/archive/zip/writer_test.go @@ -181,12 +181,11 @@ func testReadFile(t *testing.T, f *File, wt *WriteTest) { } func BenchmarkCompressedZipGarbage(b *testing.B) { - b.ReportAllocs() - var buf bytes.Buffer bigBuf := bytes.Repeat([]byte("a"), 1<<20) - for i := 0; i <= b.N; i++ { + + runOnce := func(buf *bytes.Buffer) { buf.Reset() - zw := NewWriter(&buf) + zw := NewWriter(buf) for j := 0; j < 3; j++ { w, _ := zw.CreateHeader(&FileHeader{ Name: "foo", @@ -195,11 +194,19 @@ func BenchmarkCompressedZipGarbage(b *testing.B) { w.Write(bigBuf) } zw.Close() - if i == 0 { - // Reset the timer after the first time through. - // This effectively discards the very large initial flate setup cost, - // as well as the initialization of bigBuf. - b.ResetTimer() - } } + + b.ReportAllocs() + // Run once and then reset the timer. + // This effectively discards the very large initial flate setup cost, + // as well as the initialization of bigBuf. + runOnce(&bytes.Buffer{}) + b.ResetTimer() + + b.RunParallel(func(pb *testing.PB) { + var buf bytes.Buffer + for pb.Next() { + runOnce(&buf) + } + }) } diff --git a/src/archive/zip/zip_test.go b/src/archive/zip/zip_test.go index 72d2b00089..18c2171ba6 100644 --- a/src/archive/zip/zip_test.go +++ b/src/archive/zip/zip_test.go @@ -681,6 +681,18 @@ func BenchmarkZip64Test(b *testing.B) { } } +func BenchmarkZip64TestSizes(b *testing.B) { + for _, size := range []int64{1 << 12, 1 << 20, 1 << 26} { + b.Run(fmt.Sprint(size), func(b *testing.B) { + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + testZip64(b, size) + } + }) + }) + } +} + func TestSuffixSaver(t *testing.T) { const keep = 10 ss := &suffixSaver{keep: keep} -- 2.48.1