-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathjsonpath_bench_test.go
More file actions
195 lines (168 loc) · 4.61 KB
/
jsonpath_bench_test.go
File metadata and controls
195 lines (168 loc) · 4.61 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
package jsonpath
import "testing"
// Benchmarks for JsonPath lookup performance
func BenchmarkJsonPathLookupCompiled(b *testing.B) {
c, err := Compile("$.store.book[0].price")
if err != nil {
b.Fatalf("%v", err)
}
for n := 0; n < b.N; n++ {
res, err := c.Lookup(json_data)
if res_v, ok := res.(float64); ok != true || res_v != 8.95 {
b.Errorf("$.store.book[0].price should be 8.95")
}
if err != nil {
b.Errorf("Unexpected error: %v", err)
}
}
}
func BenchmarkJsonPathLookup(b *testing.B) {
for n := 0; n < b.N; n++ {
res, err := JsonPathLookup(json_data, "$.store.book[0].price")
if res_v, ok := res.(float64); ok != true || res_v != 8.95 {
b.Errorf("$.store.book[0].price should be 8.95")
}
if err != nil {
b.Errorf("Unexpected error: %v", err)
}
}
}
func BenchmarkJsonPathLookup_0(b *testing.B) {
for i := 0; i < b.N; i++ {
JsonPathLookup(json_data, "$.expensive")
}
}
func BenchmarkJsonPathLookup_1(b *testing.B) {
for i := 0; i < b.N; i++ {
JsonPathLookup(json_data, "$.store.book[0].price")
}
}
func BenchmarkJsonPathLookup_2(b *testing.B) {
for i := 0; i < b.N; i++ {
JsonPathLookup(json_data, "$.store.book[-1].price")
}
}
func BenchmarkJsonPathLookup_3(b *testing.B) {
for i := 0; i < b.N; i++ {
JsonPathLookup(json_data, "$.store.book[0,1].price")
}
}
func BenchmarkJsonPathLookup_4(b *testing.B) {
for i := 0; i < b.N; i++ {
JsonPathLookup(json_data, "$.store.book[0:2].price")
}
}
func BenchmarkJsonPathLookup_5(b *testing.B) {
for i := 0; i < b.N; i++ {
JsonPathLookup(json_data, "$.store.book[?(@.isbn)].price")
}
}
func BenchmarkJsonPathLookup_6(b *testing.B) {
for i := 0; i < b.N; i++ {
JsonPathLookup(json_data, "$.store.book[?(@.price > 10)].title")
}
}
func BenchmarkJsonPathLookup_7(b *testing.B) {
for i := 0; i < b.N; i++ {
JsonPathLookup(json_data, "$.store.book[?(@.price < $.expensive)].price")
}
}
func BenchmarkJsonPathLookup_8(b *testing.B) {
for i := 0; i < b.N; i++ {
JsonPathLookup(json_data, "$.store.book[:].price")
}
}
func BenchmarkJsonPathLookup_9(b *testing.B) {
for i := 0; i < b.N; i++ {
JsonPathLookup(json_data, "$.store.book[?(@.author == 'Nigel Rees')].price")
}
}
func BenchmarkJsonPathLookup_10(b *testing.B) {
for i := 0; i < b.N; i++ {
JsonPathLookup(json_data, "$.store.book[?(@.author =~ /(?i).*REES/)].price")
}
}
func BenchmarkJsonPathLookup_Simple(b *testing.B) {
data := map[string]interface{}{
"store": map[string]interface{}{
"book": []interface{}{
map[string]interface{}{"author": "A", "price": 10.0},
map[string]interface{}{"author": "B", "price": 20.0},
},
},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
JsonPathLookup(data, "$.store.book[0].author")
}
}
func BenchmarkJsonPathLookup_Filter(b *testing.B) {
data := map[string]interface{}{
"store": map[string]interface{}{
"book": []interface{}{
map[string]interface{}{"author": "A", "price": 10.0},
map[string]interface{}{"author": "B", "price": 20.0},
map[string]interface{}{"author": "C", "price": 30.0},
},
},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
JsonPathLookup(data, "$.store.book[?(@.price > 15)].author")
}
}
func BenchmarkJsonPathLookup_Range(b *testing.B) {
data := map[string]interface{}{
"store": map[string]interface{}{
"book": []interface{}{
map[string]interface{}{"author": "A", "price": 10.0},
map[string]interface{}{"author": "B", "price": 20.0},
map[string]interface{}{"author": "C", "price": 30.0},
},
},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
JsonPathLookup(data, "$.store.book[0:2].price")
}
}
func BenchmarkJsonPathLookup_Recursive(b *testing.B) {
data := map[string]interface{}{
"store": map[string]interface{}{
"book": []interface{}{
map[string]interface{}{"author": "A", "price": 10.0},
map[string]interface{}{"author": "B", "price": 20.0},
},
},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
JsonPathLookup(data, "$..author")
}
}
func BenchmarkJsonPathLookup_RootArrayFilter(b *testing.B) {
data := []interface{}{
map[string]interface{}{"name": "John", "age": 30},
map[string]interface{}{"name": "Jane", "age": 25},
map[string]interface{}{"name": "Bob", "age": 35},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
JsonPathLookup(data, "$[?(@.age > 25)]")
}
}
func BenchmarkCompileAndLookup(b *testing.B) {
data := map[string]interface{}{
"store": map[string]interface{}{
"book": []interface{}{
map[string]interface{}{"author": "A", "price": 10.0},
map[string]interface{}{"author": "B", "price": 20.0},
},
},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
c, _ := Compile("$.store.book[?(@.price > 10)].author")
c.Lookup(data)
}
}