Skip to content

Commit

Permalink
add tests and benchmarks of expression semantics check based on real …
Browse files Browse the repository at this point in the history
…world inputs
  • Loading branch information
rhysd committed Aug 5, 2021
1 parent d320852 commit 0440fe4
Show file tree
Hide file tree
Showing 3 changed files with 1,978 additions and 4 deletions.
8 changes: 4 additions & 4 deletions expr_sema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ func TestExprSemanticsCheckOK(t *testing.T) {
t.Fatal("Parse error:", tc.input)
}

c := NewExprSemanticsChecker()
c := NewExprSemanticsChecker(false)
if tc.funcs != nil {
c.funcs = tc.funcs
}
Expand Down Expand Up @@ -1001,7 +1001,7 @@ func TestExprSemanticsCheckError(t *testing.T) {
t.Fatal("Parse error:", tc.input)
}

c := NewExprSemanticsChecker()
c := NewExprSemanticsChecker(false)
if tc.funcs != nil {
c.funcs = tc.funcs // Set functions for testing
}
Expand Down Expand Up @@ -1032,7 +1032,7 @@ func TestExprSemanticsCheckError(t *testing.T) {
}

func TestExprSemanticsCheckerUpdateMatrix(t *testing.T) {
c := NewExprSemanticsChecker()
c := NewExprSemanticsChecker(false)
ty := NewObjectType()
prev := c.vars["matrix"]
c.UpdateMatrix(ty)
Expand All @@ -1047,7 +1047,7 @@ func TestExprSemanticsCheckerUpdateMatrix(t *testing.T) {
}

func TestExprSemanticsCheckerUpdateSteps(t *testing.T) {
c := NewExprSemanticsChecker()
c := NewExprSemanticsChecker(false)
ty := NewObjectType()
prev := c.vars["steps"]
c.UpdateSteps(ty)
Expand Down
116 changes: 116 additions & 0 deletions expr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package actionlint

import (
"bufio"
"os"
"path/filepath"
"testing"
)

func TestExprSemanticsCheckRealWorld(t *testing.T) {
f, err := os.Open(filepath.Join("testdata", "bench", "expressions.txt"))
if err != nil {
panic(err)
}
defer f.Close()

s := bufio.NewScanner(f)
for s.Scan() {
expr := s.Text()
l := NewExprLexer(expr + "}}")
p := NewExprParser()
root, err := p.Parse(l)
if err != nil {
t.Errorf("%q caused parse error: %v", expr, err)
continue
}
c := NewExprSemanticsChecker(true)
c.Check(root)
}
if err := s.Err(); err != nil {
t.Fatal(err)
}
}

func BenchmarkExprRealWorld(b *testing.B) {
f, err := os.Open(filepath.Join("testdata", "bench", "expressions.txt"))
if err != nil {
b.Fatal(err)
}

exprs := []string{}
s := bufio.NewScanner(f)
for s.Scan() {
exprs = append(exprs, s.Text()+"}}")
}
f.Close()
if err := s.Err(); err != nil {
b.Fatal(err)
}

b.Run("Lex", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, expr := range exprs {
l := NewExprLexer(expr)
for {
t := l.Next()
if l.lexErr != nil {
b.Fatal(l.lexErr)
}
if t.Kind == TokenKindEnd {
break
}
}
}
}
})

b.Run("LexParse", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, expr := range exprs {
if _, err := NewExprParser().Parse(NewExprLexer(expr)); err != nil {
b.Fatal(err)
}
}
}
})

b.Run("LexParseSema", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, expr := range exprs {
root, err := NewExprParser().Parse(NewExprLexer(expr + "}}"))
if err != nil {
b.Fatalf("%q caused parse error: %v", expr, err)
}
NewExprSemanticsChecker(true).Check(root)
}
}
})

trees := []ExprNode{}
for i := 0; i < b.N; i++ {
for _, expr := range exprs {
t, err := NewExprParser().Parse(NewExprLexer(expr))
if err != nil {
b.Fatal(err)
}
trees = append(trees, t)
}
}

b.Run("Sema-untrust", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, t := range trees {
NewExprSemanticsChecker(true).Check(t)
}
}
})

b.Run("Sema-trust", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, t := range trees {
NewExprSemanticsChecker(false).Check(t)
}
}
})
}
Loading

0 comments on commit 0440fe4

Please sign in to comment.