Skip to content

Commit

Permalink
Add rudimentary parsing benchmark (#503)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcking authored Mar 8, 2022
1 parent a22969d commit 7200247
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ var (
)

func (p *parser) parse(expr runes.Buffer, desc string) *exprpb.Expr {
// TODO: get rid of these pools once https://github.com/antlr/antlr4/pull/3571 is in a release
lexer := lexerPool.Get().(*gen.CELLexer)
prsr := parserPool.Get().(*gen.CELParser)

Expand Down
40 changes: 40 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1750,3 +1750,43 @@ func TestParserOptionErrors(t *testing.T) {
t.Fatalf("got %q, want %q", err, "expression size code point limit must be greater than or equal to -1: -2")
}
}

func BenchmarkParse(b *testing.B) {
p, err := NewParser(
Macros(AllMacros...),
MaxRecursionDepth(32),
ErrorRecoveryLimit(4),
ErrorRecoveryLookaheadTokenLimit(4),
PopulateMacroCalls(true),
)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
for _, testCase := range testCases {
p.Parse(common.NewTextSource(testCase.I))
}
}
}

func BenchmarkParseParallel(b *testing.B) {
p, err := NewParser(
Macros(AllMacros...),
MaxRecursionDepth(32),
ErrorRecoveryLimit(4),
ErrorRecoveryLookaheadTokenLimit(4),
PopulateMacroCalls(true),
)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
for _, testCase := range testCases {
p.Parse(common.NewTextSource(testCase.I))
}
}
})
}

0 comments on commit 7200247

Please sign in to comment.