/
benchmark_small_pl_test.go
142 lines (128 loc) · 2.91 KB
/
benchmark_small_pl_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
package main
import (
"encoding/json"
"strconv"
"testing"
"github.com/buger/jsonparser"
gojson "github.com/goccy/go-json"
"github.com/json-iterator/go"
"github.com/mailru/easyjson/jlexer"
"github.com/minio/simdjson-go"
"github.com/valyala/fastjson"
)
// jsonparser
func BenchmarkJsonParserSmall(b *testing.B) {
b.ReportAllocs()
paths := [][]string{
{"uuid"},
{"tz"},
{"ua"},
{"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...)
}
}
// go-json
func BenchmarkGoJsonSmall(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
var data SmallPayload
gojson.Unmarshal(smallFixture, &data)
}
}
// fastjson
func BenchmarkFastJsonSmall(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
var p fastjson.Parser
var data SmallPayload
v, _ := p.ParseBytes(smallFixture)
data.Uuid = string(v.GetStringBytes("uuid"))
data.Tz = v.GetInt("tz")
data.Ua = string(v.GetStringBytes("ua"))
data.St = v.GetInt("st")
}
}
// simdjson-go
func BenchmarkSimdJsonSmall(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
d, _ := simdjson.Parse(smallFixture, nil)
var data SmallPayload
uuid, _ := findKey(d.Iter(), "uuid")
data.Uuid = uuid
tzs, _ := findKey(d.Iter(), "tz")
tz, _ := strconv.Atoi(tzs)
data.Tz = tz
}
}
func BenchmarkJsnoiterPullSmall(b *testing.B) {
b.ReportAllocs()
iter := jsoniter.ParseBytes(jsoniter.ConfigDefault, 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(jsoniter.ConfigDefault, 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)
}
}