-
-
Notifications
You must be signed in to change notification settings - Fork 237
/
bench_test.go
397 lines (332 loc) · 7.28 KB
/
bench_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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package msgpack_test
import (
"bytes"
"encoding/json"
"io/ioutil"
"math"
"testing"
"time"
"github.com/vmihailenco/msgpack/v5"
)
func BenchmarkDiscard(b *testing.B) {
enc := msgpack.NewEncoder(ioutil.Discard)
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := enc.Encode(nil); err != nil {
b.Fatal(err)
}
if err := enc.Encode("hello"); err != nil {
b.Fatal(err)
}
}
}
func benchmarkEncodeDecode(b *testing.B, src, dst interface{}) {
var buf bytes.Buffer
enc := msgpack.NewEncoder(&buf)
dec := msgpack.NewDecoder(&buf)
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := enc.Encode(src); err != nil {
b.Fatal(err)
}
if err := dec.Decode(dst); err != nil {
b.Fatal(err)
}
}
}
func benchmarkJSONEncodeDecode(b *testing.B, src, dst interface{}) {
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
dec := json.NewDecoder(&buf)
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := enc.Encode(src); err != nil {
b.Fatal(err)
}
if err := dec.Decode(dst); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkBool(b *testing.B) {
var dst bool
benchmarkEncodeDecode(b, true, &dst)
}
func BenchmarkInt0(b *testing.B) {
var dst int
benchmarkEncodeDecode(b, 1, &dst)
}
func BenchmarkInt1(b *testing.B) {
var dst int
benchmarkEncodeDecode(b, -33, &dst)
}
func BenchmarkInt2(b *testing.B) {
var dst int
benchmarkEncodeDecode(b, 128, &dst)
}
func BenchmarkInt4(b *testing.B) {
var dst int
benchmarkEncodeDecode(b, 32768, &dst)
}
func BenchmarkInt8(b *testing.B) {
var dst int
benchmarkEncodeDecode(b, int64(2147483648), &dst)
}
func BenchmarkInt32(b *testing.B) {
var dst int32
benchmarkEncodeDecode(b, int32(0), &dst)
}
func BenchmarkFloat32(b *testing.B) {
var dst float32
benchmarkEncodeDecode(b, float32(0), &dst)
}
func BenchmarkFloat32_Max(b *testing.B) {
var dst float32
benchmarkEncodeDecode(b, float32(math.MaxFloat32), &dst)
}
func BenchmarkFloat64(b *testing.B) {
var dst float64
benchmarkEncodeDecode(b, float64(0), &dst)
}
func BenchmarkFloat64_Max(b *testing.B) {
var dst float64
benchmarkEncodeDecode(b, float64(math.MaxFloat64), &dst)
}
func BenchmarkTime(b *testing.B) {
var dst time.Time
benchmarkEncodeDecode(b, time.Now(), &dst)
}
func BenchmarkDuration(b *testing.B) {
var dst time.Duration
benchmarkEncodeDecode(b, time.Hour, &dst)
}
func BenchmarkByteSlice(b *testing.B) {
src := make([]byte, 1024)
var dst []byte
benchmarkEncodeDecode(b, src, &dst)
}
func BenchmarkByteArray(b *testing.B) {
var src [1024]byte
var dst [1024]byte
benchmarkEncodeDecode(b, src, &dst)
}
func BenchmarkByteArrayPtr(b *testing.B) {
var src [1024]byte
var dst [1024]byte
benchmarkEncodeDecode(b, &src, &dst)
}
func BenchmarkMapStringString(b *testing.B) {
src := map[string]string{
"hello": "world",
"foo": "bar",
}
var dst map[string]string
benchmarkEncodeDecode(b, src, &dst)
}
func BenchmarkMapStringStringPtr(b *testing.B) {
src := map[string]string{
"hello": "world",
"foo": "bar",
}
dst := new(map[string]string)
benchmarkEncodeDecode(b, src, &dst)
}
func BenchmarkMapStringInterfaceMsgpack(b *testing.B) {
src := map[string]interface{}{
"hello": "world",
"foo": "bar",
"one": 1111111,
"two": 2222222,
}
var dst map[string]interface{}
benchmarkEncodeDecode(b, src, &dst)
}
func BenchmarkMapStringInterfaceJSON(b *testing.B) {
src := map[string]interface{}{
"hello": "world",
"foo": "bar",
"one": 1111111,
"two": 2222222,
}
var dst map[string]interface{}
benchmarkJSONEncodeDecode(b, src, &dst)
}
func BenchmarkMapIntInt(b *testing.B) {
src := map[int]int{
1: 10,
2: 20,
}
var dst map[int]int
benchmarkEncodeDecode(b, src, &dst)
}
func BenchmarkStringSlice(b *testing.B) {
src := []string{"hello", "world"}
var dst []string
benchmarkEncodeDecode(b, src, &dst)
}
func BenchmarkStringSlicePtr(b *testing.B) {
src := []string{"hello", "world"}
var dst []string
dstptr := &dst
benchmarkEncodeDecode(b, src, &dstptr)
}
type benchmarkStruct struct {
Name string
Age int
Colors []string
Data []byte
CreatedAt time.Time
UpdatedAt time.Time
}
type benchmarkStruct2 struct {
Name string
Age int
Colors []string
Data []byte
CreatedAt time.Time
UpdatedAt time.Time
}
var (
_ msgpack.CustomEncoder = (*benchmarkStruct2)(nil)
_ msgpack.CustomDecoder = (*benchmarkStruct2)(nil)
)
func (s *benchmarkStruct2) EncodeMsgpack(enc *msgpack.Encoder) error {
return enc.EncodeMulti(
s.Name,
s.Colors,
s.Age,
s.Data,
s.CreatedAt,
s.UpdatedAt,
)
}
func (s *benchmarkStruct2) DecodeMsgpack(dec *msgpack.Decoder) error {
return dec.DecodeMulti(
&s.Name,
&s.Colors,
&s.Age,
&s.Data,
&s.CreatedAt,
&s.UpdatedAt,
)
}
func structForBenchmark() *benchmarkStruct {
return &benchmarkStruct{
Name: "Hello World",
Colors: []string{"red", "orange", "yellow", "green", "blue", "violet"},
Age: math.MaxInt32,
Data: make([]byte, 1024),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
}
func structForBenchmark2() *benchmarkStruct2 {
return &benchmarkStruct2{
Name: "Hello World",
Colors: []string{"red", "orange", "yellow", "green", "blue", "violet"},
Age: math.MaxInt32,
Data: make([]byte, 1024),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
}
func BenchmarkStructVmihailencoMsgpack(b *testing.B) {
in := structForBenchmark()
out := new(benchmarkStruct)
b.ResetTimer()
for i := 0; i < b.N; i++ {
buf, err := msgpack.Marshal(in)
if err != nil {
b.Fatal(err)
}
err = msgpack.Unmarshal(buf, out)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkStructMarshal(b *testing.B) {
in := structForBenchmark()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := msgpack.Marshal(in)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkStructUnmarshal(b *testing.B) {
in := structForBenchmark()
buf, err := msgpack.Marshal(in)
if err != nil {
b.Fatal(err)
}
out := new(benchmarkStruct)
b.ResetTimer()
for i := 0; i < b.N; i++ {
err = msgpack.Unmarshal(buf, out)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkStructManual(b *testing.B) {
in := structForBenchmark2()
out := new(benchmarkStruct2)
b.ResetTimer()
for i := 0; i < b.N; i++ {
buf, err := msgpack.Marshal(in)
if err != nil {
b.Fatal(err)
}
err = msgpack.Unmarshal(buf, out)
if err != nil {
b.Fatal(err)
}
}
}
type benchmarkStructPartially struct {
Name string
Age int
}
func BenchmarkStructUnmarshalPartially(b *testing.B) {
in := structForBenchmark()
buf, err := msgpack.Marshal(in)
if err != nil {
b.Fatal(err)
}
out := new(benchmarkStructPartially)
b.ResetTimer()
for i := 0; i < b.N; i++ {
err = msgpack.Unmarshal(buf, out)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkQuery(b *testing.B) {
var records []map[string]interface{}
for i := 0; i < 1000; i++ {
record := map[string]interface{}{
"id": int64(i),
"attrs": map[string]interface{}{"phone": int64(i)},
}
records = append(records, record)
}
bs, err := msgpack.Marshal(records)
if err != nil {
b.Fatal(err)
}
dec := msgpack.NewDecoder(bytes.NewBuffer(bs))
b.ResetTimer()
for i := 0; i < b.N; i++ {
dec.Reset(bytes.NewBuffer(bs))
values, err := dec.Query("10.attrs.phone")
if err != nil {
b.Fatal(err)
}
if values[0].(int64) != 10 {
b.Fatalf("%v != %d", values[0], 10)
}
}
}