-
-
Save klauspost/446e9ab7aeae0b75d7974339b65df815 to your computer and use it in GitHub Desktop.
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 compress | |
import ( | |
"bytes" | |
"io" | |
"io/ioutil" | |
"math/rand" | |
"net/http" | |
"testing" | |
"github.com/valyala/gozstd" | |
"github.com/klauspost/compress/s2" | |
"github.com/klauspost/compress/zstd" | |
) | |
func BenchmarkCZstd(b *testing.B) { | |
var buf bytes.Buffer | |
enc := gozstd.NewWriter(&buf) | |
dec := gozstd.NewReader(&buf) | |
b.SetBytes(int64(len(bin))) | |
b.ReportAllocs() | |
bb := make([]byte, len(bin)) | |
for i := 0; i < b.N; i++ { | |
buf.Reset() | |
enc.Reset(&buf, nil, gozstd.DefaultCompressionLevel) | |
if _, err := enc.Write(bin); err != nil { | |
b.Fatal(err) | |
} | |
if err := enc.Flush(); err != nil { | |
b.Fatal(err) | |
} | |
dec.Reset(&buf, nil) | |
_, err := io.ReadFull(dec, bb) | |
if err != nil { | |
b.Fatal(err) | |
} | |
if !bytes.Equal(bb, bin) { | |
b.Fatal("not reached") | |
} | |
} | |
} | |
func BenchmarkGoZstd(b *testing.B) { | |
var buf bytes.Buffer | |
enc, err := zstd.NewWriter(nil, | |
zstd.WithEncoderCRC(false)) | |
if err != nil { | |
b.Fatal(err) | |
} | |
dec, err := zstd.NewReader(nil) | |
if err != nil { | |
b.Fatal(err) | |
} | |
bb := make([]byte, len(bin)) | |
b.SetBytes(int64(len(bin))) | |
b.ResetTimer() | |
b.ReportAllocs() | |
for i := 0; i < b.N; i++ { | |
buf.Reset() | |
enc.Reset(&buf) | |
if _, err := enc.Write(bin); err != nil { | |
b.Fatal(err) | |
} | |
if err := enc.Close(); err != nil { | |
b.Fatal(err) | |
} | |
if err := dec.Reset(&buf); err != nil { | |
b.Fatal(err) | |
} | |
_, err := io.ReadFull(dec, bb) | |
if err != nil { | |
b.Fatal(err) | |
} | |
if !bytes.Equal(bb, bin) { | |
b.Fatal("not reached") | |
} | |
} | |
} | |
func BenchmarkGoZstd2(b *testing.B) { | |
enc, err := zstd.NewWriter(nil, | |
zstd.WithEncoderCRC(false)) | |
if err != nil { | |
b.Fatal(err) | |
} | |
dec, err := zstd.NewReader(nil) | |
if err != nil { | |
b.Fatal(err) | |
} | |
encoded := enc.EncodeAll(bin, nil) | |
decoded, err := dec.DecodeAll(encoded, nil) | |
b.SetBytes(int64(len(bin))) | |
b.ResetTimer() | |
b.ReportAllocs() | |
for i := 0; i < b.N; i++ { | |
encoded = enc.EncodeAll(bin, encoded[:0]) | |
decoded, err = dec.DecodeAll(encoded, decoded[:0]) | |
if err != nil { | |
b.Fatal(err) | |
} | |
if !bytes.Equal(decoded, bin) { | |
b.Fatal("not reached") | |
} | |
} | |
} | |
func BenchmarkS2(b *testing.B) { | |
var buf bytes.Buffer | |
enc := s2.NewWriter(nil) | |
dec := s2.NewReader(nil) | |
b.SetBytes(int64(len(bin))) | |
b.ResetTimer() | |
b.ReportAllocs() | |
bb := make([]byte, len(bin)) | |
for i := 0; i < b.N; i++ { | |
buf.Reset() | |
enc.Reset(&buf) | |
if err := enc.EncodeBuffer(bin); err != nil { | |
b.Fatal(err) | |
} | |
if err := enc.Close(); err != nil { | |
b.Fatal(err) | |
} | |
dec.Reset(&buf) | |
_, err := io.ReadFull(dec, bb) | |
if err != nil { | |
b.Fatal(err) | |
} | |
if !bytes.Equal(bb, bin) { | |
b.Fatal("not reached") | |
} | |
} | |
} | |
var bin []byte | |
func init() { | |
resp, err := http.Get("https://golang.org/pkg/net/http/") | |
if err != nil { | |
panic(err) | |
} | |
defer resp.Body.Close() | |
b, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
panic(err) | |
} | |
bin = b | |
//bin, err = ioutil.ReadFile("enwik9") | |
if err != nil { | |
panic(err) | |
} | |
} | |
func writeChunked(w io.Writer, b []byte) { | |
for len(b) > 0 { | |
n := rand.Intn(min(len(b)+1, 5)) | |
if n == 0 { | |
continue | |
} | |
if _, err := w.Write(b[:n]); err != nil { | |
panic(err) | |
} | |
b = b[n:] | |
} | |
} | |
func min(a, b int) int { | |
if a <= b { | |
return a | |
} | |
return b | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment