mirror of
https://github.com/smallnest/rpcx.git
synced 2025-10-21 15:19:26 +08:00
feat (package protocol): add benchmark test
1. add snappy compressor 2. add compressor benchmark
This commit is contained in:
111
protocol/compressor_test.go
Normal file
111
protocol/compressor_test.go
Normal file
@@ -0,0 +1,111 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/smallnest/rpcx/codec"
|
||||
"github.com/smallnest/rpcx/protocol/testdata"
|
||||
)
|
||||
|
||||
func newBenchmarkMessage() *testdata.BenchmarkMessage {
|
||||
var theAnswer = "Answer to the Ultimate Question of Life, the Universe, and Everything"
|
||||
var args testdata.BenchmarkMessage
|
||||
|
||||
v := reflect.ValueOf(&args).Elem()
|
||||
|
||||
for k := 0; k < v.NumField(); k++ {
|
||||
field := v.Field(k)
|
||||
fieldName := v.Type().Field(k).Name
|
||||
// filter unexported fields
|
||||
if fieldName[0] >= 97 && fieldName[0] <= 122 {
|
||||
continue
|
||||
}
|
||||
|
||||
switch field.Kind() {
|
||||
case reflect.Int, reflect.Int32, reflect.Int64:
|
||||
field.SetInt(31415926)
|
||||
case reflect.Bool:
|
||||
field.SetBool(true)
|
||||
case reflect.String:
|
||||
field.SetString(theAnswer)
|
||||
}
|
||||
}
|
||||
|
||||
return &args
|
||||
}
|
||||
|
||||
func BenchmarkGzipCompressor_Zip(b *testing.B) {
|
||||
compressor := GzipCompressor{}
|
||||
serializer := codec.PBCodec{}
|
||||
raw, _ := serializer.Encode(newBenchmarkMessage())
|
||||
zipped := make([]byte, 1024)
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
zipped, _ = compressor.Zip(raw)
|
||||
}
|
||||
b.ReportMetric(float64(len(zipped)), "bytes")
|
||||
}
|
||||
|
||||
func BenchmarkRawDataCompressor_Zip(b *testing.B) {
|
||||
compressor := RawDataCompressor{}
|
||||
serializer := codec.PBCodec{}
|
||||
raw, _ := serializer.Encode(newBenchmarkMessage())
|
||||
zipped := make([]byte, 1024)
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
zipped, _ = compressor.Zip(raw)
|
||||
}
|
||||
b.ReportMetric(float64(len(zipped)), "bytes")
|
||||
}
|
||||
|
||||
func BenchmarkSnappyCompressor_Zip(b *testing.B) {
|
||||
compressor := SnappyCompressor{}
|
||||
serializer := codec.PBCodec{}
|
||||
raw, _ := serializer.Encode(newBenchmarkMessage())
|
||||
zipped := make([]byte, 1024)
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
zipped, _ = compressor.Zip(raw)
|
||||
}
|
||||
b.ReportMetric(float64(len(zipped)), "bytes")
|
||||
}
|
||||
|
||||
func BenchmarkGzipCompressor_Unzip(b *testing.B) {
|
||||
compressor := GzipCompressor{}
|
||||
serializer := codec.PBCodec{}
|
||||
raw, _ := serializer.Encode(newBenchmarkMessage())
|
||||
zipped, _ := compressor.Zip(raw)
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
raw, _ = compressor.Unzip(zipped)
|
||||
}
|
||||
b.ReportMetric(float64(len(raw)), "bytes")
|
||||
}
|
||||
|
||||
func BenchmarkRawDataCompressor_Unzip(b *testing.B) {
|
||||
compressor := RawDataCompressor{}
|
||||
serializer := codec.PBCodec{}
|
||||
raw, _ := serializer.Encode(newBenchmarkMessage())
|
||||
zipped, _ := compressor.Zip(raw)
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
raw, _ = compressor.Unzip(zipped)
|
||||
}
|
||||
b.ReportMetric(float64(len(raw)), "bytes")
|
||||
}
|
||||
|
||||
func BenchmarkSnappyCompressor_Unzip(b *testing.B) {
|
||||
compressor := SnappyCompressor{}
|
||||
serializer := codec.PBCodec{}
|
||||
raw, _ := serializer.Encode(newBenchmarkMessage())
|
||||
zipped, _ := compressor.Zip(raw)
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
raw, _ = compressor.Unzip(zipped)
|
||||
}
|
||||
b.ReportMetric(float64(len(raw)), "bytes")
|
||||
}
|
Reference in New Issue
Block a user