Skip to content

Commit 54ae25d

Browse files
committed
add more benchmark
1 parent 8b208a8 commit 54ae25d

2 files changed

Lines changed: 68 additions & 15 deletions

File tree

jsonpath_test.go

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package jsonpath
33
import (
44
"encoding/json"
55
"fmt"
6-
//"golang.org/x/tools/go/types"
76
"go/token"
87
"go/types"
98
"reflect"
@@ -55,18 +54,6 @@ func init() {
5554
json.Unmarshal([]byte(data), &json_data)
5655
}
5756

58-
func Benchmark_JsonPathLookup(b *testing.B) {
59-
for n := 0; n < b.N; n++ {
60-
res, err := JsonPathLookup(json_data, "$.store.book[0].price")
61-
if res_v, ok := res.(float64); ok != true || res_v != 8.95 {
62-
b.Errorf("$.store.book[0].price should be 8.95")
63-
}
64-
if err != nil {
65-
b.Errorf("Unexpected error: %v", err)
66-
}
67-
}
68-
}
69-
7057
func Test_jsonpath_JsonPathLookup_1(t *testing.T) {
7158
// key from root
7259
res, _ := JsonPathLookup(json_data, "$.expensive")
@@ -996,7 +983,73 @@ func Test_jsonpath_num_cmp(t *testing.T) {
996983
}
997984

998985
func BenchmarkJsonPathLookup(b *testing.B) {
986+
for n := 0; n < b.N; n++ {
987+
res, err := JsonPathLookup(json_data, "$.store.book[0].price")
988+
if res_v, ok := res.(float64); ok != true || res_v != 8.95 {
989+
b.Errorf("$.store.book[0].price should be 8.95")
990+
}
991+
if err != nil {
992+
b.Errorf("Unexpected error: %v", err)
993+
}
994+
}
995+
}
996+
997+
func BenchmarkJsonPathLookup_0(b *testing.B) {
998+
for i := 0; i < b.N; i++ {
999+
JsonPathLookup(json_data, "$.expensive")
1000+
}
1001+
}
1002+
1003+
func BenchmarkJsonPathLookup_1(b *testing.B) {
1004+
for i := 0; i < b.N; i++ {
1005+
JsonPathLookup(json_data, "$.store.book[0].price")
1006+
}
1007+
}
1008+
1009+
func BenchmarkJsonPathLookup_2(b *testing.B) {
1010+
for i := 0; i < b.N; i++ {
1011+
JsonPathLookup(json_data, "$.store.book[-1].price")
1012+
}
1013+
}
1014+
1015+
func BenchmarkJsonPathLookup_3(b *testing.B) {
1016+
for i := 0; i < b.N; i++ {
1017+
JsonPathLookup(json_data, "$.store.book[0,1].price")
1018+
}
1019+
}
1020+
1021+
func BenchmarkJsonPathLookup_4(b *testing.B) {
1022+
for i := 0; i < b.N; i++ {
1023+
JsonPathLookup(json_data, "$.store.book[0:2].price")
1024+
}
1025+
}
1026+
1027+
func BenchmarkJsonPathLookup_5(b *testing.B) {
1028+
for i := 0; i < b.N; i++ {
1029+
JsonPathLookup(json_data, "$.store.book[?(@.isbn)].price")
1030+
}
1031+
}
1032+
1033+
func BenchmarkJsonPathLookup_6(b *testing.B) {
1034+
for i := 0; i < b.N; i++ {
1035+
JsonPathLookup(json_data, "$.store.book[?(@.price > 10)].title")
1036+
}
1037+
}
1038+
1039+
func BenchmarkJsonPathLookup_7(b *testing.B) {
1040+
for i := 0; i < b.N; i++ {
1041+
JsonPathLookup(json_data, "$.store.book[?(@.price < $.expensive)].price")
1042+
}
1043+
}
1044+
1045+
func BenchmarkJsonPathLookup_8(b *testing.B) {
1046+
for i := 0; i < b.N; i++ {
1047+
JsonPathLookup(json_data, "$.store.book[:].price")
1048+
}
1049+
}
1050+
1051+
func BenchmarkJsonPathLookup_9(b *testing.B) {
9991052
for i := 0; i < b.N; i++ {
1000-
JsonPathLookup(json_data, "$.books[?(@.price > 20)].name")
1053+
JsonPathLookup(json_data, "$.store.book[?(@.author == 'Nigel Rees')].price")
10011054
}
10021055
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ example json path syntax.
100100
| $.store.book[-1].isbn | "0-395-19395-8"|
101101
| $.store.book[0,1].price | [8.95, 12.99] |
102102
| $.store.book[0:2].price | [8.95, 12.99, 8.99]|
103-
| $.store.book[?(@.isbn)].price| [8.99, 22.99] |
103+
| $.store.book[?(@.isbn)].price | [8.99, 22.99] |
104104
| $.store.book[?(@.price > 10)].title | ["Sword of Honour", "The Lord of the Rings"]|
105105
| $.store.book[?(@.price < $.expensive)].price | [8.95, 8.99] |
106106
| $.store.book[:].price | [8.9.5, 12.99, 8.9.9, 22.99] |

0 commit comments

Comments
 (0)