Created
April 16, 2020 05:16
-
-
Save abihf/b14c76346f129ee426ea267661c4abbc to your computer and use it in GitHub Desktop.
GZip vs Brotli
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"compress/gzip" | |
"fmt" | |
"io" | |
"io/ioutil" | |
"os" | |
"testing" | |
"github.com/andybalholm/brotli" | |
"github.com/google/brotli/go/cbrotli" | |
"github.com/youtube/vitess/go/cgzip" | |
) | |
func BenchmarkCompression(t *testing.B) { | |
file, _ := os.Open("data.json") | |
data, _ := ioutil.ReadAll(file) | |
for q := 3; q <= 4; q++ { | |
for lgw := 18; lgw <= 22; lgw++ { | |
t.Run(fmt.Sprintf("brotli Q%d-LLWin%d", q, lgw), func(b *testing.B) { | |
compress(b, data, func(out io.Writer) io.WriteCloser { | |
return brotli.NewWriterOptions(out, brotli.WriterOptions{Quality: q, LGWin: lgw}) | |
}) | |
}) | |
} | |
} | |
for q := 3; q <= 4; q++ { | |
for lgw := 18; lgw <= 22; lgw++ { | |
t.Run(fmt.Sprintf("cbrotli Q%d-LLWin%d", q, lgw), func(b *testing.B) { | |
compress(b, data, func(out io.Writer) io.WriteCloser { | |
return cbrotli.NewWriter(out, cbrotli.WriterOptions{Quality: q, LGWin: lgw}) | |
}) | |
}) | |
} | |
} | |
t.Run("gzip DefaultCompression", func(b *testing.B) { | |
compress(b, data, func(out io.Writer) io.WriteCloser { | |
w, _ := gzip.NewWriterLevel(out, gzip.DefaultCompression) | |
return w | |
}) | |
}) | |
t.Run("gzip BestSpeed", func(b *testing.B) { | |
compress(b, data, func(out io.Writer) io.WriteCloser { | |
w, _ := gzip.NewWriterLevel(out, gzip.BestSpeed) | |
return w | |
}) | |
}) | |
t.Run("gzip BestCompression", func(b *testing.B) { | |
compress(b, data, func(out io.Writer) io.WriteCloser { | |
w, _ := gzip.NewWriterLevel(out, gzip.BestCompression) | |
return w | |
}) | |
}) | |
t.Run("cgzip DefaultCompression", func(b *testing.B) { | |
compress(b, data, func(out io.Writer) io.WriteCloser { | |
w, _ := cgzip.NewWriterLevel(out, cgzip.Z_DEFAULT_COMPRESSION) | |
return w | |
}) | |
}) | |
t.Run("cgzip BestSpeed", func(b *testing.B) { | |
compress(b, data, func(out io.Writer) io.WriteCloser { | |
w, _ := cgzip.NewWriterLevel(out, cgzip.Z_BEST_SPEED) | |
return w | |
}) | |
}) | |
t.Run("cgzip BestCompression", func(b *testing.B) { | |
compress(b, data, func(out io.Writer) io.WriteCloser { | |
w, _ := cgzip.NewWriterLevel(out, cgzip.Z_BEST_COMPRESSION) | |
return w | |
}) | |
}) | |
} | |
type compressorFactory func(out io.Writer) io.WriteCloser | |
func compress(b *testing.B, data []byte, createCompressor compressorFactory) { | |
maxSize := 0 | |
b.ReportAllocs() | |
for i := 0; i < b.N; i++ { | |
buffer := &bytes.Buffer{} | |
w := createCompressor(buffer) | |
w.Write([]byte(data)) | |
w.Close() | |
size := buffer.Len() | |
if size > maxSize { | |
maxSize = size | |
} | |
} | |
b.ReportMetric(float64(maxSize), "byte") | |
b.ReportMetric(float64(maxSize)/float64(len(data)), "ratio") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment