Skip to content

Commit

Permalink
1. Adding comms to the in (#5)
Browse files Browse the repository at this point in the history
2. Adding the benchmarks
3. Removing console error strategy
  • Loading branch information
nikunjy authored May 11, 2019
1 parent 4379dbd commit 921ff6f
Show file tree
Hide file tree
Showing 10 changed files with 310 additions and 165 deletions.
8 changes: 5 additions & 3 deletions JsonQuery.g4
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ listStrings
;
subListOfStrings
: STRING SP subListOfStrings
: STRING COMMA subListOfStrings
| STRING ']';
fragment ESC
Expand All @@ -106,15 +106,15 @@ listDoubles
;
subListOfDoubles
: DOUBLE SP subListOfDoubles
: DOUBLE COMMA subListOfDoubles
| DOUBLE ']';
listInts
: '[' subListOfInts
;
subListOfInts
: INT SP subListOfInts
: INT COMMA subListOfInts
| INT ']';
// INT no leading zeros.
Expand All @@ -130,6 +130,8 @@ EXP
NEWLINE
: '\n' ;
COMMA
: ',' ' '*;
SP
: ' ' NEWLINE*
;
4 changes: 3 additions & 1 deletion parser/JsonQuery.interp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ null
null
'\n'
null
null

token symbolic names:
null
Expand Down Expand Up @@ -60,6 +61,7 @@ DOUBLE
INT
EXP
NEWLINE
COMMA
SP

rule names:
Expand All @@ -76,4 +78,4 @@ subListOfInts


atn:
[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 3, 31, 110, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 4, 10, 9, 10, 4, 11, 9, 11, 3, 2, 3, 2, 5, 2, 25, 10, 2, 3, 2, 5, 2, 28, 10, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 5, 2, 44, 10, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 7, 2, 51, 10, 2, 12, 2, 14, 2, 54, 11, 2, 3, 3, 3, 3, 5, 3, 58, 10, 3, 3, 4, 3, 4, 3, 4, 3, 5, 3, 5, 3, 5, 3, 5, 3, 5, 3, 5, 5, 5, 69, 10, 5, 3, 5, 3, 5, 5, 5, 73, 10, 5, 3, 5, 3, 5, 3, 5, 5, 5, 78, 10, 5, 3, 6, 3, 6, 3, 6, 3, 7, 3, 7, 3, 7, 3, 7, 3, 7, 5, 7, 88, 10, 7, 3, 8, 3, 8, 3, 8, 3, 9, 3, 9, 3, 9, 3, 9, 3, 9, 5, 9, 98, 10, 9, 3, 10, 3, 10, 3, 10, 3, 11, 3, 11, 3, 11, 3, 11, 3, 11, 5, 11, 108, 10, 11, 3, 11, 2, 3, 2, 12, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 2, 3, 3, 2, 14, 23, 2, 118, 2, 43, 3, 2, 2, 2, 4, 55, 3, 2, 2, 2, 6, 59, 3, 2, 2, 2, 8, 77, 3, 2, 2, 2, 10, 79, 3, 2, 2, 2, 12, 87, 3, 2, 2, 2, 14, 89, 3, 2, 2, 2, 16, 97, 3, 2, 2, 2, 18, 99, 3, 2, 2, 2, 20, 107, 3, 2, 2, 2, 22, 24, 8, 2, 1, 2, 23, 25, 7, 10, 2, 2, 24, 23, 3, 2, 2, 2, 24, 25, 3, 2, 2, 2, 25, 27, 3, 2, 2, 2, 26, 28, 7, 31, 2, 2, 27, 26, 3, 2, 2, 2, 27, 28, 3, 2, 2, 2, 28, 29, 3, 2, 2, 2, 29, 30, 7, 3, 2, 2, 30, 31, 5, 2, 2, 2, 31, 32, 7, 4, 2, 2, 32, 44, 3, 2, 2, 2, 33, 34, 5, 4, 3, 2, 34, 35, 7, 31, 2, 2, 35, 36, 7, 5, 2, 2, 36, 44, 3, 2, 2, 2, 37, 38, 5, 4, 3, 2, 38, 39, 7, 31, 2, 2, 39, 40, 9, 2, 2, 2, 40, 41, 7, 31, 2, 2, 41, 42, 5, 8, 5, 2, 42, 44, 3, 2, 2, 2, 43, 22, 3, 2, 2, 2, 43, 33, 3, 2, 2, 2, 43, 37, 3, 2, 2, 2, 44, 52, 3, 2, 2, 2, 45, 46, 12, 5, 2, 2, 46, 47, 7, 31, 2, 2, 47, 48, 7, 11, 2, 2, 48, 49, 7, 31, 2, 2, 49, 51, 5, 2, 2, 6, 50, 45, 3, 2, 2, 2, 51, 54, 3, 2, 2, 2, 52, 50, 3, 2, 2, 2, 52, 53, 3, 2, 2, 2, 53, 3, 3, 2, 2, 2, 54, 52, 3, 2, 2, 2, 55, 57, 7, 24, 2, 2, 56, 58, 5, 6, 4, 2, 57, 56, 3, 2, 2, 2, 57, 58, 3, 2, 2, 2, 58, 5, 3, 2, 2, 2, 59, 60, 7, 6, 2, 2, 60, 61, 5, 4, 3, 2, 61, 7, 3, 2, 2, 2, 62, 78, 7, 12, 2, 2, 63, 78, 7, 13, 2, 2, 64, 78, 7, 25, 2, 2, 65, 78, 7, 26, 2, 2, 66, 78, 7, 27, 2, 2, 67, 69, 7, 7, 2, 2, 68, 67, 3, 2, 2, 2, 68, 69, 3, 2, 2, 2, 69, 70, 3, 2, 2, 2, 70, 72, 7, 28, 2, 2, 71, 73, 7, 29, 2, 2, 72, 71, 3, 2, 2, 2, 72, 73, 3, 2, 2, 2, 73, 78, 3, 2, 2, 2, 74, 78, 5, 18, 10, 2, 75, 78, 5, 14, 8, 2, 76, 78, 5, 10, 6, 2, 77, 62, 3, 2, 2, 2, 77, 63, 3, 2, 2, 2, 77, 64, 3, 2, 2, 2, 77, 65, 3, 2, 2, 2, 77, 66, 3, 2, 2, 2, 77, 68, 3, 2, 2, 2, 77, 74, 3, 2, 2, 2, 77, 75, 3, 2, 2, 2, 77, 76, 3, 2, 2, 2, 78, 9, 3, 2, 2, 2, 79, 80, 7, 8, 2, 2, 80, 81, 5, 12, 7, 2, 81, 11, 3, 2, 2, 2, 82, 83, 7, 26, 2, 2, 83, 84, 7, 31, 2, 2, 84, 88, 5, 12, 7, 2, 85, 86, 7, 26, 2, 2, 86, 88, 7, 9, 2, 2, 87, 82, 3, 2, 2, 2, 87, 85, 3, 2, 2, 2, 88, 13, 3, 2, 2, 2, 89, 90, 7, 8, 2, 2, 90, 91, 5, 16, 9, 2, 91, 15, 3, 2, 2, 2, 92, 93, 7, 27, 2, 2, 93, 94, 7, 31, 2, 2, 94, 98, 5, 16, 9, 2, 95, 96, 7, 27, 2, 2, 96, 98, 7, 9, 2, 2, 97, 92, 3, 2, 2, 2, 97, 95, 3, 2, 2, 2, 98, 17, 3, 2, 2, 2, 99, 100, 7, 8, 2, 2, 100, 101, 5, 20, 11, 2, 101, 19, 3, 2, 2, 2, 102, 103, 7, 28, 2, 2, 103, 104, 7, 31, 2, 2, 104, 108, 5, 20, 11, 2, 105, 106, 7, 28, 2, 2, 106, 108, 7, 9, 2, 2, 107, 102, 3, 2, 2, 2, 107, 105, 3, 2, 2, 2, 108, 21, 3, 2, 2, 2, 13, 24, 27, 43, 52, 57, 68, 72, 77, 87, 97, 107]
[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 3, 32, 110, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 4, 10, 9, 10, 4, 11, 9, 11, 3, 2, 3, 2, 5, 2, 25, 10, 2, 3, 2, 5, 2, 28, 10, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 5, 2, 44, 10, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 7, 2, 51, 10, 2, 12, 2, 14, 2, 54, 11, 2, 3, 3, 3, 3, 5, 3, 58, 10, 3, 3, 4, 3, 4, 3, 4, 3, 5, 3, 5, 3, 5, 3, 5, 3, 5, 3, 5, 5, 5, 69, 10, 5, 3, 5, 3, 5, 5, 5, 73, 10, 5, 3, 5, 3, 5, 3, 5, 5, 5, 78, 10, 5, 3, 6, 3, 6, 3, 6, 3, 7, 3, 7, 3, 7, 3, 7, 3, 7, 5, 7, 88, 10, 7, 3, 8, 3, 8, 3, 8, 3, 9, 3, 9, 3, 9, 3, 9, 3, 9, 5, 9, 98, 10, 9, 3, 10, 3, 10, 3, 10, 3, 11, 3, 11, 3, 11, 3, 11, 3, 11, 5, 11, 108, 10, 11, 3, 11, 2, 3, 2, 12, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 2, 3, 3, 2, 14, 23, 2, 118, 2, 43, 3, 2, 2, 2, 4, 55, 3, 2, 2, 2, 6, 59, 3, 2, 2, 2, 8, 77, 3, 2, 2, 2, 10, 79, 3, 2, 2, 2, 12, 87, 3, 2, 2, 2, 14, 89, 3, 2, 2, 2, 16, 97, 3, 2, 2, 2, 18, 99, 3, 2, 2, 2, 20, 107, 3, 2, 2, 2, 22, 24, 8, 2, 1, 2, 23, 25, 7, 10, 2, 2, 24, 23, 3, 2, 2, 2, 24, 25, 3, 2, 2, 2, 25, 27, 3, 2, 2, 2, 26, 28, 7, 32, 2, 2, 27, 26, 3, 2, 2, 2, 27, 28, 3, 2, 2, 2, 28, 29, 3, 2, 2, 2, 29, 30, 7, 3, 2, 2, 30, 31, 5, 2, 2, 2, 31, 32, 7, 4, 2, 2, 32, 44, 3, 2, 2, 2, 33, 34, 5, 4, 3, 2, 34, 35, 7, 32, 2, 2, 35, 36, 7, 5, 2, 2, 36, 44, 3, 2, 2, 2, 37, 38, 5, 4, 3, 2, 38, 39, 7, 32, 2, 2, 39, 40, 9, 2, 2, 2, 40, 41, 7, 32, 2, 2, 41, 42, 5, 8, 5, 2, 42, 44, 3, 2, 2, 2, 43, 22, 3, 2, 2, 2, 43, 33, 3, 2, 2, 2, 43, 37, 3, 2, 2, 2, 44, 52, 3, 2, 2, 2, 45, 46, 12, 5, 2, 2, 46, 47, 7, 32, 2, 2, 47, 48, 7, 11, 2, 2, 48, 49, 7, 32, 2, 2, 49, 51, 5, 2, 2, 6, 50, 45, 3, 2, 2, 2, 51, 54, 3, 2, 2, 2, 52, 50, 3, 2, 2, 2, 52, 53, 3, 2, 2, 2, 53, 3, 3, 2, 2, 2, 54, 52, 3, 2, 2, 2, 55, 57, 7, 24, 2, 2, 56, 58, 5, 6, 4, 2, 57, 56, 3, 2, 2, 2, 57, 58, 3, 2, 2, 2, 58, 5, 3, 2, 2, 2, 59, 60, 7, 6, 2, 2, 60, 61, 5, 4, 3, 2, 61, 7, 3, 2, 2, 2, 62, 78, 7, 12, 2, 2, 63, 78, 7, 13, 2, 2, 64, 78, 7, 25, 2, 2, 65, 78, 7, 26, 2, 2, 66, 78, 7, 27, 2, 2, 67, 69, 7, 7, 2, 2, 68, 67, 3, 2, 2, 2, 68, 69, 3, 2, 2, 2, 69, 70, 3, 2, 2, 2, 70, 72, 7, 28, 2, 2, 71, 73, 7, 29, 2, 2, 72, 71, 3, 2, 2, 2, 72, 73, 3, 2, 2, 2, 73, 78, 3, 2, 2, 2, 74, 78, 5, 18, 10, 2, 75, 78, 5, 14, 8, 2, 76, 78, 5, 10, 6, 2, 77, 62, 3, 2, 2, 2, 77, 63, 3, 2, 2, 2, 77, 64, 3, 2, 2, 2, 77, 65, 3, 2, 2, 2, 77, 66, 3, 2, 2, 2, 77, 68, 3, 2, 2, 2, 77, 74, 3, 2, 2, 2, 77, 75, 3, 2, 2, 2, 77, 76, 3, 2, 2, 2, 78, 9, 3, 2, 2, 2, 79, 80, 7, 8, 2, 2, 80, 81, 5, 12, 7, 2, 81, 11, 3, 2, 2, 2, 82, 83, 7, 26, 2, 2, 83, 84, 7, 31, 2, 2, 84, 88, 5, 12, 7, 2, 85, 86, 7, 26, 2, 2, 86, 88, 7, 9, 2, 2, 87, 82, 3, 2, 2, 2, 87, 85, 3, 2, 2, 2, 88, 13, 3, 2, 2, 2, 89, 90, 7, 8, 2, 2, 90, 91, 5, 16, 9, 2, 91, 15, 3, 2, 2, 2, 92, 93, 7, 27, 2, 2, 93, 94, 7, 31, 2, 2, 94, 98, 5, 16, 9, 2, 95, 96, 7, 27, 2, 2, 96, 98, 7, 9, 2, 2, 97, 92, 3, 2, 2, 2, 97, 95, 3, 2, 2, 2, 98, 17, 3, 2, 2, 2, 99, 100, 7, 8, 2, 2, 100, 101, 5, 20, 11, 2, 101, 19, 3, 2, 2, 2, 102, 103, 7, 28, 2, 2, 103, 104, 7, 31, 2, 2, 104, 108, 5, 20, 11, 2, 105, 106, 7, 28, 2, 2, 106, 108, 7, 9, 2, 2, 107, 102, 3, 2, 2, 2, 107, 105, 3, 2, 2, 2, 108, 21, 3, 2, 2, 2, 13, 24, 27, 43, 52, 57, 68, 72, 77, 87, 97, 107]
3 changes: 2 additions & 1 deletion parser/JsonQuery.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ DOUBLE=25
INT=26
EXP=27
NEWLINE=28
SP=29
COMMA=29
SP=30
'('=1
')'=2
'pr'=3
Expand Down
5 changes: 4 additions & 1 deletion parser/JsonQueryLexer.interp

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion parser/JsonQueryLexer.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ DOUBLE=25
INT=26
EXP=27
NEWLINE=28
SP=29
COMMA=29
SP=30
'('=1
')'=2
'pr'=3
Expand Down
122 changes: 122 additions & 0 deletions parser/bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package parser

import (
"fmt"
"testing"
)

var conditions = []string{
`x eq 1`,
`y IN [1, 2, 3, 4, 5]`,
`z in ["abcd", "efghijkl", "asdoihadoihasoihsaoasihasdoihdadoiahsdoihdasodha", "asdoisahdadoihasdoisdwoqeioheqcnac"]`,
`(a > 123 or b <= 456)`,
`(aa == 123123131.1232313131312312 or bb == 1231.1312313123123131231)`,
`(jh gt "1.2.3" or kl le "1.4.56")`,
}

func BenchmarkLogicalExpression(b *testing.B) {
rule := ""
for i, condition := range conditions {
if i == len(conditions)-1 {
rule += (" " + condition)
continue
}
rule += fmt.Sprintf("%s or ", condition)
}
input := map[string]interface{}{
"x": 123,
"y": 2345,
"z": "asdada",
"a": "1212121",
"b": 123131,
"aa": 123123.123131232113,
"jh": "1.2.3",
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
ev, _ := NewEvaluator(rule)
ev.Process(input)
}
}

func BenchmarkLogicalExpressionBig(b *testing.B) {
rule := ""
for j := 0; j < 100; j++ {
for i, condition := range conditions {
if i == len(conditions)-1 {
rule += fmt.Sprintf(" (((%s)))", condition)
continue
}
rule += fmt.Sprintf("(((%s))) or ", condition)
}
}
input := map[string]interface{}{
"x": 123,
"y": 2345,
"z": "asdada",
"a": "1212121",
"b": 123131,
"aa": 123123.123131232113,
"jh": "1.2.3",
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
ev, _ := NewEvaluator(rule)
ev.Process(input)
}

}

func BenchmarkLogicalExpressionBigTreeCached(b *testing.B) {
rule := ""
for j := 0; j < 100; j++ {
for i, condition := range conditions {
if i == len(conditions)-1 {
rule += fmt.Sprintf(" (((%s)))", condition)
continue
}
rule += fmt.Sprintf("(((%s))) or ", condition)
}
}
input := map[string]interface{}{
"x": 123,
"y": 2345,
"z": "asdada",
"a": "1212121",
"b": 123131,
"aa": 123123.123131232113,
"jh": "1.2.3",
}
b.ResetTimer()
ev, _ := NewEvaluator(rule)
for i := 0; i < b.N; i++ {
ev.Process(input)
ev.Reset()
}

}

func BenchmarkLogicalExpressionNested(b *testing.B) {
rule := ""
for i, condition := range conditions {
if i == len(conditions)-1 {
rule += fmt.Sprintf(" (((%s)))", condition)
continue
}
rule += fmt.Sprintf("(((%s))) or ", condition)
}
input := map[string]interface{}{
"x": 123,
"y": 2345,
"z": "asdada",
"a": "1212121",
"b": 123131,
"aa": 123123.123131232113,
"jh": "1.2.3",
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
ev, _ := NewEvaluator(rule)
ev.Process(input)
}
}
8 changes: 8 additions & 0 deletions parser/evaluate.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ func NewEvaluator(rule string) (ret *Evaluator, retErr error) {
}()
input := antlr.NewInputStream(rule)
lex := NewJsonQueryLexer(input)
lex.RemoveErrorListeners()
tokens := antlr.NewCommonTokenStream(lex, antlr.TokenDefaultChannel)
p := NewJsonQueryParser(tokens)
// TODO: maybe log properly
p.RemoveErrorListeners()
tree := p.Query()

return &Evaluator{
Expand All @@ -36,6 +39,11 @@ func NewEvaluator(rule string) (ret *Evaluator, retErr error) {
}, nil
}

func (e *Evaluator) Reset() error {
e.lastDebugErr = nil
return nil
}

func (e *Evaluator) LastDebugErr() error {
return e.lastDebugErr
}
Expand Down
Loading

0 comments on commit 921ff6f

Please sign in to comment.