mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
Improves test and benchmark
Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>
This commit is contained in:
parent
17a558336b
commit
e9f0f0ebed
|
@ -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 {
|
||||||
|
|
|
@ -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"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
regexes = []string{
|
||||||
|
"(foo|bar)",
|
||||||
|
"foo.*",
|
||||||
|
".*foo",
|
||||||
|
"^.*foo$",
|
||||||
|
"^.+foo$",
|
||||||
|
".*",
|
||||||
|
".+",
|
||||||
|
"foo.+",
|
||||||
|
".+foo",
|
||||||
|
"foo\n.+",
|
||||||
|
"foo\n.*",
|
||||||
|
".*foo.*",
|
||||||
|
".+foo.+",
|
||||||
|
"",
|
||||||
|
}
|
||||||
|
values = []string{"foo", " foo bar", "bar", "buzz\nbar", "bar foo", "bfoo", "\n", "\nfoo", "foo\n", "hello foo world", "hello foo\n world", ""}
|
||||||
|
)
|
||||||
|
|
||||||
func TestNewFastRegexMatcher(t *testing.T) {
|
func TestNewFastRegexMatcher(t *testing.T) {
|
||||||
cases := []struct {
|
for _, r := range regexes {
|
||||||
regex string
|
r := r
|
||||||
value string
|
for _, v := range values {
|
||||||
expected bool
|
v := v
|
||||||
}{
|
t.Run(r+` on "`+v+`"`, func(t *testing.T) {
|
||||||
{regex: "(foo|bar)", value: "foo", expected: true},
|
t.Parallel()
|
||||||
{regex: "(foo|bar)", value: "foo bar", expected: false},
|
m, err := NewFastRegexMatcher(r)
|
||||||
{regex: "(foo|bar)", value: "bar", expected: true},
|
require.NoError(t, err)
|
||||||
{regex: "foo.*", value: "foo bar", expected: true},
|
re, err := regexp.Compile("^(?:" + r + ")$")
|
||||||
{regex: "foo.*", value: "bar foo", expected: false},
|
require.NoError(t, err)
|
||||||
{regex: ".*foo", value: "foo bar", expected: false},
|
require.Equal(t, re.MatchString(v), m.MatchString(v))
|
||||||
{regex: ".*foo", value: "bar foo", expected: true},
|
})
|
||||||
{regex: ".*foo", value: "foo", expected: true},
|
|
||||||
{regex: "^.*foo$", value: "foo", expected: true},
|
|
||||||
{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},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range cases {
|
}
|
||||||
m, err := NewFastRegexMatcher(c.regex)
|
}
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, c.expected, m.MatchString(c.value))
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue