Skip to content

Commit 96598ba

Browse files
Merge pull request #16 from evilmonkeyinc/refactor/reduce-loops
Refactor/reduce loops
2 parents 542b5d3 + f7ae1e0 commit 96598ba

20 files changed

+2601
-2488
lines changed

benchmark/README.md

Lines changed: 69 additions & 89 deletions
Large diffs are not rendered by default.

benchmark/go.mod

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,12 @@ go 1.17
55
require (
66
github.com/PaesslerAG/jsonpath v0.1.1
77
github.com/bhmj/jsonslice v1.1.2
8-
github.com/evilmonkeyinc/jsonpath v0.7.0
8+
github.com/evilmonkeyinc/jsonpath v0.7.2
99
github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852
1010
github.com/spyzhov/ajson v0.7.0
11-
github.com/stretchr/testify v1.7.0
1211
)
1312

1413
require (
1514
github.com/PaesslerAG/gval v1.0.0 // indirect
1615
github.com/bhmj/xpression v0.9.1 // indirect
17-
github.com/davecgh/go-spew v1.1.0 // indirect
18-
github.com/pmezard/go-difflib v1.0.0 // indirect
19-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
2016
)

benchmark/go.sum

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ github.com/bhmj/xpression v0.9.1 h1:N7bX/nWx9oFi/zsiMTx2ehoRApTDAWdQadq/5o2wMGk=
99
github.com/bhmj/xpression v0.9.1/go.mod h1:j9oYmEXJjeL9mrgW1+ZDBKJXnbupsCPGhlO9J5YhS1Q=
1010
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
1111
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
12-
github.com/evilmonkeyinc/jsonpath v0.7.0 h1:jHar8KiQNNhobwKpftn5SzqaVGbGbNb+pWm+BaRQiQw=
13-
github.com/evilmonkeyinc/jsonpath v0.7.0/go.mod h1:exI6Yme6vucFk2m/1iVtxPaHGI17Dy9uWfsqbJ4iGHM=
12+
github.com/evilmonkeyinc/jsonpath v0.7.2 h1:QfnIN8jFo3JOQNLkwhuAg8uPJbJz4NcCV+LRPCLLuoQ=
13+
github.com/evilmonkeyinc/jsonpath v0.7.2/go.mod h1:exI6Yme6vucFk2m/1iVtxPaHGI17Dy9uWfsqbJ4iGHM=
1414
github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852 h1:Yl0tPBa8QPjGmesFh1D0rDy+q1Twx6FyU7VWHi8wZbI=
1515
github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852/go.mod h1:eqOVx5Vwu4gd2mmMZvVZsgIqNSaW3xxRThUJ0k/TPk4=
1616
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -20,7 +20,6 @@ github.com/spyzhov/ajson v0.7.0/go.mod h1:63V+CGM6f1Bu/p4nLIN8885ojBdt88TbLoSFzy
2020
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
2121
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
2222
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
23-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
2423
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2524
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
2625
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

jsonpath_test.go

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
)
99

