-
Notifications
You must be signed in to change notification settings - Fork 158
/
expr_test.go
116 lines (105 loc) · 2.22 KB
/
expr_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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, nil)
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, nil).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, nil).Check(t)
}
}
})
b.Run("Sema-trust", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, t := range trees {
NewExprSemanticsChecker(false, nil).Check(t)
}
}
})
}