Repository navigation
Expand file tree
/
Copy pathparse_benchmark_test.go
More file actions
206 lines (183 loc) · 4.75 KB
/
Copy pathparse_benchmark_test.go
File metadata and controls
206 lines (183 loc) · 4.75 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
package jsonpath
import (
"encoding/json/v2"
"testing"
)
var benchmarkData map[string]any
func init() {
err := json.Unmarshal([]byte(`{
"store": {
"book": [
{"category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95},
{"category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honor", "price": 12.99},
{"category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99},
{"category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99}
],
"bicycle": {"color": "red", "price": 19.95}
},
"expensive": 10
}`), &benchmarkData)
if err != nil {
panic(err)
}
}
// benchmarkJSON is the same document as benchmarkData, kept as raw bytes so the
// decode step callers must perform is measured too. The library itself never
// touches encoding/json - it consumes already-decoded values - so these two
// benchmarks are what a json/v2 change actually moves.
var benchmarkJSON = []byte(`{
"store": {
"book": [
{"category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95},
{"category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honor", "price": 12.99},
{"category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99},
{"category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99}
],
"bicycle": {"color": "red", "price": 19.95}
},
"expensive": 10
}`)
func BenchmarkUnmarshal(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
var v map[string]any
if err := json.Unmarshal(benchmarkJSON, &v); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkUnmarshalThenApply(b *testing.B) {
a, err := Parse("$.store.book[?(@.price < 10)]")
if err != nil {
b.Fatal(err)
}
b.ReportAllocs()
for b.Loop() {
var v map[string]any
if err := json.Unmarshal(benchmarkJSON, &v); err != nil {
b.Fatal(err)
}
if _, err := a.Apply(v); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkParse(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_, _ = Parse("$.store.book[0].title")
}
}
func BenchmarkParseSimplePath(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_, _ = Parse("$.store.bicycle.color")
}
}
func BenchmarkParseCached(b *testing.B) {
// Pre-warm the cache
_, _ = Parse("$.store.book.title")
b.ReportAllocs()
for b.Loop() {
_, _ = Parse("$.store.book.title")
}
}
func BenchmarkParseSimplePathUncached(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
// Use unique paths to avoid cache hits
_ = parseSimpleDotPath("$.store.bicycle.color")
}
}
func BenchmarkParseNoCache(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_, _ = ParseNoCache("$.store.bicycle.color")
}
}
func BenchmarkNormalize(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_, _ = normalize("$.store.book[0].title")
}
}
func BenchmarkApplySimple(b *testing.B) {
a, _ := Parse("$.store.bicycle.color")
b.ReportAllocs()
for b.Loop() {
_, _ = a.Apply(benchmarkData)
}
}
func BenchmarkApplyArrayIndex(b *testing.B) {
a, _ := Parse("$.store.book[0].title")
b.ReportAllocs()
for b.Loop() {
_, _ = a.Apply(benchmarkData)
}
}
func BenchmarkApplyWildcard(b *testing.B) {
a, _ := Parse("$.store.book[*].author")
b.ReportAllocs()
for b.Loop() {
_, _ = a.Apply(benchmarkData)
}
}
func BenchmarkApplyDescent(b *testing.B) {
a, _ := Parse("$..author")
b.ReportAllocs()
for b.Loop() {
_, _ = a.Apply(benchmarkData)
}
}
func BenchmarkApplyFilterNumeric(b *testing.B) {
a, _ := Parse("$.store.book[?(@.price < 10)]")
b.ReportAllocs()
for b.Loop() {
_, _ = a.Apply(benchmarkData)
}
}
func BenchmarkApplyFilterString(b *testing.B) {
a, _ := Parse("$.store.book[?(@.category == fiction)]")
b.ReportAllocs()
for b.Loop() {
_, _ = a.Apply(benchmarkData)
}
}
func BenchmarkApplyFilterRegex(b *testing.B) {
a, _ := Parse("$.store.book[?(@.author =~ 'J.*')]")
b.ReportAllocs()
for b.Loop() {
_, _ = a.Apply(benchmarkData)
}
}
func BenchmarkCmpAny(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_, _ = cmp_any(8.95, "10", "<")
}
}
func BenchmarkCmpWildcard(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_, _ = cmp_wildcard("J. R. R. Tolkien", "J.*", "=~")
}
}
// Benchmark with larger dataset to show filter performance impact
func BenchmarkApplyFilterLargeArray(b *testing.B) {
// Create a large array of items
largeData := make(map[string]any)
items := make([]any, 100)
for i := range 100 {
items[i] = map[string]any{
"id": i,
"price": float64(i * 10),
"name": "item",
}
}
largeData["items"] = items
a, _ := Parse("$.items[?(@.price < 500)]")
b.ReportAllocs()
for b.Loop() {
_, _ = a.Apply(largeData)
}
}