-
Notifications
You must be signed in to change notification settings - Fork 8
/
pcre2_bench_test.go
254 lines (220 loc) · 6.4 KB
/
pcre2_bench_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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package pcre2_test
import (
"regexp"
"testing"
"github.com/lestrrat/go-pcre2"
)
type regexper interface {
Match([]byte) bool
MatchString(string) bool
FindAllIndex([]byte, int) [][]int
FindAllSubmatchIndex([]byte, int) [][]int
FindAllStringIndex(string, int) [][]int
FindAllStringSubmatchIndex(string, int) [][]int
FindSubmatchIndex([]byte) []int
FindStringSubmatchIndex(string) []int
}
func benchMatch(b *testing.B, re regexper, dos bool) {
patterns := []string{`Hello World!`, `Hello Friend!`, `Hello 友達!`}
for _, pat := range patterns {
var rv bool
if dos {
rv = re.MatchString(pat)
} else {
rv = re.Match([]byte(pat))
}
if !rv {
b.Errorf("Expected to match, failed")
return
}
}
patterns = []string{`Goodbye World!`, `Hell no!`, `HelloWorld!`}
for _, pat := range patterns {
var rv bool
if dos {
rv = re.MatchString(pat)
} else {
rv = re.Match([]byte(pat))
}
if rv {
b.Errorf("Expected to NOT match, matched")
return
}
}
}
func benchFindAllIndex(b *testing.B, re regexper, dos bool) {
patterns := []string{`Alice:35 Bob:42 Charlie:21`, `桃:三年 栗:三年 柿:八年`, `vini:came vidi:saw vici:won`}
for _, pat := range patterns {
var matches [][]int
if dos {
matches = re.FindAllStringIndex(pat, -1)
} else {
matches = re.FindAllIndex([]byte(pat), -1)
}
if len(matches) != 3 {
b.Errorf("Expected to match '%s' against '%#v', got %d", pat, re, len(matches))
b.Logf("%#v", matches)
return
}
}
}
func benchFindSubmatchIndex(b *testing.B, re regexper, dos bool) {
patterns := []string{`Alice:35 Bob:42 Charlie:21`, `桃:三年 栗:三年 柿:八年`, `vini:came vidi:saw vici:won`}
for _, pat := range patterns {
var matches []int
if dos {
matches = re.FindStringSubmatchIndex(pat)
} else {
matches = re.FindSubmatchIndex([]byte(pat))
}
if len(matches) != 6 {
b.Errorf("Expected to match '%s' against '%#v', got %d", pat, re, len(matches))
b.Logf("%#v", matches)
return
}
}
}
func benchFindAllSubmatchIndex(b *testing.B, re regexper, dos bool) {
patterns := []string{`Alice:35 Bob:42 Charlie:21`, `桃:三年 栗:三年 柿:八年`, `vini:came vidi:saw vici:won`}
for _, pat := range patterns {
var matches [][]int
if dos {
matches = re.FindAllStringSubmatchIndex(pat, -1)
} else {
matches = re.FindAllSubmatchIndex([]byte(pat), -1)
}
if len(matches) != 3 {
b.Errorf("Expected to match '%s' against '%#v', got %d", pat, re, len(matches))
b.Logf("%#v", matches)
return
}
}
}
func makeBenchFunc(b *testing.B, which bool, dos bool, pattern string, f func(*testing.B, regexper, bool)) func() {
// Forcing a function call so that we have chance to
// run garbage collection for each iteration
return func() {
var re regexper
var err error
if which { // true == pcre2
re, err = pcre2.Compile(pattern)
} else {
re, err = regexp.Compile(pattern)
}
if err != nil {
b.Errorf("compile failed: %s", err)
return
}
f(b, re, dos)
}
}
const (
UseGoRegexp = false
UsePCRE2Regexp = true
UseBytes = false
UseString = true
)
// Match, MatchString
const RegexpMatchRegex = `^Hello (.+)!$`
func BenchmarkGoRegexpMatch(b *testing.B) {
benchf := makeBenchFunc(b, UseGoRegexp, UseBytes, RegexpMatchRegex, benchMatch)
for i := 0; i < b.N; i++ {
benchf()
}
}
func BenchmarkPCRE2RegexpMatch(b *testing.B) {
benchf := makeBenchFunc(b, UsePCRE2Regexp, UseBytes, RegexpMatchRegex, benchMatch)
for i := 0; i < b.N; i++ {
benchf()
}
}
func BenchmarkGoRegexpMatchString(b *testing.B) {
benchf := makeBenchFunc(b, UseGoRegexp, UseString, RegexpMatchRegex, benchMatch)
for i := 0; i < b.N; i++ {
benchf()
}
}
func BenchmarkPCRE2RegexpMatchString(b *testing.B) {
benchf := makeBenchFunc(b, UsePCRE2Regexp, UseString, RegexpMatchRegex, benchMatch)
for i := 0; i < b.N; i++ {
benchf()
}
}
// FindAllIndex, FindAllStringIndex
const FindAllIndexRegex = `(\S+):(\S+)`
func BenchmarkGoFindAllIndex(b *testing.B) {
benchf := makeBenchFunc(b, UseGoRegexp, UseBytes, FindAllIndexRegex, benchFindAllIndex)
for i := 0; i < b.N; i++ {
benchf()
}
}
func BenchmarkPCRE2FindAllIndex(b *testing.B) {
benchf := makeBenchFunc(b, UsePCRE2Regexp, UseBytes, FindAllIndexRegex, benchFindAllIndex)
for i := 0; i < b.N; i++ {
benchf()
}
}
func BenchmarkGoFindAllStringIndex(b *testing.B) {
benchf := makeBenchFunc(b, UseGoRegexp, UseString, FindAllIndexRegex, benchFindAllIndex)
for i := 0; i < b.N; i++ {
benchf()
}
}
func BenchmarkPCRE2FindAllStringIndex(b *testing.B) {
benchf := makeBenchFunc(b, UsePCRE2Regexp, UseString, FindAllIndexRegex, benchFindAllIndex)
for i := 0; i < b.N; i++ {
benchf()
}
}
// FindSubmatchIndex, FindStringSubmatchIndex
const FindSubmatchIndexRegex = `(\S+):(\S+)`
func BenchmarkGoFindSubmatchIndex(b *testing.B) {
benchf := makeBenchFunc(b, UseGoRegexp, UseBytes, FindSubmatchIndexRegex, benchFindSubmatchIndex)
for i := 0; i < b.N; i++ {
benchf()
}
}
func BenchmarkPCRE2FindSubmatchIndex(b *testing.B) {
benchf := makeBenchFunc(b, UsePCRE2Regexp, UseBytes, FindSubmatchIndexRegex, benchFindSubmatchIndex)
for i := 0; i < b.N; i++ {
benchf()
}
}
func BenchmarkGoFindStringSubmatchIndex(b *testing.B) {
benchf := makeBenchFunc(b, UseGoRegexp, UseString, FindSubmatchIndexRegex, benchFindSubmatchIndex)
for i := 0; i < b.N; i++ {
benchf()
}
}
func BenchmarkPCRE2FindStringSubmatchIndex(b *testing.B) {
benchf := makeBenchFunc(b, UsePCRE2Regexp, UseString, FindSubmatchIndexRegex, benchFindSubmatchIndex)
for i := 0; i < b.N; i++ {
benchf()
}
}
// FindAllSubmatchIndex, FindAllStringSubmatchIndex
const FindAllSubmatchIndexRegex = `(\S+):(\S+)`
func BenchmarkGoFindAllSubmatchIndex(b *testing.B) {
benchf := makeBenchFunc(b, UseGoRegexp, UseBytes, FindAllSubmatchIndexRegex, benchFindAllSubmatchIndex)
for i := 0; i < b.N; i++ {
benchf()
}
}
func BenchmarkPCRE2FindAllSubmatchIndex(b *testing.B) {
benchf := makeBenchFunc(b, UsePCRE2Regexp, UseBytes, FindAllSubmatchIndexRegex, benchFindAllSubmatchIndex)
for i := 0; i < b.N; i++ {
benchf()
}
}
func BenchmarkGoFindAllStringSubmatchIndex(b *testing.B) {
benchf := makeBenchFunc(b, UseGoRegexp, UseString, FindAllSubmatchIndexRegex, benchFindAllSubmatchIndex)
for i := 0; i < b.N; i++ {
benchf()
}
}
func BenchmarkPCRE2FindAllStringSubmatchIndex(b *testing.B) {
benchf := makeBenchFunc(b, UsePCRE2Regexp, UseString, FindAllSubmatchIndexRegex, benchFindAllSubmatchIndex)
for i := 0; i < b.N; i++ {
benchf()
}
}