Improves test and benchmark

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>
This commit is contained in:
Cyril Tovena 2021-10-06 15:24:57 +02:00
parent 17a558336b
commit e9f0f0ebed
No known key found for this signature in database
GPG key ID: FD8F768F9D633FB6
2 changed files with 56 additions and 35 deletions

View file

@ -244,12 +244,16 @@ func optimizeConcatRegex(r *syntax.Regexp) (prefix, suffix, contains string) {
// todo remove anchors ^foo$ // todo remove anchors ^foo$
// remove captures. // remove captures.
// .+ | .* | .*POD.* | 5.. | test-.* | .*-test | test-.+ | .+-test // .+ | .* | .*POD.* | test-.* | .*-test | test-.+ | .+-test
// regexp prefix fn ???? can we use this ? // regexp prefix fn ???? can we use this ?
// benchmark them // benchmark them
type StringMatcher interface {
Matches(s string) bool
}
func contains(s, substr string) bool { func contains(s, substr string) bool {
pos := strings.Index(s, substr) pos := strings.Index(s, substr)
if pos < 0 { if pos < 0 {

View file

@ -14,48 +14,65 @@
package labels package labels
import ( import (
"regexp"
"regexp/syntax" "regexp/syntax"
"testing" "testing"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func TestNewFastRegexMatcher(t *testing.T) { var (
cases := []struct { regexes = []string{
regex string "(foo|bar)",
value string "foo.*",
expected bool ".*foo",
}{ "^.*foo$",
{regex: "(foo|bar)", value: "foo", expected: true}, "^.+foo$",
{regex: "(foo|bar)", value: "foo bar", expected: false}, ".*",
{regex: "(foo|bar)", value: "bar", expected: true}, ".+",
{regex: "foo.*", value: "foo bar", expected: true}, "foo.+",
{regex: "foo.*", value: "bar foo", expected: false}, ".+foo",
{regex: ".*foo", value: "foo bar", expected: false}, "foo\n.+",
{regex: ".*foo", value: "bar foo", expected: true}, "foo\n.*",
{regex: ".*foo", value: "foo", expected: true}, ".*foo.*",
{regex: "^.*foo$", value: "foo", expected: true}, ".+foo.+",
{regex: "^.+foo$", value: "foo", expected: false}, "",
{regex: "^.+foo$", value: "bfoo", expected: true},
{regex: ".*", value: "\n", expected: false},
{regex: ".*", value: "\nfoo", expected: false},
{regex: ".*foo", value: "\nfoo", expected: false},
{regex: "foo.*", value: "foo\n", expected: false},
{regex: "foo\n.*", value: "foo\n", expected: true},
{regex: ".*foo.*", value: "foo", expected: true},
{regex: ".*foo.*", value: "foo bar", expected: true},
{regex: ".*foo.*", value: "hello foo world", expected: true},
{regex: ".*foo.*", value: "hello foo\n world", expected: false},
{regex: ".*foo\n.*", value: "hello foo\n world", expected: true},
{regex: ".*", value: "foo", expected: true},
{regex: "", value: "foo", expected: false},
{regex: "", value: "", expected: true},
} }
values = []string{"foo", " foo bar", "bar", "buzz\nbar", "bar foo", "bfoo", "\n", "\nfoo", "foo\n", "hello foo world", "hello foo\n world", ""}
)
for _, c := range cases { func TestNewFastRegexMatcher(t *testing.T) {
m, err := NewFastRegexMatcher(c.regex) for _, r := range regexes {
require.NoError(t, err) r := r
require.Equal(t, c.expected, m.MatchString(c.value)) for _, v := range values {
v := v
t.Run(r+` on "`+v+`"`, func(t *testing.T) {
t.Parallel()
m, err := NewFastRegexMatcher(r)
require.NoError(t, err)
re, err := regexp.Compile("^(?:" + r + ")$")
require.NoError(t, err)
require.Equal(t, re.MatchString(v), m.MatchString(v))
})
}
}
}
func BenchmarkNewFastRegexMatcher(b *testing.B) {
for _, r := range regexes {
r := r
for _, v := range values {
v := v
b.Run(r+` on "`+v+`"`, func(b *testing.B) {
m, err := NewFastRegexMatcher(r)
require.NoError(b, err)
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.MatchString(v)
}
})
}
} }
} }