@@ -19,36 +19,21 @@ package compression
1919import (
2020 "bytes"
2121 "compress/gzip"
22- "crypto/md5 "
22+ "context "
2323 "io"
2424 "io/ioutil"
2525 "math/rand"
26- "net/http"
2726 "os"
2827 "os/exec"
2928 "path/filepath"
3029 "runtime"
30+ "strings"
3131 "testing"
3232)
3333
34- const benchmarkTestDataURL = "https://git.io/fADcl"
35-
36- var benchmarkTestData []byte
37-
3834func TestMain (m * testing.M ) {
39- // Download test data for benchmark from gist
40- resp , err := http .Get (benchmarkTestDataURL )
41- if err != nil {
42- panic (err )
43- }
44-
45- defer resp .Body .Close ()
46-
47- benchmarkTestData , err = ioutil .ReadAll (resp .Body )
48- if err != nil {
49- panic (err )
50- }
51-
35+ // Force initPigz to be called, so tests start with the same initial state
36+ gzipDecompress (context .Background (), strings .NewReader ("" ))
5237 os .Exit (m .Run ())
5338}
5439
@@ -203,71 +188,3 @@ func TestCmdStreamBad(t *testing.T) {
203188 t .Fatalf ("wrong output: %s" , string (buf ))
204189 }
205190}
206-
207- func generateCompressedData (b * testing.B , sizeInMb int ) []byte {
208- sizeInBytes := sizeInMb * 1024 * 1024
209- data := benchmarkTestData
210-
211- for len (data ) < sizeInBytes {
212- data = append (data , data ... )
213- }
214-
215- b .SetBytes (int64 (len (data )))
216-
217- var buf bytes.Buffer
218- compressor , err := CompressStream (& buf , Gzip )
219- if err != nil {
220- b .Fatal (err )
221- }
222-
223- if n , err := compressor .Write (data ); err != nil || n != len (data ) {
224- b .Fatal (err )
225- }
226-
227- compressor .Close ()
228- return buf .Bytes ()
229- }
230-
231- func benchmarkDecompression (sizeInMb int ) func (* testing.B ) {
232- buf := make ([]byte , 32 * 1024 )
233- return func (b * testing.B ) {
234- compressed := generateCompressedData (b , sizeInMb )
235- hash := md5 .New ()
236-
237- b .ResetTimer ()
238- for n := 0 ; n < b .N ; n ++ {
239- decompressor , err := DecompressStream (bytes .NewReader (compressed ))
240- if err != nil {
241- b .Fatal (err )
242- }
243-
244- if _ , err = io .CopyBuffer (hash , decompressor , buf ); err != nil {
245- b .Fatal (err )
246- }
247-
248- decompressor .Close ()
249- }
250- }
251- }
252-
253- func BenchmarkGzipDecompression (b * testing.B ) {
254- oldUnpigzPath := unpigzPath
255- unpigzPath = ""
256- defer func () { unpigzPath = oldUnpigzPath }()
257-
258- b .Run ("gzip-32mb" , benchmarkDecompression (32 ))
259- b .Run ("gzip-64mb" , benchmarkDecompression (64 ))
260- b .Run ("gzip-128mb" , benchmarkDecompression (128 ))
261- b .Run ("gzip-256mb" , benchmarkDecompression (256 ))
262- }
263-
264- func BenchmarkPigzDecompression (b * testing.B ) {
265- if _ , err := exec .LookPath ("unpigz" ); err != nil {
266- b .Skip ("pigz not installed" )
267- }
268-
269- b .Run ("pigz-32mb" , benchmarkDecompression (32 ))
270- b .Run ("pigz-64mb" , benchmarkDecompression (64 ))
271- b .Run ("pigz-128mb" , benchmarkDecompression (128 ))
272- b .Run ("pigz-256mb" , benchmarkDecompression (256 ))
273- }
0 commit comments