Remove decompression benchmark, use sync.Once for initialization

Benchmark gist: https://git.io/fASMy

Signed-off-by: Maksym Pavlenko <makpav@amazon.com>
This commit is contained in:
Maksym Pavlenko 2018-09-18 17:09:12 -07:00
parent 003b27eff4
commit e8fac24e7b
No known key found for this signature in database
GPG Key ID: 4B17AAB3098F26DA
2 changed files with 14 additions and 94 deletions

View File

@ -45,13 +45,10 @@ const (
const disablePigzEnv = "CONTAINERD_DISABLE_PIGZ" const disablePigzEnv = "CONTAINERD_DISABLE_PIGZ"
var unpigzPath string var (
initPigz sync.Once
func init() { unpigzPath string
if unpigzPath = detectPigz(); unpigzPath != "" { )
log.L.Debug("using pigz for decompression")
}
}
var ( var (
bufioReader32KPool = &sync.Pool{ bufioReader32KPool = &sync.Pool{
@ -176,6 +173,12 @@ func (compression *Compression) Extension() string {
} }
func gzipDecompress(ctx context.Context, buf io.Reader) (io.ReadCloser, error) { func gzipDecompress(ctx context.Context, buf io.Reader) (io.ReadCloser, error) {
initPigz.Do(func() {
if unpigzPath = detectPigz(); unpigzPath != "" {
log.L.Debug("using pigz for decompression")
}
})
if unpigzPath == "" { if unpigzPath == "" {
return gzip.NewReader(buf) return gzip.NewReader(buf)
} }

View File

@ -19,36 +19,21 @@ package compression
import ( import (
"bytes" "bytes"
"compress/gzip" "compress/gzip"
"crypto/md5" "context"
"io" "io"
"io/ioutil" "io/ioutil"
"math/rand" "math/rand"
"net/http"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings"
"testing" "testing"
) )
const benchmarkTestDataURL = "https://git.io/fADcl"
var benchmarkTestData []byte
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
// Download test data for benchmark from gist // Force initPigz to be called, so tests start with the same initial state
resp, err := http.Get(benchmarkTestDataURL) gzipDecompress(context.Background(), strings.NewReader(""))
if err != nil {
panic(err)
}
defer resp.Body.Close()
benchmarkTestData, err = ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
os.Exit(m.Run()) os.Exit(m.Run())
} }
@ -203,71 +188,3 @@ func TestCmdStreamBad(t *testing.T) {
t.Fatalf("wrong output: %s", string(buf)) t.Fatalf("wrong output: %s", string(buf))
} }
} }
func generateCompressedData(b *testing.B, sizeInMb int) []byte {
sizeInBytes := sizeInMb * 1024 * 1024
data := benchmarkTestData
for len(data) < sizeInBytes {
data = append(data, data...)
}
b.SetBytes(int64(len(data)))
var buf bytes.Buffer
compressor, err := CompressStream(&buf, Gzip)
if err != nil {
b.Fatal(err)
}
if n, err := compressor.Write(data); err != nil || n != len(data) {
b.Fatal(err)
}
compressor.Close()
return buf.Bytes()
}
func benchmarkDecompression(sizeInMb int) func(*testing.B) {
buf := make([]byte, 32*1024)
return func(b *testing.B) {
compressed := generateCompressedData(b, sizeInMb)
hash := md5.New()
b.ResetTimer()
for n := 0; n < b.N; n++ {
decompressor, err := DecompressStream(bytes.NewReader(compressed))
if err != nil {
b.Fatal(err)
}
if _, err = io.CopyBuffer(hash, decompressor, buf); err != nil {
b.Fatal(err)
}
decompressor.Close()
}
}
}
func BenchmarkGzipDecompression(b *testing.B) {
oldUnpigzPath := unpigzPath
unpigzPath = ""
defer func() { unpigzPath = oldUnpigzPath }()
b.Run("gzip-32mb", benchmarkDecompression(32))
b.Run("gzip-64mb", benchmarkDecompression(64))
b.Run("gzip-128mb", benchmarkDecompression(128))
b.Run("gzip-256mb", benchmarkDecompression(256))
}
func BenchmarkPigzDecompression(b *testing.B) {
if _, err := exec.LookPath("unpigz"); err != nil {
b.Skip("pigz not installed")
}
b.Run("pigz-32mb", benchmarkDecompression(32))
b.Run("pigz-64mb", benchmarkDecompression(64))
b.Run("pigz-128mb", benchmarkDecompression(128))
b.Run("pigz-256mb", benchmarkDecompression(256))
}