forked from smallnest/gosercomp
/
gosercomp_test.go
173 lines (154 loc) · 3.97 KB
/
gosercomp_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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package gosercomp
import (
"encoding/json"
"encoding/xml"
"testing"
vitessbson "github.com/youtube/vitess/go/bson"
"github.com/golang/protobuf/proto"
goproto "github.com/gogo/protobuf/proto"
flatbuffers "github.com/google/flatbuffers/go"
)
var group = ColorGroup{
Id: 1,
Name: "Reds",
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
}
var protobufGroup = ProtoColorGroup{
Id: proto.Int32(17),
Name: proto.String("Reds"),
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
}
var gogoProtobufGroup = GogoProtoColorGroup{
Id: 1,
Name: "Reds",
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
}
func BenchmarkMarshalByJson(b *testing.B) {
for i := 0; i < b.N; i++ {
json.Marshal(group)
}
}
func BenchmarkUnmarshalByJson(b *testing.B) {
bytes, _ := json.Marshal(group)
result := ColorGroup{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
json.Unmarshal(bytes, &result)
}
}
func BenchmarkMarshalByXml(b *testing.B) {
for i := 0; i < b.N; i++ {
xml.Marshal(group)
}
}
func BenchmarkUnmarshalByXml(b *testing.B) {
bytes, _ := xml.Marshal(group)
result := ColorGroup{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
xml.Unmarshal(bytes, &result)
}
}
func BenchmarkMarshalByBson(b *testing.B) {
for i := 0; i < b.N; i++ {
vitessbson.Marshal(group)
}
}
func BenchmarkUnmarshalByBson(b *testing.B) {
bytes, _ := vitessbson.Marshal(group)
result := ColorGroup{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
vitessbson.Unmarshal(bytes, &result)
}
}
func BenchmarkMarshalByMsgp(b *testing.B) {
for i := 0; i < b.N; i++ {
group.MarshalMsg(nil)
}
}
func BenchmarkUnmarshalByMsgp(b *testing.B) {
bytes, _ := group.MarshalMsg(nil)
result := ColorGroup{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
result.UnmarshalMsg(bytes)
}
}
func BenchmarkMarshalByProtoBuf(b *testing.B) {
for i := 0; i < b.N; i++ {
proto.Marshal(&protobufGroup)
}
}
func BenchmarkUnmarshalByProtoBuf(b *testing.B) {
bytes, _ := proto.Marshal(&protobufGroup)
result := ProtoColorGroup{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
proto.Unmarshal(bytes, &result)
}
}
func BenchmarkMarshalByGogoProtoBuf(b *testing.B) {
for i := 0; i < b.N; i++ {
goproto.Marshal(&gogoProtobufGroup)
}
}
func BenchmarkUnmarshalByGogoProtoBuf(b *testing.B) {
bytes, _ := proto.Marshal(&gogoProtobufGroup)
result := GogoProtoColorGroup{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
goproto.Unmarshal(bytes, &result)
}
}
func serializeByFlatBuffers(builder *flatbuffers.Builder, cg *ColorGroup) []byte {
builder.Reset()
//prepare data
name := builder.CreateString(cg.Name)
//prepare colors array
colorsLen := len(cg.Colors)
FlatBufferColorGroupStartColorsVector(builder, colorsLen)
offsets := make([]flatbuffers.UOffsetT, colorsLen)
for i := colorsLen - 1; i >= 0; i-- {
offsets[i] = builder.CreateString(cg.Colors[i])
}
for i := colorsLen - 1; i >= 0; i-- {
builder.PrependUOffsetT(offsets[i])
}
offset := builder.EndVector(colorsLen)
FlatBufferColorGroupStart(builder)
FlatBufferColorGroupAddCgId(builder, int32(cg.Id))
FlatBufferColorGroupAddName(builder, name)
FlatBufferColorGroupAddColors(builder, offset)
builder.Finish(FlatBufferColorGroupEnd(builder))
return builder.Bytes[builder.Head():]
}
func BenchmarkMarshalByFlatBuffers(b *testing.B) {
builder := flatbuffers.NewBuilder(0)
b.ResetTimer()
for i := 0; i < b.N; i++ {
serializeByFlatBuffers(builder, &group)
}
}
func BenchmarkUnmarshalByFlatBuffers(b *testing.B) {
builder := flatbuffers.NewBuilder(0)
bytes := serializeByFlatBuffers(builder, &group)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = GetRootAsFlatBufferColorGroup(bytes, 0)
}
}
func BenchmarkUnmarshalByFlatBuffers_withFields(b *testing.B) {
builder := flatbuffers.NewBuilder(0)
bytes := serializeByFlatBuffers(builder, &group)
b.ResetTimer()
for i := 0; i < b.N; i++ {
result := GetRootAsFlatBufferColorGroup(bytes, 0)
result.CgId()
result.Name()
colorsLen := result.ColorsLength()
for j := 0; j < colorsLen; j++ {
result.Colors(j)
}
}
}