Last active
January 5, 2022 06:34
-
-
Save js2854/29528b7619cf3b7949cabf595140d1af to your computer and use it in GitHub Desktop.
compress/uncompress protobuf benchmark test
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/flate" | |
"compress/gzip" | |
"compress/zlib" | |
"io/ioutil" | |
"math/rand" | |
"testing" | |
"time" | |
"github.com/gogo/protobuf/proto" | |
"github.com/klauspost/compress/zstd" | |
"github.com/klauspost/pgzip" | |
"github.com/pierrec/lz4" | |
pb "zvelo.io/msg/msgpb" | |
) | |
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") | |
func BenchmarkProtobufCompress(b *testing.B) { | |
msg := &pb.RequestID{RequestId: randSeq(2048)} | |
var totalBytes int64 | |
for i := 0; i < b.N; i++ { | |
data, _ := proto.Marshal(msg) | |
totalBytes += int64(len(data)) | |
} | |
b.ReportMetric(float64(totalBytes/int64(b.N)), "outlen/op") | |
} | |
func BenchmarkProtobufGzipCompress(b *testing.B) { | |
msg := &pb.RequestID{RequestId: randSeq(2048)} | |
var totalBytes int64 | |
for i := 0; i < b.N; i++ { | |
data, _ := proto.Marshal(msg) | |
encoded := gzipCompress(data) | |
totalBytes += int64(len(encoded)) | |
} | |
b.ReportMetric(float64(totalBytes/int64(b.N)), "outlen/op") | |
} | |
func BenchmarkProtobufZlibCompress(b *testing.B) { | |
msg := &pb.RequestID{RequestId: randSeq(2048)} | |
var totalBytes int64 | |
for i := 0; i < b.N; i++ { | |
data, _ := proto.Marshal(msg) | |
encoded := zlibCompress(data) | |
totalBytes += int64(len(encoded)) | |
} | |
b.ReportMetric(float64(totalBytes/int64(b.N)), "outlen/op") | |
} | |
func BenchmarkProtobufFlateCompress(b *testing.B) { | |
msg := &pb.RequestID{RequestId: randSeq(2048)} | |
var totalBytes int64 | |
for i := 0; i < b.N; i++ { | |
data, _ := proto.Marshal(msg) | |
encoded := flateCompress(data) | |
totalBytes += int64(len(encoded)) | |
} | |
b.ReportMetric(float64(totalBytes/int64(b.N)), "outlen/op") | |
} | |
func BenchmarkProtobufPGzipCompress(b *testing.B) { | |
msg := &pb.RequestID{RequestId: randSeq(2048)} | |
var totalBytes int64 | |
for i := 0; i < b.N; i++ { | |
data, _ := proto.Marshal(msg) | |
encoded := pgzipCompress(data) | |
totalBytes += int64(len(encoded)) | |
} | |
b.ReportMetric(float64(totalBytes/int64(b.N)), "outlen/op") | |
} | |
func BenchmarkProtobufZstdCompress(b *testing.B) { | |
msg := &pb.RequestID{RequestId: randSeq(2048)} | |
var totalBytes int64 | |
for i := 0; i < b.N; i++ { | |
data, _ := proto.Marshal(msg) | |
encoded := zstdCompress(data) | |
totalBytes += int64(len(encoded)) | |
} | |
b.ReportMetric(float64(totalBytes/int64(b.N)), "outlen/op") | |
} | |
func BenchmarkProtobufLZ4Compress(b *testing.B) { | |
msg := &pb.RequestID{RequestId: randSeq(2048)} | |
var totalBytes int64 | |
for i := 0; i < b.N; i++ { | |
data, _ := proto.Marshal(msg) | |
encoded := lz4Compress(data) | |
totalBytes += int64(len(encoded)) | |
} | |
b.ReportMetric(float64(totalBytes/int64(b.N)), "outlen/op") | |
} | |
func BenchmarkProtobufUncompress(b *testing.B) { | |
data, _ := proto.Marshal(&pb.RequestID{RequestId: randSeq(2048)}) | |
for i := 0; i < b.N; i++ { | |
var msg pb.RequestID | |
proto.Unmarshal(data, &msg) | |
} | |
} | |
func BenchmarkProtobufGzipUncompress(b *testing.B) { | |
data, _ := proto.Marshal(&pb.RequestID{RequestId: randSeq(2048)}) | |
encoded := gzipCompress(data) | |
for i := 0; i < b.N; i++ { | |
var msg pb.RequestID | |
proto.Unmarshal(gzipUncompress(encoded), &msg) | |
} | |
} | |
func BenchmarkProtobufZlibUncompress(b *testing.B) { | |
data, _ := proto.Marshal(&pb.RequestID{RequestId: randSeq(2048)}) | |
encoded := zlibCompress(data) | |
for i := 0; i < b.N; i++ { | |
var msg pb.RequestID | |
proto.Unmarshal(zlibUncompress(encoded), &msg) | |
} | |
} | |
func BenchmarkProtobufFlateUncompress(b *testing.B) { | |
data, _ := proto.Marshal(&pb.RequestID{RequestId: randSeq(2048)}) | |
encoded := flateCompress(data) | |
for i := 0; i < b.N; i++ { | |
var msg pb.RequestID | |
proto.Unmarshal(flateUncompress(encoded), &msg) | |
} | |
} | |
func BenchmarkProtobufPGzipUncompress(b *testing.B) { | |
data, _ := proto.Marshal(&pb.RequestID{RequestId: randSeq(2048)}) | |
encoded := pgzipCompress(data) | |
for i := 0; i < b.N; i++ { | |
var msg pb.RequestID | |
proto.Unmarshal(pgzipUncompress(encoded), &msg) | |
} | |
} | |
func BenchmarkProtobufZstdUncompress(b *testing.B) { | |
data, _ := proto.Marshal(&pb.RequestID{RequestId: randSeq(2048)}) | |
encoded := zstdCompress(data) | |
for i := 0; i < b.N; i++ { | |
var msg pb.RequestID | |
proto.Unmarshal(zstdUncompress(encoded), &msg) | |
} | |
} | |
func BenchmarkProtobufLZ4Uncompress(b *testing.B) { | |
data, _ := proto.Marshal(&pb.RequestID{RequestId: randSeq(2048)}) | |
encoded := lz4Compress(data) | |
for i := 0; i < b.N; i++ { | |
var msg pb.RequestID | |
proto.Unmarshal(lz4Uncompress(encoded), &msg) | |
} | |
} | |
func randSeq(n int) string { | |
rand.Seed(time.Now().Unix()) | |
b := make([]rune, n) | |
for i := range b { | |
b[i] = letters[rand.Intn(len(letters))] | |
} | |
return string(b) | |
} | |
func gzipCompress(b []byte) []byte { | |
var buf bytes.Buffer | |
w := gzip.NewWriter(&buf) | |
w.Write(b) | |
w.Close() | |
return buf.Bytes() | |
} | |
func zlibCompress(b []byte) []byte { | |
var buf bytes.Buffer | |
w := zlib.NewWriter(&buf) | |
w.Write(b) | |
w.Close() | |
return buf.Bytes() | |
} | |
func flateCompress(b []byte) []byte { | |
var buf bytes.Buffer | |
w, _ := flate.NewWriter(&buf, 9) | |
w.Write(b) | |
w.Close() | |
return buf.Bytes() | |
} | |
func pgzipCompress(b []byte) []byte { | |
var buf bytes.Buffer | |
w := pgzip.NewWriter(&buf) | |
w.Write(b) | |
w.Close() | |
return buf.Bytes() | |
} | |
func zstdCompress(b []byte) []byte { | |
var buf bytes.Buffer | |
w, _ := zstd.NewWriter(&buf) | |
w.Write(b) | |
w.Close() | |
return buf.Bytes() | |
} | |
func lz4Compress(b []byte) []byte { | |
var buf bytes.Buffer | |
w := lz4.NewWriter(&buf) | |
w.Write(b) | |
w.Close() | |
return buf.Bytes() | |
} | |
func gzipUncompress(b []byte) []byte { | |
r, _ := gzip.NewReader(bytes.NewReader(b)) | |
defer r.Close() | |
data, _ := ioutil.ReadAll(r) | |
return data | |
} | |
func zlibUncompress(b []byte) []byte { | |
r, _ := zlib.NewReader(bytes.NewReader(b)) | |
defer r.Close() | |
data, _ := ioutil.ReadAll(r) | |
return data | |
} | |
func flateUncompress(b []byte) []byte { | |
r := flate.NewReader(bytes.NewReader(b)) | |
defer r.Close() | |
data, _ := ioutil.ReadAll(r) | |
return data | |
} | |
func pgzipUncompress(b []byte) []byte { | |
r, _ := pgzip.NewReader(bytes.NewReader(b)) | |
defer r.Close() | |
data, _ := ioutil.ReadAll(r) | |
return data | |
} | |
func zstdUncompress(b []byte) []byte { | |
r, _ := zstd.NewReader(bytes.NewReader(b)) | |
defer r.Close() | |
data, _ := ioutil.ReadAll(r) | |
return data | |
} | |
func lz4Uncompress(b []byte) []byte { | |
r := lz4.NewReader(bytes.NewReader(b)) | |
data, _ := ioutil.ReadAll(r) | |
return data | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
test result: