From: Joe Tsai Date: Wed, 9 Dec 2015 02:26:22 +0000 (-0800) Subject: compress/bzip2: fix benchmark to actually measure decompression rate X-Git-Tag: go1.7beta1~1654 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=ff274210674e407ab7c44585a097259cd029dced;p=gostls13.git compress/bzip2: fix benchmark to actually measure decompression rate Motivation: * Previously, the size of the compressed data was used for metrics, rather than the uncompressed size. This causes the library to appear to perform poorly relative to C or other implementation. Switch it to use the uncompressed size so that it matches how decompression benchmarks are usually done (like in compress/flate). This also makes it easier to compare bzip2 rates to other algorithms since they measure performance in this way. * Also, reset the timer after doing initialization work. Change-Id: I32112c2ee8e7391e658c9cf31039f70a689d9b9d Reviewed-on: https://go-review.googlesource.com/17611 Reviewed-by: Brad Fitzpatrick Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- diff --git a/src/compress/bzip2/bzip2_test.go b/src/compress/bzip2/bzip2_test.go index 2a2136df4d..2acf40290c 100644 --- a/src/compress/bzip2/bzip2_test.go +++ b/src/compress/bzip2/bzip2_test.go @@ -194,7 +194,17 @@ func benchmarkDecode(b *testing.B, testfile int) { if err != nil { b.Fatal(err) } - b.SetBytes(int64(len(compressed))) + + // Determine the uncompressed size of testfile. + uncompressedSize, err := io.Copy(ioutil.Discard, NewReader(bytes.NewReader(compressed))) + if err != nil { + b.Fatal(err) + } + + b.SetBytes(uncompressedSize) + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { r := bytes.NewReader(compressed) io.Copy(ioutil.Discard, NewReader(r))