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
|
// SPDX-License-Identifier: GPL-3.0-or-later
package matcher
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestWithCache(t *testing.T) {
regMatcher, _ := NewRegExpMatcher("[0-9]+")
cached := WithCache(regMatcher)
assert.True(t, cached.MatchString("1"))
assert.True(t, cached.MatchString("1"))
assert.True(t, cached.Match([]byte("2")))
assert.True(t, cached.Match([]byte("2")))
}
func TestWithCache_specialCase(t *testing.T) {
assert.Equal(t, TRUE(), WithCache(TRUE()))
assert.Equal(t, FALSE(), WithCache(FALSE()))
}
func BenchmarkCachedMatcher_MatchString_cache_hit(b *testing.B) {
benchmarks := []struct {
name string
expr string
target string
}{
{"stringFullMatcher", "= abc123", "abc123"},
{"stringPrefixMatcher", "~ ^abc123", "abc123456"},
{"stringSuffixMatcher", "~ abc123$", "hello abc123"},
{"stringSuffixMatcher", "~ abc123", "hello abc123 world"},
{"globMatcher", "* abc*def", "abc12345678def"},
{"regexp", "~ [0-9]+", "1234567890"},
}
for _, bm := range benchmarks {
m := Must(Parse(bm.expr))
b.Run(bm.name+"_raw", func(b *testing.B) {
for i := 0; i < b.N; i++ {
m.MatchString(bm.target)
}
})
b.Run(bm.name+"_cache", func(b *testing.B) {
cached := WithCache(m)
b.ResetTimer()
for i := 0; i < b.N; i++ {
cached.MatchString(bm.target)
}
})
}
}
|