File: regexp_performance_test.go

package info (click to toggle)
golang-github-dlclark-regexp2 1.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 560 kB
  • sloc: makefile: 3
file content (307 lines) | stat: -rw-r--r-- 9,299 bytes parent folder | download | duplicates (2)
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package regexp2

import (
	"strings"
	"testing"
)

func BenchmarkLiteral(b *testing.B) {
	x := strings.Repeat("x", 50) + "y"
	b.StopTimer()
	re := MustCompile("y", 0)
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		if m, err := re.MatchString(x); !m || err != nil {
			b.Fatalf("no match or error! %v", err)
		}
	}
}

func BenchmarkNotLiteral(b *testing.B) {
	x := strings.Repeat("x", 50) + "y"
	b.StopTimer()
	re := MustCompile(".y", 0)
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		if m, err := re.MatchString(x); !m || err != nil {
			b.Fatalf("no match or error! %v", err)
		}
	}
}

func BenchmarkMatchClass(b *testing.B) {
	b.StopTimer()
	x := strings.Repeat("xxxx", 20) + "w"
	re := MustCompile("[abcdw]", 0)
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		if m, err := re.MatchString(x); !m || err != nil {
			b.Fatalf("no match or error! %v", err)
		}

	}
}

func BenchmarkMatchClass_InRange(b *testing.B) {
	b.StopTimer()
	// 'b' is between 'a' and 'c', so the charclass
	// range checking is no help here.
	x := strings.Repeat("bbbb", 20) + "c"
	re := MustCompile("[ac]", 0)
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		if m, err := re.MatchString(x); !m || err != nil {
			b.Fatalf("no match or error! %v", err)
		}
	}
}

/*
func BenchmarkReplaceAll(b *testing.B) {
	x := "abcdefghijklmnopqrstuvwxyz"
	b.StopTimer()
	re := MustCompile("[cjrw]", 0)
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		re.ReplaceAllString(x, "")
	}
}
*/
func BenchmarkAnchoredLiteralShortNonMatch(b *testing.B) {
	b.StopTimer()
	x := "abcdefghijklmnopqrstuvwxyz"
	re := MustCompile("^zbc(d|e)", 0)
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		if m, err := re.MatchString(x); m || err != nil {
			b.Fatalf("unexpected match or error! %v", err)
		}
	}
}

func BenchmarkAnchoredLiteralLongNonMatch(b *testing.B) {
	b.StopTimer()

	data := "abcdefghijklmnopqrstuvwxyz"
	x := make([]rune, 32768*len(data))
	for i := 0; i < 32768; /*(2^15)*/ i++ {
		for j := 0; j < len(data); j++ {
			x[i*len(data)+j] = rune(data[j])
		}
	}

	re := MustCompile("^zbc(d|e)", 0)
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		if m, err := re.MatchRunes(x); m || err != nil {
			b.Fatalf("unexpected match or error! %v", err)
		}
	}
}

func BenchmarkAnchoredShortMatch(b *testing.B) {
	b.StopTimer()
	x := "abcdefghijklmnopqrstuvwxyz"
	re := MustCompile("^.bc(d|e)", 0)
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		if m, err := re.MatchString(x); !m || err != nil {
			b.Fatalf("no match or error! %v", err)
		}
	}
}

func BenchmarkAnchoredLongMatch(b *testing.B) {
	b.StopTimer()
	data := "abcdefghijklmnopqrstuvwxyz"
	x := make([]rune, 32768*len(data))
	for i := 0; i < 32768; /*(2^15)*/ i++ {
		for j := 0; j < len(data); j++ {
			x[i*len(data)+j] = rune(data[j])
		}
	}

	re := MustCompile("^.bc(d|e)", 0)
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		if m, err := re.MatchRunes(x); !m || err != nil {
			b.Fatalf("no match or error! %v", err)
		}
	}
}

func BenchmarkOnePassShortA(b *testing.B) {
	b.StopTimer()
	x := "abcddddddeeeededd"
	re := MustCompile("^.bc(d|e)*$", 0)
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		if m, err := re.MatchString(x); !m || err != nil {
			b.Fatalf("no match or error! %v", err)
		}
	}
}

