github.com/netdata/go.d.plugin@v0.58.1/pkg/matcher/regexp_test.go (about) 1 // SPDX-License-Identifier: GPL-3.0-or-later 2 3 package matcher 4 5 import ( 6 "regexp" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestRegExpMatch_Match(t *testing.T) { 13 m := regexp.MustCompile("[0-9]+") 14 15 cases := []struct { 16 expected bool 17 line string 18 }{ 19 { 20 expected: true, 21 line: "2019", 22 }, 23 { 24 expected: true, 25 line: "It's over 9000!", 26 }, 27 { 28 expected: false, 29 line: "This will never fail!", 30 }, 31 } 32 33 for _, c := range cases { 34 assert.Equal(t, c.expected, m.MatchString(c.line)) 35 } 36 } 37 38 func BenchmarkRegExp_MatchString(b *testing.B) { 39 benchmarks := []struct { 40 expr string 41 test string 42 }{ 43 {"", ""}, 44 {"abc", "abcd"}, 45 {"^abc", "abcd"}, 46 {"abc$", "abcd"}, 47 {"^abc$", "abcd"}, 48 {"[a-z]+", "abcd"}, 49 } 50 for _, bm := range benchmarks { 51 b.Run(bm.expr+"_raw", func(b *testing.B) { 52 m := regexp.MustCompile(bm.expr) 53 b.ResetTimer() 54 for i := 0; i < b.N; i++ { 55 m.MatchString(bm.test) 56 } 57 }) 58 b.Run(bm.expr+"_optimized", func(b *testing.B) { 59 m, _ := NewRegExpMatcher(bm.expr) 60 b.ResetTimer() 61 for i := 0; i < b.N; i++ { 62 m.MatchString(bm.test) 63 } 64 }) 65 } 66 }