1010
func Benchmark_Selector(b *testing.B) {
11-
1211
selectors := []string{
1312
"$.store.book[*].author",
1413
"$..author",
@@ -25,17 +24,62 @@ func Benchmark_Selector(b *testing.B) {
2524
"$..*",
2625
}
2726

28-
for _, selector := range selectors {
29-
b.Run(fmt.Sprintf("%s", selector), func(b *testing.B) {
30-
var err error
31-
for i := 0; i < b.N; i++ {
32-
_, err = QueryString(selector, sampleDataString)
33-
if err != nil {
34-
b.Error()
27+
b.Run("QueryString", func(b *testing.B) {
28+
for _, selector := range selectors {
29+
b.Run(fmt.Sprintf("%s", selector), func(b *testing.B) {
30+
var err error
31+
for i := 0; i < b.N; i++ {
32+
_, err = QueryString(selector, sampleDataString)
33+
if err != nil {
34+
b.Error()
35+
}
3536
}
36-
}
37-
})
38-
}
37+
})
38+
}
39+
})
40+
41+
compiledSelectors := make([]*Selector, 0)
42+
b.Run("Compile", func(b *testing.B) {
43+
for _, selector := range selectors {
44+
b.Run(fmt.Sprintf("%s", selector), func(b *testing.B) {
45+
var err error
46+
var compiled *Selector
47+
for i := 0; i < b.N; i++ {
48+
compiled, err = Compile(selector)
49+
if err != nil {
50+
b.Error()
51+
}
52+
}
53+
compiledSelectors = append(compiledSelectors, compiled)
54+
})
55+
}
56+
})
57+
58+
b.Run("Selector.QueryString", func(b *testing.B) {
59+
for _, selector := range compiledSelectors {
60+
b.Run(fmt.Sprintf("%s", selector.selector), func(b *testing.B) {
61+
for i := 0; i < b.N; i++ {
62+
_, err := selector.QueryString(sampleDataString)
63+
if err != nil {
64+
b.Error()
65+
}
66+
}
67+
})
68+
}
69+
})
70+
71+
b.Run("Selector.Query", func(b *testing.B) {
72+
for _, selector := range compiledSelectors {
73+
b.Run(fmt.Sprintf("%s", selector.selector), func(b *testing.B) {
74+
for i := 0; i < b.N; i++ {
75+
_, err := selector.Query(sampleDataObject)
76+
if err != nil {
77+
b.Error()
78+
}
79+
}
80+
})
81+
}
82+
})
3983
}
4084

4185
// Tests designed after the examples in the specification document

token/current_test.go

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,39 +21,42 @@ func Test_CurrentToken_Type(t *testing.T) {
2121
assert.Equal(t, "current", (&currentToken{}).Type())
2222
}
2323

24-
func Test_CurrentToken_Apply(t *testing.T) {
25-
26-
tests := []*tokenTest{
27-
{
28-
token: &currentToken{},
29-
input: input{
30-
current: map[string]interface{}{
31-
"name": "first",
32-
},
24+
var currentTests = []*tokenTest{
25+
{
26+
token: &currentToken{},
27+
input: input{
28+
current: map[string]interface{}{
29+
"name": "first",
3330
},
34-
expected: expected{
35-
value: map[string]interface{}{
36-
"name": "first",
37-
},
31+
},
32+
expected: expected{
33+
value: map[string]interface{}{
34+
"name": "first",
3835
},
3936
},
40-
{
41-
token: &currentToken{},
42-
input: input{
43-
current: map[string]interface{}{
44-
"name": "first",
45-
},
46-
tokens: []Token{
47-
&keyToken{
48-
key: "name",
49-
},
50-
},
37+
},
38+
{
39+
token: &currentToken{},
40+
input: input{
41+
current: map[string]interface{}{
42+
"name": "first",
5143
},
52-
expected: expected{
53-
value: "first",
44+
tokens: []Token{
45+
&keyToken{
46+
key: "name",
47+
},
5448
},
5549
},
56-
}
50+
expected: expected{
51+
value: "first",
52+
},
53+
},
54+
}
55+
56+
func Test_CurrentToken_Apply(t *testing.T) {
57+
batchTokenTests(t, currentTests)
58+
}
5759

58-
batchTokenTests(t, tests)
60+
func Benchmark_CurrentToken_Apply(b *testing.B) {
61+
batchTokenBenchmarks(b, currentTests)
5962
}

token/expression_test.go

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -72,49 +72,52 @@ func Test_ExpressionToken_Type(t *testing.T) {
7272
assert.Equal(t, "expression", (&expressionToken{}).Type())
7373
}
7474

75-
func Test_ExpressionToken_Apply(t *testing.T) {
76-
77-
tests := []*tokenTest{
78-
{
79-
token: &expressionToken{},
80-
input: input{},
81-
expected: expected{
82-
err: "invalid expression. is empty",
83-
},
75+
var expressionTests = []*tokenTest{
76+
{
77+
token: &expressionToken{},
78+
input: input{},
79+
expected: expected{
80+
err: "invalid expression. is empty",
8481
},
85-
{
86-
token: &expressionToken{
87-
expression: "any",
88-
compiledExpression: &testCompiledExpression{err: fmt.Errorf("engine error")},
89-
},
90-
input: input{},
91-
expected: expected{
92-
err: "invalid expression. engine error",
93-
},
82+
},
83+
{
84+
token: &expressionToken{
85+
expression: "any",
86+
compiledExpression: &testCompiledExpression{err: fmt.Errorf("engine error")},
9487
},
95-
{
96-
token: &expressionToken{
97-
expression: "any",
98-
compiledExpression: &testCompiledExpression{response: true},
99-
},
100-
input: input{},
101-
expected: expected{
102-
value: true,
103-
},
88+
input: input{},
89+
expected: expected{
90+
err: "invalid expression. engine error",
10491
},
105-
{
106-
token: &expressionToken{
107-
expression: "any",
108-
compiledExpression: &testCompiledExpression{response: false},
109-
},
110-
input: input{
111-
tokens: []Token{&currentToken{}},
112-
},
113-
expected: expected{
114-
value: false,
115-
},
92+
},
93+
{
94+
token: &expressionToken{
95+
expression: "any",
96+
compiledExpression: &testCompiledExpression{response: true},
11697
},
117-
}
98+
input: input{},
99+
expected: expected{
100+
value: true,
101+
},
102+
},
103+
{
104+
token: &expressionToken{
105+
expression: "any",
106+
compiledExpression: &testCompiledExpression{response: false},
107+
},
108+
input: input{
109+
tokens: []Token{&currentToken{}},
110+
},
111+
expected: expected{
112+
value: false,
113+
},
114+
},
115+
}
116+
117+
func Test_ExpressionToken_Apply(t *testing.T) {
118+
batchTokenTests(t, expressionTests)
119+
}
118120

119-
batchTokenTests(t, tests)
121+
func Benchmark_ExpressionToken_Apply(b *testing.B) {
122+
batchTokenBenchmarks(b, expressionTests)
120123
}

0 commit comments

Comments
 (0)