func BenchmarkNotOnePassShortA(b *testing.B) {
	b.StopTimer()
	x := "abcddddddeeeededd"
	re := MustCompile(".bc(d|e)*$", 0)
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		if m, err := re.MatchString(x); !m || err != nil {
			b.Fatalf("no match or error! %v", err)
		}
	}
}

func BenchmarkOnePassShortB(b *testing.B) {
	b.StopTimer()
	x := "abcddddddeeeededd"
	re := MustCompile("^.bc(?:d|e)*$", 0)
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		if m, err := re.MatchString(x); !m || err != nil {
			b.Fatalf("no match or error! %v", err)
		}
	}
}

func BenchmarkNotOnePassShortB(b *testing.B) {
	b.StopTimer()
	x := "abcddddddeeeededd"
	re := MustCompile(".bc(?:d|e)*$", 0)
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		if m, err := re.MatchString(x); !m || err != nil {
			b.Fatalf("no match or error! %v", err)
		}
	}
}

func BenchmarkOnePassLongPrefix(b *testing.B) {
	b.StopTimer()
	x := "abcdefghijklmnopqrstuvwxyz"
	re := MustCompile("^abcdefghijklmnopqrstuvwxyz.*$", 0)
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		if m, err := re.MatchString(x); !m || err != nil {
			b.Fatalf("no match or error! %v", err)
		}
	}
}

func BenchmarkOnePassLongNotPrefix(b *testing.B) {
	b.StopTimer()
	x := "abcdefghijklmnopqrstuvwxyz"
	re := MustCompile("^.bcdefghijklmnopqrstuvwxyz.*$", 0)
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		if m, err := re.MatchString(x); !m || err != nil {
			b.Fatalf("no match or error! %v", err)
		}
	}
}

var text []rune

func makeText(n int) []rune {
	if len(text) >= n {
		return text[:n]
	}
	text = make([]rune, n)
	x := ^uint32(0)
	for i := range text {
		x += x
		x ^= 1
		if int32(x) < 0 {
			x ^= 0x88888eef
		}
		if x%31 == 0 {
			text[i] = '\n'
		} else {
			text[i] = rune(x%(0x7E+1-0x20) + 0x20)
		}
	}
	return text
}

func benchmark(b *testing.B, re string, n int) {
	r := MustCompile(re, 0)
	t := makeText(n)
	b.ResetTimer()
	b.SetBytes(int64(n))
	for i := 0; i < b.N; i++ {
		if m, err := r.MatchRunes(t); m {
			b.Fatal("match!")
		} else if err != nil {
			b.Fatalf("Err %v", err)
		}
	}
}

const (
	easy0  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ$"
	easy1  = "A[AB]B[BC]C[CD]D[DE]E[EF]F[FG]G[GH]H[HI]I[IJ]J$"
	medium = "[XYZ]ABCDEFGHIJKLMNOPQRSTUVWXYZ$"
	hard   = "[ -~]*ABCDEFGHIJKLMNOPQRSTUVWXYZ$"
	hard1  = "ABCD|CDEF|EFGH|GHIJ|IJKL|KLMN|MNOP|OPQR|QRST|STUV|UVWX|WXYZ"
	parens = "([ -~])*(A)(B)(C)(D)(E)(F)(G)(H)(I)(J)(K)(L)(M)" +
		"(N)(O)(P)(Q)(R)(S)(T)(U)(V)(W)(X)(Y)(Z)$"
)

