Skip to content

Commit d0a045d

Browse files
author
Bryan C. Mills
committed
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 <[email protected]>
1 parent 15b3765 commit d0a045d

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

src/archive/zip/writer_test.go

+17-10
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,11 @@ func testReadFile(t *testing.T, f *File, wt *WriteTest) {
181181
}
182182

183183
func BenchmarkCompressedZipGarbage(b *testing.B) {
184-
b.ReportAllocs()
185-
var buf bytes.Buffer
186184
bigBuf := bytes.Repeat([]byte("a"), 1<<20)
187-
for i := 0; i <= b.N; i++ {
185+
186+
runOnce := func(buf *bytes.Buffer) {
188187
buf.Reset()
189-
zw := NewWriter(&buf)
188+
zw := NewWriter(buf)
190189
for j := 0; j < 3; j++ {
191190
w, _ := zw.CreateHeader(&FileHeader{
192191
Name: "foo",
@@ -195,11 +194,19 @@ func BenchmarkCompressedZipGarbage(b *testing.B) {
195194
w.Write(bigBuf)
196195
}
197196
zw.Close()
198-
if i == 0 {
199-
// Reset the timer after the first time through.
200-
// This effectively discards the very large initial flate setup cost,
201-
// as well as the initialization of bigBuf.
202-
b.ResetTimer()
203-
}
204197
}
198+
199+
b.ReportAllocs()
200+
// Run once and then reset the timer.
201+
// This effectively discards the very large initial flate setup cost,
202+
// as well as the initialization of bigBuf.
203+
runOnce(&bytes.Buffer{})
204+
b.ResetTimer()
205+
206+
b.RunParallel(func(pb *testing.PB) {
207+
var buf bytes.Buffer
208+
for pb.Next() {
209+
runOnce(&buf)
210+
}
211+
})
205212
}

src/archive/zip/zip_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,18 @@ func BenchmarkZip64Test(b *testing.B) {
681681
}
682682
}
683683

684+
func BenchmarkZip64TestSizes(b *testing.B) {
685+
for _, size := range []int64{1 << 12, 1 << 20, 1 << 26} {
686+
b.Run(fmt.Sprint(size), func(b *testing.B) {
687+
b.RunParallel(func(pb *testing.PB) {
688+
for pb.Next() {
689+
testZip64(b, size)
690+
}
691+
})
692+
})
693+
}
694+
}
695+
684696
func TestSuffixSaver(t *testing.T) {
685697
const keep = 10
686698
ss := &suffixSaver{keep: keep}

0 commit comments

Comments
 (0)