|
一、实验性能结果
从性能角度:
序列化:gogo/protobuf的gofast插件性能最好(是json的5倍、golang/protobuf的6倍、gogo/protobuf的gogo插件的8倍)
反序列化:gogo/protobuf的gofast插件性最好(是json的6倍、golang/protobuf的3.3倍、gogo/protobuf的gogo插件的2.5倍)
从内存占用角度:
序列化:gogo/protobuf的gofast插件内存占用最少(是json的1/6、golang/protobuf的1/4、gogo/protobuf的gogo插件的1/7)
反序列化:gogo/protobuf的gofast插内存占用最少(与json相当、golang/protobuf的1/1.5、gogo/protobuf的1/1.2)
从序列化以后的二进制大小角度:
protobuf相比于json要小;json大约是protobuf的1.5倍
二、实验环境:Mac Pro 16C16G1.5T
三、实验数据:
var JudgeItemModel = model.JudgeItem{ Endpoint: "172.12.12.12", Metric: "docker_cpu_util", Value: 70, Timestamp: 1656155443, JudgeType: "JUDGE", Tags: map[string]string{ "key1":"value1", "key2":"value1", "key3":"value1", "key4":"value1", },}var JudgeItemsModel = model.JudgeItems{ JudgeItem: []*model.JudgeItem{ &JudgeItemModel, },}四、测试场景
1、golang自带的json序列化/反序列化(json.Marshal/Unmarshal)
2、golang/protobuf
3、gogo/protobuf提供gogo插件
4、gogo/protobuf提供的gofast插件
BenchMark执行命令:go test -benchtime=5s -bench=. -benchmem -cpu=8
Marshal测试结果
BenchmarkJsonMarshal-8 3753434 1600 ns/op 213.0 marshaledBytes 896 B/op 16 allocs/opBenchmarkGolangProtobufMarshal-8 3284922 1804 ns/op 132.0 marshaledBytes 656 B/op 21 allocs/opBenchmarkGogoProtobufMarshal-8 2809514 2137 ns/op 132.0 marshaledBytes 1040 B/op 37 allocs/opBenchmarkGofastProtobufMarshal-8 19904810 301.9 ns/op 132.0 marshaledBytes 144 B/op 1 allocs/opUnmarshal测试结果
BenchmarkJsonUnmarshal-8 1497835 3970 ns/op 592 B/op 29 allocs/opBenchmarkGolangProtobufUnmarshal-8 3094698 1920 ns/op 816 B/op 31 allocs/opBenchmarkGogoProtobufUnmarshal-8 4156102 1440 ns/op 688 B/op 23 allocs/opBenchmarkGofastProtobufUnmarshal-8 10006056 597.3 ns/op 560 B/op 15 allocs/op |
|