mirror of
https://github.com/kubernetes/node-problem-detector.git
synced 2026-08-23 22:26:27 +00:00
Precompile log buffer regular expressions
Compile fixed patterns during monitor and log counter initialization, pass compiled expressions to the log buffer, and reject invalid log counter patterns before watching logs.
This commit is contained in:
@@ -61,6 +61,12 @@ func TestPush(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompilePatternRejectsInvalidExpression(t *testing.T) {
|
||||
if _, err := CompilePattern(`foo\`); err == nil {
|
||||
t.Fatal("expected invalid expression error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMatch(t *testing.T) {
|
||||
max := 4
|
||||
for c, test := range []struct {
|
||||
@@ -97,7 +103,11 @@ func TestMatch(t *testing.T) {
|
||||
b.Push(&types.Log{Message: log})
|
||||
}
|
||||
for i, expr := range test.exprs {
|
||||
logs := b.Match(expr)
|
||||
pattern, err := CompilePattern(expr)
|
||||
if err != nil {
|
||||
t.Fatalf("case %d.%d: failed to compile pattern %q: %v", c+1, i+1, expr, err)
|
||||
}
|
||||
logs := b.Match(pattern)
|
||||
got := []string{}
|
||||
for _, log := range logs {
|
||||
got = append(got, log.Message)
|
||||
@@ -117,8 +127,12 @@ func BenchmarkMatch(b *testing.B) {
|
||||
// A pattern from the default kernel monitor configuration which does not
|
||||
// match the buffered logs.
|
||||
expr := `task [\S ]+:\w+ blocked for more than \w+ seconds\.`
|
||||
pattern, err := CompilePattern(expr)
|
||||
if err != nil {
|
||||
b.Fatalf("failed to compile pattern %q: %v", expr, err)
|
||||
}
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
buf.Match(expr)
|
||||
buf.Match(pattern)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user