forked from smallnest/rpcx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodec_test.go
122 lines (102 loc) ยท 2.63 KB
/
codec_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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package codec
import (
"testing"
"github.com/smallnest/rpcx/codec/testdata"
)
type ColorGroup struct {
Id int `json:"id" xml:"id,attr" msg:"id"`
Name string `json:"name" xml:"name" msg:"name"`
Colors []string `json:"colors" xml:"colors" msg:"colors"`
}
var group = ColorGroup{
Id: 1,
Name: "Reds",
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
}
func BenchmarkJSONCodec_Encode(b *testing.B) {
var raw = make([]byte, 0, 1024)
serializer := JSONCodec{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
raw, _ = serializer.Encode(group)
}
b.ReportMetric(float64(len(raw)), "bytes")
}
func BenchmarkPBCodec_Encode(b *testing.B) {
var raw = make([]byte, 0, 1024)
serializer := PBCodec{}
group := testdata.ProtoColorGroup{
Id: 1,
Name: "Reds",
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
raw, _ = serializer.Encode(&group)
}
b.ReportMetric(float64(len(raw)), "bytes")
}
func BenchmarkMsgpackCodec_Encode(b *testing.B) {
var raw = make([]byte, 0, 1024)
serializer := MsgpackCodec{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
raw, _ = serializer.Encode(group)
}
b.ReportMetric(float64(len(raw)), "bytes")
}
func BenchmarkThriftCodec_Encode(b *testing.B) {
var bb = make([]byte, 0, 1024)
serializer := ThriftCodec{}
thriftColorGroup := testdata.ThriftColorGroup{
ID: 1,
Name: "Reds",
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
bb, _ = serializer.Encode(&thriftColorGroup)
}
b.ReportMetric(float64(len(bb)), "bytes")
}
func BenchmarkJSONCodec_Decode(b *testing.B) {
serializer := JSONCodec{}
bytes, _ := serializer.Encode(group)
result := ColorGroup{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = serializer.Decode(bytes, &result)
}
}
func BenchmarkPBCodec_Decode(b *testing.B) {
serializer := PBCodec{}
bytes, _ := serializer.Encode(group)
result := ColorGroup{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = serializer.Decode(bytes, &result)
}
}
func BenchmarkMsgpackCodec_Decode(b *testing.B) {
serializer := MsgpackCodec{}
bytes, _ := serializer.Encode(group)
result := ColorGroup{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = serializer.Decode(bytes, &result)
}
}
func BenchmarkThriftCodec_Decode(b *testing.B) {
serializer := ThriftCodec{}
thriftColorGroup := testdata.ThriftColorGroup{
ID: 1,
Name: "Reds",
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
}
bytes, _ := serializer.Encode(&thriftColorGroup)
result := testdata.ThriftColorGroup{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = serializer.Decode(bytes, &result)
}
}