forked from minio/simdjson-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsed_json_test.go
252 lines (224 loc) · 5.3 KB
/
parsed_json_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
/*
* MinIO Cloud Storage, (C) 2020 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package simdjson
import (
"encoding/binary"
"encoding/json"
"io/ioutil"
"path/filepath"
"testing"
"github.com/klauspost/compress/zstd"
)
const demo_json = `{"Image":{"Width":800,"Height":600,"Title":"View from 15th Floor","Thumbnail":{"Url":"http://www.example.com/image/481989943","Height":125,"Width":100},"Animated":false,"IDs":[116,943,234,38793]}}`
type tester interface {
Fatal(args ...interface{})
}
func loadCompressed(t tester, file string) (ref []byte) {
dec, err := zstd.NewReader(nil)
if err != nil {
t.Fatal(err)
}
ref, err = ioutil.ReadFile(filepath.Join("testdata", file+".json.zst"))
if err != nil {
t.Fatal(err)
}
ref, err = dec.DecodeAll(ref, nil)
if err != nil {
t.Fatal(err)
}
return ref
}
var testCases = []struct {
name string
array bool
}{
{
name: "apache_builds",
},
{
name: "canada",
},
{
name: "citm_catalog",
},
{
name: "github_events",
array: true,
},
{
name: "gsoc-2018",
},
{
name: "instruments",
},
{
name: "numbers",
array: true,
},
{
name: "marine_ik",
},
{
name: "mesh",
},
{
name: "mesh.pretty",
},
{
name: "twitterescaped",
},
{
name: "twitter",
},
{
name: "random",
},
{
name: "update-center",
},
}
func bytesToUint64(buf []byte) []uint64 {
tape := make([]uint64, len(buf)/8)
for i := range tape {
tape[i] = binary.LittleEndian.Uint64(buf[i*8:])
}
return tape
}
func testCTapeCtoGoTapeCompare(t *testing.T, ctape []uint64, csbuf []byte, pj internalParsedJson) {
gotape := pj.Tape
cindex, goindex := 0, 0
for goindex < len(gotape) {
if cindex == len(ctape) {
t.Errorf("TestCTapeCtoGoTapeCompare: unexpected, ctape at end, but gotape not yet")
break
}
cval, goval := ctape[cindex], gotape[goindex]
// Make sure the type is the same between the C and Go version
if cval>>56 != goval>>56 {
t.Errorf("TestCTapeCtoGoTapeCompare: got: %02x want: %02x", goval>>56, cval>>56)
}
ntype := Tag(goval >> 56)
switch ntype {
case TagRoot, TagObjectStart, TagObjectEnd, TagArrayStart, TagArrayEnd:
cindex++
goindex++
case TagString:
cpayload := cval & JSONVALUEMASK
cstrlen := binary.LittleEndian.Uint32(csbuf[cpayload : cpayload+4])
cstr := string(csbuf[cpayload+4 : cpayload+4+uint64(cstrlen)])
gostr, _ := pj.stringAt(goval&JSONVALUEMASK, gotape[goindex+1])
if cstr != gostr {
t.Errorf("TestCTapeCtoGoTapeCompare: got: %s want: %s", gostr, cstr)
}
cindex++
goindex += 2
case TagNull, TagBoolTrue, TagBoolFalse:
cindex++
goindex++
case TagInteger, TagFloat:
if ctape[cindex+1] != gotape[goindex+1] {
if !(ntype == TagFloat && GOLANG_NUMBER_PARSING) {
t.Errorf("TestCTapeCtoGoTapeCompare: got: %016x want: %016x", gotape[goindex+1], ctape[cindex+1])
}
}
cindex += 2
goindex += 2
default:
t.Errorf("TestCTapeCtoGoTapeCompare: unexpected token, got: %02x", ntype)
}
}
if cindex != len(ctape) {
t.Errorf("TestCTapeCtoGoTapeCompare: got: %d want: %d", cindex, len(ctape))
}
}
func BenchmarkIter_MarshalJSONBuffer(b *testing.B) {
if !SupportedCPU() {
b.SkipNow()
}
for _, tt := range testCases {
b.Run(tt.name, func(b *testing.B) {
ref := loadCompressed(b, tt.name)
pj, err := Parse(ref, nil)
if err != nil {
b.Fatal(err)
}
iter := pj.Iter()
cpy := iter
output, err := cpy.MarshalJSON()
if err != nil {
b.Fatal(err)
}
b.SetBytes(int64(len(output)))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
cpy := iter
output, err = cpy.MarshalJSONBuffer(output[:0])
if err != nil {
b.Fatal(err)
}
}
})
}
}
func BenchmarkGoMarshalJSON(b *testing.B) {
for _, tt := range testCases {
b.Run(tt.name, func(b *testing.B) {
ref := loadCompressed(b, tt.name)
var m interface{}
m = map[string]interface{}{}
if tt.array {
m = []interface{}{}
}
err := json.Unmarshal(ref, &m)
if err != nil {
b.Fatal(err)
}
output, err := json.Marshal(m)
if err != nil {
b.Fatal(err)
}
b.SetBytes(int64(len(output)))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
output, err = json.Marshal(m)
if err != nil {
b.Fatal(err)
}
}
})
}
}
func TestPrintJson(t *testing.T) {
if !SupportedCPU() {
t.SkipNow()
}
msg := []byte(demo_json)
expected := `{"Image":{"Width":800,"Height":600,"Title":"View from 15th Floor","Thumbnail":{"Url":"http://www.example.com/image/481989943","Height":125,"Width":100},"Animated":false,"IDs":[116,943,234,38793]}}`
pj, err := Parse(msg, nil)
if err != nil {
t.Errorf("parseMessage failed\n")
}
iter := pj.Iter()
out, err := iter.MarshalJSON()
if err != nil {
t.Fatal(err)
}
if string(out) != expected {
t.Errorf("TestPrintJson: got: %s want: %s", out, expected)
}
}