protobuf 在 go 表现

protobuf 优势

  • 跨平台跨语言
  • 高效

准备

  • 安装 protobuf
1
2
go install google.golang.org/protobuf/cmd/protoc-gen-go
https://github.com/protocolbuffers/protobuf 下载最新release
  • 新建 blockchain.proto
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
syntax = "proto3";
package tutorial;


import "google/protobuf/timestamp.proto";


option go_package = ".;test";


message Transaction{
string Sender = 1;
string Recipient = 2;
int32 Amount = 3;
}


message Block {
int32 Index = 1;
google.protobuf.Timestamp Timestamp = 2;


repeated Transaction Transactions = 3;


int32 Proof = 4;
string PreviousHash = 5;
}


message BlockchainData{
repeated Block CurrentBlockchain = 1;
repeated Transaction CurrentTransactions = 2;
repeated string Nodes = 3;
}
  • 生成 go 文件,注意目录
1
protoc  -I=/Users/test --go_out=/Users/test /Users/blockchain.proto
  • test,使用 reids 存储
1
2
3
4
5
6
7
8
var jsonTest bool
func GetData(data interface{}) ([]byte, error) {
if jsonTest {
return json.Marshal(data)
} else {
return proto.Marshal(data.(proto.Message))
}
}

现象

  • 10000次,转后存redis。花费7.7s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
redis not open
[StoreAllChain] time cost = 1.275416ms
redis not open
[StoreAllChain] time cost = 1.040458ms
[TestProtoSpeed] time cost = 6.619953912s

Redis open
[StoreAllChain] time cost = 1.471769ms
[StoreAllChain] time cost = 1.158825ms
[StoreAllChain] time cost = 1.081494ms
[TestProtoSpeed] time cost = 7.785562597s

删除其它日志
[TestProtoSpeed] time cost = 7.827823686s
--- PASS: TestProtoSpeed (7.83s)
  • 10000次,json 对比
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
redis not open
[StoreAllChain] time cost = 2.838655ms
redis not open
[StoreAllChain] time cost = 2.539433ms
redis not open
[StoreAllChain] time cost = 2.775757ms
[TestJsonSpeed] time cost = 12.611491383s

Redis open
[StoreAllChain] time cost = 2.643816ms
[StoreAllChain] time cost = 3.363096ms
[StoreAllChain] time cost = 2.989628ms
[StoreAllChain] time cost = 2.51413ms
[TestJsonSpeed] time cost = 16.264468555s

删除其它日志
[TestJsonSpeed] time cost = 17.868695349s
--- PASS: TestJsonSpeed (17.88s)

测试平均结果

10000 20000
Protobuf 7 24
Json 16 61

结论

protobuf 在 go 上的表现和其原始并没有量级的差距,但还是好过 go 原生 json 解析不少。其跨语言跨平台的特性和高效还是值得一试的。