func BenchmarkMatchEasy0_32(b *testing.B)   { benchmark(b, easy0, 32<<0) }
func BenchmarkMatchEasy0_1K(b *testing.B)   { benchmark(b, easy0, 1<<10) }
func BenchmarkMatchEasy0_32K(b *testing.B)  { benchmark(b, easy0, 32<<10) }
func BenchmarkMatchEasy0_1M(b *testing.B)   { benchmark(b, easy0, 1<<20) }
func BenchmarkMatchEasy0_32M(b *testing.B)  { benchmark(b, easy0, 32<<20) }
func BenchmarkMatchEasy1_32(b *testing.B)   { benchmark(b, easy1, 32<<0) }
func BenchmarkMatchEasy1_1K(b *testing.B)   { benchmark(b, easy1, 1<<10) }
func BenchmarkMatchEasy1_32K(b *testing.B)  { benchmark(b, easy1, 32<<10) }
func BenchmarkMatchEasy1_1M(b *testing.B)   { benchmark(b, easy1, 1<<20) }
func BenchmarkMatchEasy1_32M(b *testing.B)  { benchmark(b, easy1, 32<<20) }
func BenchmarkMatchMedium_32(b *testing.B)  { benchmark(b, medium, 32<<0) }
func BenchmarkMatchMedium_1K(b *testing.B)  { benchmark(b, medium, 1<<10) }
func BenchmarkMatchMedium_32K(b *testing.B) { benchmark(b, medium, 32<<10) }
func BenchmarkMatchMedium_1M(b *testing.B)  { benchmark(b, medium, 1<<20) }
func BenchmarkMatchMedium_32M(b *testing.B) { benchmark(b, medium, 32<<20) }
func BenchmarkMatchHard_32(b *testing.B)    { benchmark(b, hard, 32<<0) }
func BenchmarkMatchHard_1K(b *testing.B)    { benchmark(b, hard, 1<<10) }
func BenchmarkMatchHard_32K(b *testing.B)   { benchmark(b, hard, 32<<10) }
func BenchmarkMatchHard_1M(b *testing.B)    { benchmark(b, hard, 1<<20) }
func BenchmarkMatchHard_32M(b *testing.B)   { benchmark(b, hard, 32<<20) }
func BenchmarkMatchHard1_32(b *testing.B)   { benchmark(b, hard1, 32<<0) }
func BenchmarkMatchHard1_1K(b *testing.B)   { benchmark(b, hard1, 1<<10) }
func BenchmarkMatchHard1_32K(b *testing.B)  { benchmark(b, hard1, 32<<10) }
func BenchmarkMatchHard1_1M(b *testing.B)   { benchmark(b, hard1, 1<<20) }
func BenchmarkMatchHard1_32M(b *testing.B)  { benchmark(b, hard1, 32<<20) }

// TestProgramTooLongForBacktrack tests that a regex which is too long
// for the backtracker still executes properly.
func TestProgramTooLongForBacktrack(t *testing.T) {
	longRegex := MustCompile(`(one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|twenty|twentyone|twentytwo|twentythree|twentyfour|twentyfive|twentysix|twentyseven|twentyeight|twentynine|thirty|thirtyone|thirtytwo|thirtythree|thirtyfour|thirtyfive|thirtysix|thirtyseven|thirtyeight|thirtynine|forty|fortyone|fortytwo|fortythree|fortyfour|fortyfive|fortysix|fortyseven|fortyeight|fortynine|fifty|fiftyone|fiftytwo|fiftythree|fiftyfour|fiftyfive|fiftysix|fiftyseven|fiftyeight|fiftynine|sixty|sixtyone|sixtytwo|sixtythree|sixtyfour|sixtyfive|sixtysix|sixtyseven|sixtyeight|sixtynine|seventy|seventyone|seventytwo|seventythree|seventyfour|seventyfive|seventysix|seventyseven|seventyeight|seventynine|eighty|eightyone|eightytwo|eightythree|eightyfour|eightyfive|eightysix|eightyseven|eightyeight|eightynine|ninety|ninetyone|ninetytwo|ninetythree|ninetyfour|ninetyfive|ninetysix|ninetyseven|ninetyeight|ninetynine|onehundred)`, 0)

	if m, err := longRegex.MatchString("two"); !m {
		t.Errorf("longRegex.MatchString(\"two\") was false, want true")
	} else if err != nil {
		t.Errorf("Error: %v", err)
	}
	if m, err := longRegex.MatchString("xxx"); m {
		t.Errorf("longRegex.MatchString(\"xxx\") was true, want false")
	} else if err != nil {
		t.Errorf("Error: %v", err)
	}
}

func BenchmarkLeading(b *testing.B) {
	b.StopTimer()
	r := MustCompile("[a-q][^u-z]{13}x", 0)
	inp := makeText(1000000)
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		if m, err := r.MatchRunes(inp); !m {
			b.Errorf("Expected match")
		} else if err != nil {
			b.Errorf("Error: %v", err)
		}
	}
}