-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathbenchmark_small_payload_test.go
95 lines (86 loc) · 1.93 KB
/
benchmark_small_payload_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
package go_benchmark
import (
"testing"
"github.com/buger/jsonparser"
"github.com/json-iterator/go"
"encoding/json"
"github.com/mailru/easyjson/jlexer"
)
func BenchmarkJsonParserSmall(b *testing.B) {
b.ReportAllocs()
paths := [][]string{
[]string{"uuid"},
[]string{"tz"},
[]string{"ua"},
[]string{"st"},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
var data SmallPayload
jsonparser.EachKey(smallFixture, func(idx int, value []byte, vt jsonparser.ValueType, err error) {
switch idx {
case 0:
data.Uuid, _ = jsonparser.ParseString(value)
case 1:
v, _ := jsonparser.ParseInt(value)
data.Tz = int(v)
case 2:
data.Ua, _ = jsonparser.ParseString(value)
case 3:
v, _ := jsonparser.ParseInt(value)
data.St = int(v)
}
}, paths...)
}
}
func BenchmarkJsnoiterPullSmall(b *testing.B) {
b.ReportAllocs()
iter := jsoniter.ParseBytes(smallFixture)
b.ResetTimer()
for i := 0; i < b.N; i++ {
var data SmallPayload
iter.ResetBytes(smallFixture)
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
switch field {
case "uuid":
data.Uuid = iter.ReadString()
case "tz":
data.Tz = iter.ReadInt()
case "ua":
data.Ua = iter.ReadString()
case "st":
data.St = iter.ReadInt()
default:
iter.Skip()
}
}
}
}
func BenchmarkJsnoiterReflectSmall(b *testing.B) {
iter := jsoniter.ParseBytes(smallFixture)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
var data SmallPayload
iter.ResetBytes(smallFixture)
jsoniter.Unmarshal(smallFixture, &data)
}
}
/*
encoding/json
*/
func BenchmarkEncodingJsonStructSmall(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
var data SmallPayload
json.Unmarshal(smallFixture, &data)
}
}
func BenchmarkEasyJsonSmall(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
lexer := &jlexer.Lexer{Data: smallFixture}
data := new(SmallPayload)
data.UnmarshalEasyJSON(lexer)
}
}