Skip to content

Commit e8fac24

Browse files
committed
Remove decompression benchmark, use sync.Once for initialization
Benchmark gist: https://git.io/fASMy Signed-off-by: Maksym Pavlenko <[email protected]>
1 parent 003b27e commit e8fac24

2 files changed

Lines changed: 14 additions & 94 deletions

File tree

archive/compression/compression.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,10 @@ const (
4545

4646
const disablePigzEnv = "CONTAINERD_DISABLE_PIGZ"
4747

48-
var unpigzPath string
49-
50-
func init() {
51-
if unpigzPath = detectPigz(); unpigzPath != "" {
52-
log.L.Debug("using pigz for decompression")
53-
}
54-
}
48+
var (
49+
initPigz sync.Once
50+
unpigzPath string
51+
)
5552

5653
var (
5754
bufioReader32KPool = &sync.Pool{
@@ -176,6 +173,12 @@ func (compression *Compression) Extension() string {
176173
}
177174

178175
func gzipDecompress(ctx context.Context, buf io.Reader) (io.ReadCloser, error) {
176+
initPigz.Do(func() {
177+
if unpigzPath = detectPigz(); unpigzPath != "" {
178+
log.L.Debug("using pigz for decompression")
179+
}
180+
})
181+
179182
if unpigzPath == "" {
180183
return gzip.NewReader(buf)
181184
}

archive/compression/compression_test.go

Lines changed: 4 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,21 @@ package compression
1919
import (
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-
3834
func 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

Comments
 (0)