-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
540 lines (486 loc) · 13 KB
/
benchmark_test.go
File metadata and controls
540 lines (486 loc) · 13 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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
package jsonpatch
import (
"encoding/json"
"fmt"
"strconv"
"testing"
)
// ---------------------------------------------------------------------------
// Helpers – reusable test fixtures
// ---------------------------------------------------------------------------
// makeObject builds a flat JSON object with n string keys.
//
// {"key_0":"val_0", "key_1":"val_1", …}
func makeObject(n int) []byte {
m := make(map[string]string, n)
for i := range n {
m["key_"+strconv.Itoa(i)] = "val_" + strconv.Itoa(i)
}
b, _ := json.Marshal(m)
return b
}
// makeArray builds a JSON array with n integer elements.
//
// [0, 1, 2, …]
func makeArray(n int) []byte {
a := make([]int, n)
for i := range n {
a[i] = i
}
b, _ := json.Marshal(a)
return b
}
// makeNestedObject builds a deeply-nested JSON object.
//
// {"a":{"a":{"a":… "leaf" …}}}
func makeNestedObject(depth int) []byte {
var inner interface{} = "leaf"
for range depth {
inner = map[string]interface{}{"a": inner}
}
b, _ := json.Marshal(inner)
return b
}
// modifyObject changes roughly half the keys in the object.
func modifyObject(original []byte) []byte {
var m map[string]interface{}
_ = json.Unmarshal(original, &m)
i := 0
for k := range m {
if i%2 == 0 {
m[k] = "changed"
}
i++
}
b, _ := json.Marshal(m)
return b
}
// ---------------------------------------------------------------------------
// Benchmark: Apply
// ---------------------------------------------------------------------------
func BenchmarkApply_SingleAdd(b *testing.B) {
doc := []byte(`{"foo":"bar"}`)
patch := []byte(`[{"op":"add","path":"/baz","value":"qux"}]`)
b.ResetTimer()
for b.Loop() {
_, _ = Apply(doc, patch)
}
}
func BenchmarkApply_SingleReplace(b *testing.B) {
doc := []byte(`{"foo":"bar"}`)
patch := []byte(`[{"op":"replace","path":"/foo","value":"baz"}]`)
b.ResetTimer()
for b.Loop() {
_, _ = Apply(doc, patch)
}
}
func BenchmarkApply_SingleRemove(b *testing.B) {
doc := []byte(`{"foo":"bar","baz":"qux"}`)
patch := []byte(`[{"op":"remove","path":"/baz"}]`)
b.ResetTimer()
for b.Loop() {
_, _ = Apply(doc, patch)
}
}
func BenchmarkApply_Move(b *testing.B) {
doc := []byte(`{"foo":{"bar":"baz"},"qux":{"corge":"grault"}}`)
patch := []byte(`[{"op":"move","from":"/foo/bar","path":"/qux/thud"}]`)
b.ResetTimer()
for b.Loop() {
_, _ = Apply(doc, patch)
}
}
func BenchmarkApply_Copy(b *testing.B) {
doc := []byte(`{"foo":{"bar":"baz"}}`)
patch := []byte(`[{"op":"copy","from":"/foo/bar","path":"/foo/qux"}]`)
b.ResetTimer()
for b.Loop() {
_, _ = Apply(doc, patch)
}
}
func BenchmarkApply_Test(b *testing.B) {
doc := []byte(`{"foo":"bar"}`)
patch := []byte(`[{"op":"test","path":"/foo","value":"bar"}]`)
b.ResetTimer()
for b.Loop() {
_, _ = Apply(doc, patch)
}
}
func BenchmarkApply_MultipleOps(b *testing.B) {
doc := []byte(`{"foo":"bar","baz":"qux"}`)
patch := []byte(`[
{"op":"replace","path":"/foo","value":"new"},
{"op":"add","path":"/added","value":true},
{"op":"remove","path":"/baz"},
{"op":"add","path":"/arr","value":[1,2,3]}
]`)
b.ResetTimer()
for b.Loop() {
_, _ = Apply(doc, patch)
}
}
// BenchmarkApply_LargeDocument tests applying a single operation to
// documents of increasing size.
func BenchmarkApply_LargeDocument(b *testing.B) {
sizes := []int{10, 100, 1000}
for _, n := range sizes {
doc := makeObject(n)
patch := []byte(`[{"op":"add","path":"/newkey","value":"newval"}]`)
b.Run(fmt.Sprintf("keys_%d", n), func(b *testing.B) {
b.ResetTimer()
for b.Loop() {
_, _ = Apply(doc, patch)
}
})
}
}
// BenchmarkApply_LargePatch tests applying many operations to a small document.
func BenchmarkApply_LargePatch(b *testing.B) {
counts := []int{10, 50, 100}
for _, n := range counts {
doc := []byte(`{}`)
ops := make([]Operation, n)
for i := range n {
ops[i], _ = NewOperation(OpAdd, "/key_"+strconv.Itoa(i), "val")
}
patchJSON, _ := json.Marshal(ops)
b.Run(fmt.Sprintf("ops_%d", n), func(b *testing.B) {
b.ResetTimer()
for b.Loop() {
_, _ = Apply(doc, patchJSON)
}
})
}
}
// BenchmarkApply_DeepNested benchmarks apply on deeply-nested documents.
func BenchmarkApply_DeepNested(b *testing.B) {
depths := []int{5, 10, 20}
for _, d := range depths {
doc := makeNestedObject(d)
// Build a pointer that reaches the leaf.
path := ""
for range d {
path += "/a"
}
patch := []byte(fmt.Sprintf(`[{"op":"replace","path":"%s","value":"replaced"}]`, path))
b.Run(fmt.Sprintf("depth_%d", d), func(b *testing.B) {
b.ResetTimer()
for b.Loop() {
_, _ = Apply(doc, patch)
}
})
}
}
// BenchmarkApply_ArrayInsert benchmarks inserting into arrays of various sizes.
func BenchmarkApply_ArrayInsert(b *testing.B) {
sizes := []int{10, 100, 1000}
for _, n := range sizes {
arr := makeArray(n)
doc := []byte(fmt.Sprintf(`{"items":%s}`, arr))
patch := []byte(`[{"op":"add","path":"/items/0","value":999}]`)
b.Run(fmt.Sprintf("len_%d", n), func(b *testing.B) {
b.ResetTimer()
for b.Loop() {
_, _ = Apply(doc, patch)
}
})
}
}
// ---------------------------------------------------------------------------
// Benchmark: ApplyPatch (pre-decoded patch, avoids repeated decode cost)
// ---------------------------------------------------------------------------
func BenchmarkApplyPatch_PreDecoded(b *testing.B) {
doc := []byte(`{"foo":"bar"}`)
patchJSON := []byte(`[
{"op":"add","path":"/x","value":1},
{"op":"replace","path":"/foo","value":"baz"},
{"op":"add","path":"/y","value":[1,2,3]},
{"op":"test","path":"/foo","value":"baz"}
]`)
patch, _ := DecodePatch(patchJSON)
b.ResetTimer()
for b.Loop() {
_, _ = ApplyPatch(doc, patch)
}
}
// ---------------------------------------------------------------------------
// Benchmark: CreatePatch (diff)
// ---------------------------------------------------------------------------
func BenchmarkCreatePatch_SmallObject(b *testing.B) {
original := []byte(`{"foo":"bar","baz":"qux"}`)
modified := []byte(`{"foo":"changed","baz":"qux","added":"new"}`)
b.ResetTimer()
for b.Loop() {
_, _ = CreatePatch(original, modified)
}
}
func BenchmarkCreatePatch_IdenticalObjects(b *testing.B) {
doc := makeObject(100)
b.ResetTimer()
for b.Loop() {
_, _ = CreatePatch(doc, doc)
}
}
func BenchmarkCreatePatch_LargeObject(b *testing.B) {
sizes := []int{10, 100, 1000}
for _, n := range sizes {
original := makeObject(n)
modified := modifyObject(original)
b.Run(fmt.Sprintf("keys_%d", n), func(b *testing.B) {
b.ResetTimer()
for b.Loop() {
_, _ = CreatePatch(original, modified)
}
})
}
}
func BenchmarkCreatePatch_ArrayGrow(b *testing.B) {
original := makeArray(50)
modified := makeArray(100)
b.ResetTimer()
for b.Loop() {
_, _ = CreatePatch(original, modified)
}
}
func BenchmarkCreatePatch_ArrayShrink(b *testing.B) {
original := makeArray(100)
modified := makeArray(50)
b.ResetTimer()
for b.Loop() {
_, _ = CreatePatch(original, modified)
}
}
func BenchmarkCreatePatch_DeepNested(b *testing.B) {
depths := []int{5, 10, 20}
for _, d := range depths {
original := makeNestedObject(d)
// Change the innermost value.
var doc interface{}
_ = json.Unmarshal(original, &doc)
cur := doc
for i := range d {
m := cur.(map[string]interface{})
if i == d-1 {
m["a"] = "changed"
} else {
cur = m["a"]
}
}
modified, _ := json.Marshal(doc)
b.Run(fmt.Sprintf("depth_%d", d), func(b *testing.B) {
b.ResetTimer()
for b.Loop() {
_, _ = CreatePatch(original, modified)
}
})
}
}
// BenchmarkCreatePatch_RoundTrip measures diff + apply together.
func BenchmarkCreatePatch_RoundTrip(b *testing.B) {
original := makeObject(100)
modified := modifyObject(original)
b.ResetTimer()
for b.Loop() {
patch, _ := CreatePatch(original, modified)
_, _ = ApplyPatch(original, patch)
}
}
// ---------------------------------------------------------------------------
// Benchmark: DecodePatch
// ---------------------------------------------------------------------------
func BenchmarkDecodePatch_Small(b *testing.B) {
patchJSON := []byte(`[{"op":"add","path":"/foo","value":"bar"}]`)
b.ResetTimer()
for b.Loop() {
_, _ = DecodePatch(patchJSON)
}
}
func BenchmarkDecodePatch_Large(b *testing.B) {
counts := []int{10, 50, 100}
for _, n := range counts {
ops := make([]map[string]interface{}, n)
for i := range n {
ops[i] = map[string]interface{}{
"op": "add",
"path": "/key_" + strconv.Itoa(i),
"value": i,
}
}
patchJSON, _ := json.Marshal(ops)
b.Run(fmt.Sprintf("ops_%d", n), func(b *testing.B) {
b.ResetTimer()
for b.Loop() {
_, _ = DecodePatch(patchJSON)
}
})
}
}
// ---------------------------------------------------------------------------
// Benchmark: MarshalPatch
// ---------------------------------------------------------------------------
func BenchmarkMarshalPatch(b *testing.B) {
counts := []int{1, 10, 50}
for _, n := range counts {
ops := make(Patch, n)
for i := range n {
ops[i], _ = NewOperation(OpAdd, "/key_"+strconv.Itoa(i), "val")
}
b.Run(fmt.Sprintf("ops_%d", n), func(b *testing.B) {
b.ResetTimer()
for b.Loop() {
_, _ = MarshalPatch(ops)
}
})
}
}
// ---------------------------------------------------------------------------
// Benchmark: ParsePointer
// ---------------------------------------------------------------------------
func BenchmarkParsePointer_Simple(b *testing.B) {
for b.Loop() {
_, _ = ParsePointer("/foo/bar")
}
}
func BenchmarkParsePointer_Deep(b *testing.B) {
depths := []int{5, 10, 20}
for _, d := range depths {
ptr := ""
for i := range d {
ptr += "/token" + strconv.Itoa(i)
}
b.Run(fmt.Sprintf("depth_%d", d), func(b *testing.B) {
b.ResetTimer()
for b.Loop() {
_, _ = ParsePointer(ptr)
}
})
}
}
func BenchmarkParsePointer_WithEscapes(b *testing.B) {
// "~0" → "~", "~1" → "/"
for b.Loop() {
_, _ = ParsePointer("/foo~0bar/baz~1qux/deep~0~1path")
}
}
// ---------------------------------------------------------------------------
// Benchmark: Pointer.Evaluate
// ---------------------------------------------------------------------------
func BenchmarkPointerEvaluate(b *testing.B) {
doc := map[string]interface{}{
"a": map[string]interface{}{
"b": map[string]interface{}{
"c": "value",
},
},
}
ptr, _ := ParsePointer("/a/b/c")
b.ResetTimer()
for b.Loop() {
_, _ = ptr.Evaluate(doc)
}
}
// ---------------------------------------------------------------------------
// Benchmark: Pointer.Set
// ---------------------------------------------------------------------------
func BenchmarkPointerSet(b *testing.B) {
ptr, _ := ParsePointer("/a/b/c")
b.ResetTimer()
for b.Loop() {
doc := map[string]interface{}{
"a": map[string]interface{}{
"b": map[string]interface{}{
"c": "old",
},
},
}
_, _ = ptr.Set(doc, "new")
}
}
// ---------------------------------------------------------------------------
// Benchmark: Pointer.Remove
// ---------------------------------------------------------------------------
func BenchmarkPointerRemove(b *testing.B) {
ptr, _ := ParsePointer("/a/b/c")
b.ResetTimer()
for b.Loop() {
doc := map[string]interface{}{
"a": map[string]interface{}{
"b": map[string]interface{}{
"c": "value",
},
},
}
_, _ = ptr.Remove(doc)
}
}
// ---------------------------------------------------------------------------
// Benchmark: Operation.UnmarshalJSON
// ---------------------------------------------------------------------------
func BenchmarkOperationUnmarshal(b *testing.B) {
data := []byte(`{"op":"replace","path":"/foo/bar","value":{"nested":true}}`)
b.ResetTimer()
for b.Loop() {
var op Operation
_ = json.Unmarshal(data, &op)
}
}
// ---------------------------------------------------------------------------
// Benchmark: End-to-End realistic scenario
// ---------------------------------------------------------------------------
func BenchmarkEndToEnd_RealisticObject(b *testing.B) {
original := []byte(`{
"name": "John Doe",
"age": 30,
"email": "john@example.com",
"address": {
"street": "123 Main St",
"city": "Springfield",
"state": "IL",
"zip": "62701"
},
"phones": [
{"type": "home", "number": "555-1234"},
{"type": "work", "number": "555-5678"}
],
"tags": ["admin", "user"],
"active": true
}`)
modified := []byte(`{
"name": "John Doe",
"age": 31,
"email": "john.doe@newdomain.com",
"address": {
"street": "456 Oak Ave",
"city": "Springfield",
"state": "IL",
"zip": "62702"
},
"phones": [
{"type": "home", "number": "555-1234"},
{"type": "work", "number": "555-9999"},
{"type": "mobile", "number": "555-0000"}
],
"tags": ["admin", "user", "manager"],
"active": true,
"role": "supervisor"
}`)
b.Run("CreatePatch", func(b *testing.B) {
for b.Loop() {
_, _ = CreatePatch(original, modified)
}
})
b.Run("Apply", func(b *testing.B) {
patch, _ := CreatePatch(original, modified)
patchJSON, _ := MarshalPatch(patch)
b.ResetTimer()
for b.Loop() {
_, _ = Apply(original, patchJSON)
}
})
b.Run("RoundTrip", func(b *testing.B) {
for b.Loop() {
patch, _ := CreatePatch(original, modified)
_, _ = ApplyPatch(original, patch)
}
})
}