forked from json-iterator/go-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpb_test.go
71 lines (64 loc) · 1.58 KB
/
pb_test.go
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package go_benchmark
import (
"encoding/json"
"testing"
"github.com/taiyangc/go-json-benchmarks/testobject"
"git.apache.org/thrift.git/lib/go/thrift"
"github.com/golang/protobuf/proto"
"github.com/json-iterator/go"
)
func Benchmark_protobuf(b *testing.B) {
b.ReportAllocs()
//obj := PbTestObject{"1","2","3","4","5","6","7","8","9","10"}
obj := With2Fields{"1", "2"}
data, _ := proto.Marshal(&obj)
for i := 0; i < b.N; i++ {
proto.Unmarshal(data, &obj)
}
}
func Benchmark_jsoniter2(b *testing.B) {
b.ReportAllocs()
obj := PbTestObject{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}
//obj := With2Fields{"1", "2"}
data, _ := jsoniter.Marshal(&obj)
//buf := &bytes.Buffer{}
//stream := jsoniter.NewStream(buf, 4096)
iter := jsoniter.NewIterator(jsoniter.ConfigFastest)
for i := 0; i < b.N; i++ {
iter.ResetBytes(data)
iter.ReadVal(&obj)
}
}
func Benchmark_json_marshal(b *testing.B) {
b.ReportAllocs()
obj := With2Fields{"1", "2"}
data, _ := jsoniter.Marshal(&obj)
for i := 0; i < b.N; i++ {
json.Unmarshal(data, &obj)
}
}
func Benchmark_thrift(b *testing.B) {
b.ReportAllocs()
obj := testobject.NewThriftTestObject()
obj.Field1 = "1"
obj.Field2 = "2"
obj.Field3 = "3"
obj.Field4 = "4"
obj.Field5 = "5"
obj.Field6 = "6"
obj.Field7 = "7"
obj.Field8 = "8"
obj.Field9 = "9"
obj.Field10 = "10"
buf := thrift.NewTMemoryBuffer()
protocolFactory := &thrift.TCompactProtocolFactory{}
protocol := protocolFactory.GetProtocol(buf)
buf.Reset()
obj.Write(protocol)
data := buf.Bytes()
for i := 0; i < b.N; i++ {
buf.Reset()
buf.Write(data)
obj.Read(protocol)
}
}