-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathparser_test.go
More file actions
217 lines (201 loc) · 5.84 KB
/
parser_test.go
File metadata and controls
217 lines (201 loc) · 5.84 KB
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
package cheapjson_test
import (
"encoding/json"
"log"
"math"
"os"
"strconv"
"strings"
"testing"
"github.com/a8m/djson"
"github.com/acrazing/cheapjson"
json_test_suite "github.com/acrazing/json-test-suite"
"github.com/bitly/go-simplejson"
"github.com/stretchr/testify/assert"
)
func initData() {
var normalData interface{} = map[string]interface{}{
"string": "string",
"true": true,
"false": false,
"null": nil,
"int": int64(math.MaxInt64),
"-int": -int64(math.MaxInt64),
"float": math.MaxFloat64,
"float2": math.SmallestNonzeroFloat64,
"-float": -math.MaxFloat64,
"-float2": -math.SmallestNonzeroFloat64,
"文\r\n\t\f\b中文中😍😘😢😓文": "中文中文中文\r\n\t\f\b中文中😍😘😢😓文中文",
"文\r\n\t\f\b中中😍😘😢😓文": []interface{}{
"string",
true,
false,
nil,
int64(math.MaxInt64),
-int64(math.MaxInt64),
math.MaxFloat64,
math.SmallestNonzeroFloat64,
-math.MaxFloat64,
-math.SmallestNonzeroFloat64,
"中文中文中文\r\n\t\f\b中文中😍😘😢😓文中文",
[]interface{}{"😍😘😢😓", "\r\t\f\n\b"},
},
}
var normalInput, _ = json.MarshalIndent(normalData, "", " ")
var bigData interface{} = map[string]interface{}{}
var bigInput []byte
var deepData interface{} = map[string]interface{}{}
var deepInput []byte
tempData := bigData
size := 40
for i := 0; i < size; i++ {
if temp1, ok := tempData.(map[string]interface{}); ok {
key1 := strings.Repeat("中文中文中文\r\n\t\f\b中文中😍😘😢😓文中文", i+1)
temp2 := map[string]map[string]interface{}{}
temp1[key1] = temp2
for j := 0; j < size; j++ {
key2 := strconv.Itoa(math.MaxInt64 - j)
temp3 := map[string]interface{}{}
temp2[key2] = temp3
for k := 0; k < size; k++ {
key3 := strings.Repeat("中文中文\r\n\t\f\b中文中😍", k+1)
temp3[key3] = normalData
}
}
}
}
tempData = deepData
for i := 0; i < 1000; i++ {
if temp, ok := tempData.(map[string]interface{}); ok {
tempData = map[string]interface{}{}
temp["中文中文\r\n\t\f\b中文中😍😘😢😓文中文"] = tempData
}
}
if temp, ok := tempData.(map[string]interface{}); ok {
temp["1"] = normalData
}
bigInput, _ = json.MarshalIndent(bigData, "", " ")
deepInput, _ = json.MarshalIndent(deepData, "", " ")
_ = os.MkdirAll("./data", 0777)
_ = os.WriteFile("./data/normal.json", normalInput, 0777)
_ = os.WriteFile("./data/big.json", bigInput, 0777)
_ = os.WriteFile("./data/deep.json", deepInput, 0777)
}
var normalInput []byte
var bigInput []byte
var deepInput []byte
var profileCount int
func init() {
if _, err := os.Stat("./data/normal.json"); os.IsNotExist(err) {
initData()
}
normalInput, _ = os.ReadFile("./data/normal.json")
bigInput, _ = os.ReadFile("./data/big.json")
deepInput, _ = os.ReadFile("./data/deep.json")
log.Printf("big input size: %d, normal input size: %d, deep input size: %d", len(bigInput), len(normalInput), len(deepInput))
}
func TestUnmarshal(t *testing.T) {
value, err := cheapjson.Unmarshal(normalInput)
assert.Nil(t, err)
jsonOutput, err := json.MarshalIndent(value.Value(), "", " ")
assert.Nil(t, err)
assert.Equal(t, normalInput, jsonOutput)
value, err = cheapjson.Unmarshal(bigInput)
assert.Nil(t, err)
jsonOutput, err = json.MarshalIndent(value.Value(), "", " ")
assert.Nil(t, err)
assert.Equal(t, bigInput, jsonOutput)
value, err = cheapjson.Unmarshal(deepInput)
assert.Nil(t, err)
jsonOutput, err = json.MarshalIndent(value.Value(), "", " ")
assert.Nil(t, err)
assert.Equal(t, deepInput, jsonOutput)
value, err = cheapjson.Unmarshal([]byte("\"\\ud83d\\ude02\\ud83d\\ude03\\u4e2d\\u56fd\\u4ebA\""))
assert.Nil(t, err)
assert.Equal(t, "😂😃中国人", value.String())
}
func BenchmarkUnmarshalBigInput(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
value, _ := cheapjson.Unmarshal(bigInput)
assert.NotNil(b, value)
}
})
}
func BenchmarkSimpleJsonBigInput(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
value := simplejson.New()
err := value.UnmarshalJSON(bigInput)
assert.Nil(b, err)
}
})
}
func BenchmarkUnmarshalNormalInput(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
value, _ := cheapjson.Unmarshal(normalInput)
assert.NotNil(b, value)
}
})
}
func BenchmarkSimpleJsonNormalInput(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
value := simplejson.New()
err := value.UnmarshalJSON(normalInput)
assert.Nil(b, err)
}
})
}
func BenchmarkUnmarshalDeepInput(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
value, _ := cheapjson.Unmarshal(deepInput)
assert.NotNil(b, value)
}
})
}
func BenchmarkSimpleJsonDeepInput(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
value := simplejson.New()
err := value.UnmarshalJSON(deepInput)
assert.Nil(b, err)
}
})
}
func TestProfileUnmarshal(t *testing.T) {
for i := 0; i < profileCount; i++ {
value, err := cheapjson.Unmarshal(bigInput)
assert.NotNil(t, value)
assert.Nil(t, err)
}
}
func TestProfileSimpleJson(t *testing.T) {
for i := 0; i < profileCount; i++ {
value, err := simplejson.NewJson(bigInput)
assert.NotNil(t, value)
assert.Nil(t, err)
}
}
func TestProfileDjson(t *testing.T) {
for i := 0; i < profileCount; i++ {
value, err := djson.Decode(bigInput)
assert.Nil(t, err)
assert.NotNil(t, value)
}
}
func BenchmarkCompareMarshal(b *testing.B) {
json_test_suite.CompareUnmarshal(map[string]func(data []byte) (interface{}, error){
"cheapjson": func(data []byte) (interface{}, error) {
return cheapjson.Unmarshal(data)
},
"djson": func(data []byte) (interface{}, error) {
return djson.Decode(data)
},
"go-simplejson": func(data []byte) (interface{}, error) {
return simplejson.NewJson(data)
},
}, "./json-test-suite/correct")
}