forked from alecthomas/go_serialization_benchmarks
/
serialization_benchmarks_test.go
216 lines (180 loc) · 4.4 KB
/
serialization_benchmarks_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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package goserbench
import (
vitessbson "code.google.com/p/vitess/go/bson"
"encoding/json"
"fmt"
"github.com/ugorji/go-msgpack"
"labix.org/v2/mgo/bson"
"math/rand"
"testing"
"time"
)
type A struct {
Name string
BirthDay time.Time
Phone string
Siblings int
Spouse bool
Money float64
Tags map[string]string
Aliases []string
}
func randString(l int) string {
buf := make([]byte, l)
for i := 0; i < (l+1)/2; i++ {
buf[i] = byte(rand.Intn(256))
}
return fmt.Sprintf("%x", buf)[:l]
}
func generate() []*A {
a := make([]*A, 0, 1000)
for i := 0; i < 1000; i++ {
a = append(a, &A{
Name: randString(16),
BirthDay: time.Now(),
Phone: randString(10),
Siblings: rand.Intn(5),
Spouse: rand.Intn(2) == 1,
Money: rand.Float64(),
Tags: map[string]string{"a": "a", "b": "b", "c": "c"},
Aliases: []string{randString(5), randString(5)},
})
}
return a
}
type Serializer interface {
Marshal(o interface{}) []byte
Unmarshal(d []byte, o interface{}) error
String() string
}
type MsgpackSerializer int
func (m MsgpackSerializer) Marshal(o interface{}) []byte {
d, _ := msgpack.Marshal(o)
return d
}
func (m MsgpackSerializer) Unmarshal(d []byte, o interface{}) error {
return msgpack.Unmarshal(d, o, nil)
}
func (m MsgpackSerializer) String() string {
return "msgpack"
}
type JsonSerializer int
func (m JsonSerializer) Marshal(o interface{}) []byte {
d, _ := json.Marshal(o)
return d
}
func (m JsonSerializer) Unmarshal(d []byte, o interface{}) error {
return json.Unmarshal(d, o)
}
func (j JsonSerializer) String() string {
return "json"
}
type BsonSerializer int
func (m BsonSerializer) Marshal(o interface{}) []byte {
d, _ := bson.Marshal(o)
return d
}
func (m BsonSerializer) Unmarshal(d []byte, o interface{}) error {
return bson.Unmarshal(d, o)
}
func (j BsonSerializer) String() string {
return "bson"
}
type VitessBsonSerializer int
func (m VitessBsonSerializer) Marshal(o interface{}) []byte {
d, _ := vitessbson.Marshal(o)
return d
}
func (m VitessBsonSerializer) Unmarshal(d []byte, o interface{}) error {
return vitessbson.Unmarshal(d, o)
}
func (j VitessBsonSerializer) String() string {
return "vitessbson"
}
func benchMarshal(b *testing.B, s Serializer) {
b.StopTimer()
data := generate()
b.StartTimer()
var size uint64 = 0
for i := 0; i < b.N; i++ {
b := s.Marshal(data[rand.Intn(len(data))])
size += uint64(len(b))
}
// println(fmt.Sprintf("average size of %s serialized structure is %d", s, size/uint64(b.N)))
}
func cmpTags(a, b map[string]string) bool {
if len(a) != len(b) {
return false
}
for k, v := range a {
if bv, ok := b[k]; !ok || bv != v {
return false
}
}
return true
}
func cmpAliases(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if b[i] != v {
return false
}
}
return true
}
func benchUnmarshal(b *testing.B, s Serializer) {
b.StopTimer()
data := generate()
ser := make([][]byte, len(data))
for i, d := range data {
ser[i] = s.Marshal(d)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
n := rand.Intn(len(ser))
o := &A{}
err := s.Unmarshal(ser[n], o)
if err != nil {
b.Fatalf("%s failed to unmarshal: %s", s, err)
}
// Validate unmarshalled data.
// i := data[n]
// // o.BirthDay.String() == i.BirthDay.String() &&
// correct := o.Name == i.Name && o.Phone == i.Phone && o.Siblings == i.Siblings && o.Spouse == i.Spouse && o.Money == i.Money && cmpTags(o.Tags, i.Tags) && cmpAliases(o.Aliases, i.Aliases)
// if !correct {
// b.Fatalf("unmarshaled object differed:\n%v\n%v", i, o)
// }
}
}
func TestMessage(t *testing.T) {
println(`
A test suite for benchmarking various Go serialization methods.
See README.md for details on running the benchmarks.
`)
}
func BenchmarkMsgpackMarshal(b *testing.B) {
benchMarshal(b, MsgpackSerializer(0))
}
func BenchmarkMsgpackUnmarshal(b *testing.B) {
benchUnmarshal(b, MsgpackSerializer(0))
}
func BenchmarkJsonMarshal(b *testing.B) {
benchMarshal(b, JsonSerializer(0))
}
func BenchmarkJsonUnmarshal(b *testing.B) {
benchUnmarshal(b, JsonSerializer(0))
}
func BenchmarkBsonMarshal(b *testing.B) {
benchMarshal(b, BsonSerializer(0))
}
func BenchmarkBsonUnmarshal(b *testing.B) {
benchUnmarshal(b, BsonSerializer(0))
}
func BenchmarkVitessBsonMarshal(b *testing.B) {
benchMarshal(b, VitessBsonSerializer(0))
}
func BenchmarkVitessBsonUnmarshal(b *testing.B) {
benchUnmarshal(b, VitessBsonSerializer(0))
}