-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark_test.go
242 lines (211 loc) · 5.46 KB
/
benchmark_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
package benchmark
import (
"encoding/json"
"os"
"testing"
"github.com/arloliu/jsonpack"
"github.com/arloliu/jsonpack/testdata"
goccy "github.com/goccy/go-json"
jsoniter "github.com/json-iterator/go"
"google.golang.org/protobuf/proto"
)
var jsonPacker *jsonpack.JSONPack
func init() {
var err error
jsonPacker = jsonpack.NewJSONPack()
_, err = jsonPacker.AddSchema("complex", testdata.ComplexSchDef)
if err != nil {
os.Exit(1)
}
_, err = jsonPacker.AddSchema("testStruct", testdata.StructSchDef)
if err != nil {
os.Exit(1)
}
}
func BenchmarkSimple_Protobuf_Struct_Encode(b *testing.B) {
st := testdata.TestStructPb{}
err := jsonPacker.Decode("testStruct", testdata.StructExpData, &st)
if err != nil {
b.Fatalf("decode testStruct into pb fail, err:%v\n", err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
proto.Marshal(&st)
}
}
func BenchmarkSimple_JSONPACK_Struct_Encode(b *testing.B) {
sch := jsonPacker.GetSchema("testStruct")
buf := make([]byte, 1024)
b.ResetTimer()
for i := 0; i < b.N; i++ {
sch.EncodeTo(&testdata.StructData, &buf)
}
}
func BenchmarkSimple_JSON_Struct_Encode(b *testing.B) {
for i := 0; i < b.N; i++ {
json.Marshal(&testdata.StructData)
}
}
func BenchmarkSimple_Jsoniter_Struct_Encode(b *testing.B) {
j := jsoniter.ConfigDefault
b.ResetTimer()
for i := 0; i < b.N; i++ {
j.Marshal(&testdata.StructData)
}
}
func BenchmarkSimple_GOCCY_Struct_Encode(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
goccy.Marshal(&testdata.StructData)
}
}
func BenchmarkComplex_JSONPACK_Map_Encode(b *testing.B) {
var err error
sch := jsonPacker.GetSchema("complex")
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err = sch.Encode(&testdata.ComplexData)
if err != nil {
b.FailNow()
}
}
}
func BenchmarkComplex_JSON_Map_Encode(b *testing.B) {
for i := 0; i < b.N; i++ {
json.Marshal(&testdata.ComplexData)
}
}
func BenchmarkComplex_Jsoniter_Map_Encode(b *testing.B) {
j := jsoniter.ConfigDefault
for i := 0; i < b.N; i++ {
j.Marshal(&testdata.ComplexData)
}
}
func BenchmarkComplex_GOCCY_Map_Encode(b *testing.B) {
for i := 0; i < b.N; i++ {
goccy.Marshal(&testdata.ComplexData)
}
}
func BenchmarkComplex_Protobuf_Struct_Encode(b *testing.B) {
var err error
st := testdata.ComplextPb{}
err = jsonPacker.Decode("complex", testdata.ComplexExpData, &st)
if err != nil {
b.Fatalf("decode complex into pb fail, err:%v\n", err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err = proto.Marshal(&st)
if err != nil {
b.FailNow()
}
}
}
func BenchmarkComplex_JSONPACK_Struct_Encode(b *testing.B) {
sch := jsonPacker.GetSchema("complex")
// buf := make([]byte, 4096)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = sch.Encode(&testdata.ComplexStructData)
// _ = sch.EncodeTo(&testdata.ComplexStructData, &buf)
}
}
func BenchmarkComplex_JSON_Struct_Encode(b *testing.B) {
for i := 0; i < b.N; i++ {
json.Marshal(&testdata.ComplexStructData)
}
}
func BenchmarkComplex_Jsoniter_Struct_Encode(b *testing.B) {
j := jsoniter.ConfigDefault
for i := 0; i < b.N; i++ {
j.Marshal(&testdata.ComplexStructData)
}
}
func BenchmarkComplex_GOCCY_Struct_Encode(b *testing.B) {
for i := 0; i < b.N; i++ {
goccy.Marshal(&testdata.ComplexStructData)
}
}
func BenchmarkComplex_JSONPACK_Map_Decode(b *testing.B) {
m := make(map[string]interface{})
sch := jsonPacker.GetSchema("complex")
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := sch.Decode(testdata.ComplexExpData, &m)
if err != nil {
b.Fatalf("decode fail, err: %v", err)
}
}
}
func BenchmarkComplex_JSON_Map_Decode(b *testing.B) {
m := make(map[string]interface{})
for i := 0; i < b.N; i++ {
json.Unmarshal(testdata.ComplexRawData, &m)
}
}
func BenchmarkComplex_Jsoniter_Map_Decode(b *testing.B) {
j := jsoniter.ConfigDefault
m := make(map[string]interface{})
for i := 0; i < b.N; i++ {
j.Unmarshal(testdata.ComplexRawData, &m)
}
}
func BenchmarkComplex_GOCCY_Map_Decode(b *testing.B) {
m := make(map[string]interface{})
for i := 0; i < b.N; i++ {
goccy.Unmarshal(testdata.ComplexRawData, &m)
}
}
func BenchmarkComplex_Protobuf_Struct_Decode(b *testing.B) {
s := testdata.ComplextPb{}
for i := 0; i < b.N; i++ {
err := proto.Unmarshal(testdata.ComplexPbExpData, &s)
if err != nil {
b.Fatalf("decode fail, err: %v", err)
}
}
}
func BenchmarkComplex_JSONPACK_Struct_Decode(b *testing.B) {
s := testdata.Complex{}
sch := jsonPacker.GetSchema("complex")
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := sch.Decode(testdata.ComplexExpData, &s)
if err != nil {
b.Fatalf("decode fail, err: %v", err)
}
}
}
func BenchmarkComplex_JSON_Struct_Decode(b *testing.B) {
s := testdata.Complex{}
for i := 0; i < b.N; i++ {
err := json.Unmarshal(testdata.ComplexRawData, &s)
if err != nil {
b.Fatalf("decode fail, err: %v", err)
}
}
}
func BenchmarkComplex_Jsoniter_Struct_Decode(b *testing.B) {
j := jsoniter.ConfigDefault
s := testdata.Complex{}
for i := 0; i < b.N; i++ {
err := j.Unmarshal(testdata.ComplexRawData, &s)
if err != nil {
b.Fatalf("decode fail, err: %v", err)
}
}
}
func BenchmarkComplex_GOCCY_Struct_Decode(b *testing.B) {
s := testdata.Complex{}
for i := 0; i < b.N; i++ {
err := goccy.Unmarshal(testdata.ComplexRawData, &s)
if err != nil {
b.Fatalf("decode fail, err: %v", err)
}
}
}
//nolint:deadcode,unused
func reportEncodeRatio(t *testing.T, name string, enc []byte, ori []byte) {
var encodeRatio float64 = float64(len(enc)) * 100.0 / float64(len(ori))
t.Logf("%s encode ratio %f%%", name, encodeRatio)
}