-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbig_data_test.go
More file actions
359 lines (304 loc) · 8.68 KB
/
big_data_test.go
File metadata and controls
359 lines (304 loc) · 8.68 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
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
package fxjson
import (
"fmt"
"math/rand"
"strings"
"testing"
"time"
)
// TestBigDataHandling 测试大数据量处理
func TestBigDataHandling(t *testing.T) {
// 测试大型JSON数组解析
t.Run("LargeArrayParsing", func(t *testing.T) {
const arraySize = 50000
var builder strings.Builder
builder.WriteByte('[')
for i := 0; i < arraySize; i++ {
if i > 0 {
builder.WriteByte(',')
}
builder.WriteString(fmt.Sprintf(`{"id":%d,"name":"user_%d","value":%f}`, i, i, rand.Float64()*1000))
}
builder.WriteByte(']')
largeJSON := builder.String()
t.Logf("生成大型JSON,大小: %d bytes", len(largeJSON))
// 使用自定义选项增加限制
opts := ParseOptions{
MaxDepth: 1000,
MaxStringLen: 1024 * 1024 * 10, // 10MB字符串限制
MaxObjectKeys: 100000, // 增加对象键限制
MaxArrayItems: 200000, // 增加数组项限制
StrictMode: false,
}
start := time.Now()
node := FromBytesWithOptions([]byte(largeJSON), opts)
parseTime := time.Since(start)
if !node.Exists() {
t.Fatal("大型JSON解析失败")
}
if node.Len() != arraySize {
t.Fatalf("数组长度不匹配,期望: %d,实际: %d", arraySize, node.Len())
}
// 测试随机访问性能
start = time.Now()
for i := 0; i < 1000; i++ {
idx := rand.Intn(arraySize)
item := node.Index(idx)
if !item.Exists() {
t.Fatalf("索引 %d 的元素不存在", idx)
}
id, err := item.Get("id").Int()
if err != nil || id != int64(idx) {
t.Fatalf("索引 %d 的ID不匹配,期望: %d,实际: %d", idx, idx, id)
}
}
accessTime := time.Since(start)
t.Logf("解析时间: %v, 1000次随机访问时间: %v", parseTime, accessTime)
// 性能要求:解析时间应该在合理范围内
if parseTime > 5*time.Second {
t.Errorf("解析时间过长: %v", parseTime)
}
if accessTime > 1*time.Second {
t.Errorf("访问时间过长: %v", accessTime)
}
})
// 测试大型JSON对象解析
t.Run("LargeObjectParsing", func(t *testing.T) {
const objSize = 50000
var builder strings.Builder
builder.WriteByte('{')
for i := 0; i < objSize; i++ {
if i > 0 {
builder.WriteByte(',')
}
builder.WriteString(fmt.Sprintf(`"key_%d":{"type":"data","value":%f,"index":%d}`, i, rand.Float64()*1000, i))
}
builder.WriteByte('}')
largeJSON := builder.String()
t.Logf("生成大型对象JSON,大小: %d bytes", len(largeJSON))
// 使用自定义选项增加限制
opts := ParseOptions{
MaxDepth: 1000,
MaxStringLen: 1024 * 1024 * 10, // 10MB字符串限制
MaxObjectKeys: 100000, // 增加对象键限制
MaxArrayItems: 200000, // 增加数组项限制
StrictMode: false,
}
start := time.Now()
node := FromBytesWithOptions([]byte(largeJSON), opts)
parseTime := time.Since(start)
if !node.Exists() {
t.Fatal("大型对象JSON解析失败")
}
if node.Len() != objSize {
t.Fatalf("对象键数量不匹配,期望: %d,实际: %d", objSize, node.Len())
}
// 测试键查找性能
start = time.Now()
for i := 0; i < 1000; i++ {
keyIdx := rand.Intn(objSize)
key := fmt.Sprintf("key_%d", keyIdx)
item := node.Get(key)
if !item.Exists() {
t.Fatalf("键 %s 的值不存在", key)
}
index, err := item.Get("index").Int()
if err != nil || index != int64(keyIdx) {
t.Fatalf("键 %s 的index不匹配,期望: %d,实际: %d", key, keyIdx, index)
}
}
accessTime := time.Since(start)
t.Logf("对象解析时间: %v, 1000次键查找时间: %v", parseTime, accessTime)
// 性能要求
if parseTime > 3*time.Second {
t.Errorf("对象解析时间过长: %v", parseTime)
}
if accessTime > 1*time.Second {
t.Errorf("键查找时间过长: %v", accessTime)
}
})
// 测试深度嵌套的JSON
t.Run("DeeplyNestedJSON", func(t *testing.T) {
const depth = 1000
var builder strings.Builder
// 构建深层嵌套结构
for i := 0; i < depth; i++ {
builder.WriteString(`{"level":`)
}
builder.WriteString(`"bottom"`)
for i := 0; i < depth; i++ {
builder.WriteByte('}')
}
deepJSON := builder.String()
// 使用更高的深度限制
opts := ParseOptions{
MaxDepth: 2000,
MaxStringLen: 1024 * 1024,
MaxObjectKeys: 10000,
MaxArrayItems: 100000,
StrictMode: false,
}
start := time.Now()
node := FromBytesWithOptions([]byte(deepJSON), opts)
parseTime := time.Since(start)
if !node.Exists() {
t.Fatal("深层嵌套JSON解析失败")
}
// 测试深层访问
start = time.Now()
current := node
for i := 0; i < 100; i++ { // 测试前100层
current = current.Get("level")
if !current.Exists() {
t.Fatalf("第%d层level不存在", i+1)
}
}
accessTime := time.Since(start)
t.Logf("深层嵌套解析时间: %v, 100层访问时间: %v", parseTime, accessTime)
// 性能要求
if parseTime > 2*time.Second {
t.Errorf("深层嵌套解析时间过长: %v", parseTime)
}
if accessTime > 500*time.Millisecond {
t.Errorf("深层访问时间过长: %v", accessTime)
}
})
}
// TestMemoryUsage 测试内存使用情况
func TestMemoryUsage(t *testing.T) {
t.Run("MemoryEfficiencyTest", func(t *testing.T) {
// 生成适中大小的测试数据
const arraySize = 10000
var builder strings.Builder
builder.WriteByte('[')
for i := 0; i < arraySize; i++ {
if i > 0 {
builder.WriteByte(',')
}
builder.WriteString(fmt.Sprintf(`{"id":%d,"name":"test_user_%d","data":[1,2,3,4,5],"meta":{"created":"2023-01-01","active":true}}`, i, i))
}
builder.WriteByte(']')
testJSON := []byte(builder.String())
// 解析多次,检查是否有内存泄漏
for iteration := 0; iteration < 100; iteration++ {
node := FromBytes(testJSON)
if !node.Exists() {
t.Fatal("JSON解析失败")
}
// 遍历一些数据
for i := 0; i < 100; i++ {
item := node.Index(i)
if item.Exists() {
item.Get("name").String()
item.Get("id").Int()
}
}
}
t.Log("内存效率测试完成")
})
}
// TestConcurrencyHandling 测试并发处理
func TestConcurrencyHandling(t *testing.T) {
const goroutineCount = 100
const jsonSize = 1000
// 生成测试JSON
var builder strings.Builder
builder.WriteByte('[')
for i := 0; i < jsonSize; i++ {
if i > 0 {
builder.WriteByte(',')
}
builder.WriteString(fmt.Sprintf(`{"id":%d,"value":%f}`, i, rand.Float64()))
}
builder.WriteByte(']')
testJSON := []byte(builder.String())
// 并发测试
results := make(chan error, goroutineCount)
for i := 0; i < goroutineCount; i++ {
go func(id int) {
node := FromBytes(testJSON)
if !node.Exists() {
results <- fmt.Errorf("goroutine %d: 解析失败", id)
return
}
// 随机访问测试
for j := 0; j < 100; j++ {
idx := rand.Intn(jsonSize)
item := node.Index(idx)
if !item.Exists() {
results <- fmt.Errorf("goroutine %d: 索引 %d 不存在", id, idx)
return
}
idVal, err := item.Get("id").Int()
if err != nil || idVal != int64(idx) {
results <- fmt.Errorf("goroutine %d: ID不匹配 %d != %d", id, idVal, idx)
return
}
}
results <- nil
}(i)
}
// 检查结果
for i := 0; i < goroutineCount; i++ {
if err := <-results; err != nil {
t.Error(err)
}
}
t.Log("并发测试完成")
}
// BenchmarkLargeDataProcessing 大数据处理性能基准测试
func BenchmarkLargeDataProcessing(b *testing.B) {
// 生成测试数据
const arraySize = 10000
var builder strings.Builder
builder.WriteByte('[')
for i := 0; i < arraySize; i++ {
if i > 0 {
builder.WriteByte(',')
}
builder.WriteString(fmt.Sprintf(`{"id":%d,"name":"user_%d","score":%f,"active":true}`, i, i, rand.Float64()*100))
}
builder.WriteByte(']')
testData := []byte(builder.String())
b.Logf("测试数据大小: %d bytes", len(testData))
b.ResetTimer()
b.Run("ParseOnly", func(b *testing.B) {
for i := 0; i < b.N; i++ {
node := FromBytes(testData)
if !node.Exists() {
b.Fatal("解析失败")
}
}
})
b.Run("ParseAndIterate", func(b *testing.B) {
for i := 0; i < b.N; i++ {
node := FromBytes(testData)
if !node.Exists() {
b.Fatal("解析失败")
}
// 遍历所有元素
count := 0
node.ArrayForEach(func(index int, value Node) bool {
count++
return true
})
if count != arraySize {
b.Fatalf("遍历数量不匹配: %d != %d", count, arraySize)
}
}
})
b.Run("RandomAccess", func(b *testing.B) {
node := FromBytes(testData)
if !node.Exists() {
b.Fatal("解析失败")
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
idx := rand.Intn(arraySize)
item := node.Index(idx)
if !item.Exists() {
b.Fatal("随机访问失败")
}
}
})
}