Repository navigation
Expand file tree
/
Copy pathstring_processing_benchmark_test.go
More file actions
96 lines (93 loc) · 2.52 KB
/
Copy pathstring_processing_benchmark_test.go
File metadata and controls
96 lines (93 loc) · 2.52 KB
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
package regexp2
import (
"strings"
"testing"
)
func BenchmarkStringProcessing(b *testing.B) {
for _, tc := range []struct {
name, input string
options RegexOptions
}{
{"ASCII", "needle" + strings.Repeat("x", 64<<10), None},
{"Unicode", "needle" + strings.Repeat("界", 21845), None},
{"UnicodeRTL", strings.Repeat("界", 21845) + "needle", RightToLeft},
} {
b.Run("FindAll/"+tc.name, func(b *testing.B) {
re := MustCompile(`needle`, tc.options)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
got, err := re.FindAllStringIndex(tc.input, 1)
if err != nil || len(got) != 1 || tc.input[got[0][0]:got[0][1]] != "needle" {
b.Fatal(got, err)
}
}
})
b.Run("FindMatch/"+tc.name, func(b *testing.B) {
re := MustCompile(`needle`, tc.options)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
got, err := re.FindStringMatch(tc.input)
if err != nil || got == nil || got.String() != "needle" {
b.Fatal(got, err)
}
}
})
}
for _, tc := range []struct {
name, input string
options RegexOptions
}{
{"Miss", strings.Repeat("x", 64<<10), None},
{"LateHit", strings.Repeat("x", 64<<10) + "needle", None},
{"LateHitRTL", strings.Repeat("x", 64<<10) + "needle", RightToLeft},
{"ManyHitsRTL", strings.Repeat("xxneedlexx", 256), RightToLeft},
{"ShortHit", "xxneedlexx", None},
} {
b.Run("Replace/"+tc.name, func(b *testing.B) {
re := MustCompile(`needle`, tc.options)
want := strings.ReplaceAll(tc.input, "needle", "pin")
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
got, err := re.Replace(tc.input, "pin", -1, -1)
if err != nil || got != want {
b.Fatal(got, err)
}
}
})
}
for _, tc := range []struct {
name, pattern string
want int
}{
{"NoCaptures", `,`, 1001}, {"Captures", `(,)`, 2001},
} {
b.Run("Split/"+tc.name, func(b *testing.B) {
re := MustCompile(tc.pattern)
input := strings.Repeat("abc,", 1000) + "abc"
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
got, err := re.Split(input, -1)
if err != nil || len(got) != tc.want {
b.Fatal(len(got), err)
}
}
})
}
}
func BenchmarkSplitUnicodePrefix(b *testing.B) {
re := MustCompile(`(needle)`)
prefix := strings.Repeat("界", 21845)
input := prefix + "needle尾"
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
got, err := re.Split(input, -1)
if err != nil || len(got) != 3 || got[0] != prefix || got[1] != "needle" || got[2] != "尾" {
b.Fatal(got, err)
}
}
}