-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_test.go
365 lines (349 loc) · 7.85 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
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
package qjson
import (
"encoding/json"
"io/ioutil"
"testing"
)
func BenchmarkMarshalQJSON(b *testing.B) {
t, err := Decode(textTpl)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := t.MarshalJSON(); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkUnmarshalQJSON(b *testing.B) {
for i := 0; i < b.N; i++ {
if tree, err := Decode(textTpl); err != nil {
b.Fatal(err)
} else {
tree.Release()
}
}
}
func BenchmarkMarshalStd(b *testing.B) {
m := make(map[string]interface{})
if err := json.Unmarshal(textTpl, &m); err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := json.Marshal(m); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkUnmarshalStd(b *testing.B) {
for i := 0; i < b.N; i++ {
m := make(map[string]interface{})
if err := json.Unmarshal(textTpl, &m); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkParallel(b *testing.B) {
jsonFeed := make(map[string]string)
files, err := ioutil.ReadDir("./test_feed")
if err != nil {
return
}
for _, file := range files {
filename := "./test_feed/" + file.Name()
data, err := ioutil.ReadFile(filename)
if err != nil {
b.Fatalf("read test file %s %v", filename, err)
}
jsonFeed[file.Name()] = string(data)
}
jsonFeed["buildin"] = string(textTpl)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
for _, js := range jsonFeed {
validJSON(b, []byte(js))
}
}
})
}
func validJSON(b *testing.B, data []byte) {
compareFn := func(m interface{}, tree *JSONTree) {
if mv, ok := m.(map[string]interface{}); ok {
compareTreeWithMap(b, mv, tree.Root.ObjectValues)
} else if av, ok := m.([]interface{}); ok {
compareTreeWithArray(b, av, tree.Root.ArrayValues)
} else {
vv, err := json.Marshal(m)
if err != nil {
b.Fatal(err)
}
if string(vv) != tree.Root.Value {
b.Fatalf("%s != %s", string(vv), tree.Root.Value)
}
}
}
var tree1, tree2 *JSONTree
var err error
tree1, err = Decode(data)
if err != nil {
b.Fatal(err)
}
var m interface{}
json.Unmarshal(data, &m)
compareFn(m, tree1)
// compare again
data, err = tree1.MarshalJSON()
if err != nil {
b.Fatal(err)
}
tree1.Release()
tree2, err = Decode(data)
if err != nil {
b.Fatal(err)
}
compareFn(m, tree2)
tree2.Release()
}
func compareTreeWithArray(b *testing.B, m []interface{}, elems []*Node) {
if len(m) != len(elems) {
b.Fatal("length not match")
}
for i, item := range m {
tv := elems[i]
switch elems[i].Type {
case Null:
if item != nil {
b.Fatal("item should be nil")
}
case String:
var s string
err := json.Unmarshal([]byte(tv.Value), &s)
if err != nil {
b.Fatal(err)
}
if item.(string) != s {
b.Fatalf("%v != %v", item.(string), s)
}
case Bool:
var s bool
err := json.Unmarshal([]byte(tv.Value), &s)
if err != nil {
b.Fatal(err)
}
if item.(bool) != s {
b.Fatalf("%v != %v", item.(bool), s)
}
case Integer, Float:
vs, _ := json.Marshal(item)
if string(vs) != tv.Value {
b.Fatalf("%s != %s", string(vs), tv.Value)
}
case Object:
sub, ok := item.(map[string]interface{})
if !ok {
b.Fatal("not ok")
}
compareTreeWithMap(b, sub, tv.ObjectValues)
case Array:
arr, ok := item.([]interface{})
if !ok {
b.Fatal("not ok")
}
compareTreeWithArray(b, arr, tv.ArrayValues)
}
}
}
func compareTreeWithMap(b *testing.B, m map[string]interface{}, objectValues []*ObjectElem) {
if len(m) != len(objectValues) {
b.Fatal("length not match")
}
for k, v := range m {
/* find key */
var tv *Node
var found bool
for _, kv := range objectValues {
var str string
err := json.Unmarshal([]byte(kv.Key.Value), &str)
if err != nil {
b.Fatal(err)
}
if k == str {
found = true
tv = kv.Value
break
}
}
if !found {
b.Fatalf("should find key %s", k)
return
}
/* match value */
switch tv.Type {
case Null:
if v != nil {
b.Fatal("should be nil")
}
case String:
var s string
err := json.Unmarshal([]byte(tv.Value), &s)
if err != nil {
b.Fatal(err)
}
if v.(string) != s {
b.Fatalf("%s != %s", v.(string), s)
}
case Bool:
var s bool
err := json.Unmarshal([]byte(tv.Value), &s)
if err != nil {
b.Fatal(err)
}
if v.(bool) != s {
b.Fatalf("%v != %v", v.(bool), s)
}
case Float, Integer:
vs, _ := json.Marshal(v)
if string(vs) != tv.Value {
b.Fatalf("%s != %s", string(vs), tv.Value)
}
case Object:
sub, ok := v.(map[string]interface{})
if !ok {
b.Fatal("should be ok")
}
compareTreeWithMap(b, sub, tv.ObjectValues)
case Array:
arr, ok := v.([]interface{})
if !ok {
b.Fatal("should be ok")
}
compareTreeWithArray(b, arr, tv.ArrayValues)
}
}
}
/* test JSON snippets */
var (
textTpl = []byte(`
{
"destination_addresses": [
"Washington, DC, USA",
"Philadelphia, PA, USA",
"Santa Barbara, CA, USA",
"Miami, FL, USA",
"Austin, TX, USA",
"Napa County, CA, USA"
],
"origin_addresses": [
"New York, NY, USA"
],
"rows": [{
"elements": [{
"distance": {
"text": "227 mi",
"value": 365468
},
"duration": {
"text": "3 hours 54 mins",
"value": 14064
},
"status": "OK"
},
{
"distance": {
"text": "94.6 mi",
"value": 152193
},
"duration": {
"text": "1 hour 44 mins",
"value": 6227
},
"status": "OK"
},
{
"distance": {
"text": "2,878 mi",
"value": 4632197
},
"duration": {
"text": "1 day 18 hours",
"value": 151772
},
"status": "OK"
},
{
"distance": {
"text": "1,286 mi",
"value": 2069031
},
"duration": {
"text": "18 hours 43 mins",
"value": 67405
},
"status": "OK"
},
{
"distance": {
"text": "1,742 mi",
"value": 2802972
},
"duration": {
"text": "1 day 2 hours",
"value": 93070
},
"status": "OK"
},
{
"distance": {
"text": "2,871 mi",
"value": 4620514
},
"duration": {
"text": "1 day 18 hours",
"value": 152913
},
"status": "OK"
}
]
}],
"names":null,
"status": "OK"
}
`)
text2Tpl = []byte(`{
"accounting" : [
{ "firstName" : "John",
"lastName" : "Doe",
"age" : 23 },
{ "firstName" : "Mary",
"lastName" : "Smith",
"age" : 32 }
],
"sales" : [
{ "firstName" : "Sally",
"lastName" : "Green",
"age" : 27 },
{ "firstName" : "Jim",
"lastName" : "Galley",
"age" : -1.2 }
]
} `)
text3Tpl = []byte(`{
"content": "{\"blocks\":[{\"key\":\"70eu2\",\"text\":\"show tex 2@ttx \",\"type\":\"unstyled\",\"depth\":0,\"inlineStyleRanges\":[],\"entityRanges\":[{\"offset\":14,\"length\":4,\"key\":0}],\"data\":{}}],\"entityMap\":{\"0\":{\"type\":\"mention\",\"mutability\":\"IMMUTABLE\",\"data\":{\"mention\":{\"id\":\"117\",\"name\":\"RTx\",\"additionalName\":\"eer\",\"emailPrefix\":\"674829897\",\"desc\":\"world123\",\"query\":\"\",\"localeNames\":{\"zh\":\"erte\",\"en\":\".7x5\"},\"avatar\":\"https://ggogle..png\"}}}},\"atEmployeeIds\":[\"697\"]}",
"notify": [
"1234567",
1,
1.2,
-100.2,
false,
null
],
"token": "edd05471-7823-5d38-8a7c-e60d763c3001",
"version": 100,
"t_uuid": "Av9DGOKlUgM",
"take_t": true,
"changesets": "[]"
}`)
)