|
| 1 | +// Copyright 2024 The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package silence |
| 15 | + |
| 16 | +import ( |
| 17 | + "strconv" |
| 18 | + "testing" |
| 19 | + "time" |
| 20 | + |
| 21 | + "github.com/benbjohnson/clock" |
| 22 | + "github.com/go-kit/log" |
| 23 | + "github.com/prometheus/client_golang/prometheus" |
| 24 | + "github.com/prometheus/common/model" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | + |
| 27 | + "github.com/prometheus/alertmanager/silence/silencepb" |
| 28 | + "github.com/prometheus/alertmanager/types" |
| 29 | +) |
| 30 | + |
| 31 | +// BenchmarkMutes benchmarks the Mutes method for the Muter interface for |
| 32 | +// different numbers of silences, where all silences match the alert. |
| 33 | +func BenchmarkMutes(b *testing.B) { |
| 34 | + b.Run("1 silence mutes alert", func(b *testing.B) { |
| 35 | + benchmarkMutes(b, 1) |
| 36 | + }) |
| 37 | + b.Run("10 silences mute alert", func(b *testing.B) { |
| 38 | + benchmarkMutes(b, 10) |
| 39 | + }) |
| 40 | + b.Run("100 silences mute alert", func(b *testing.B) { |
| 41 | + benchmarkMutes(b, 100) |
| 42 | + }) |
| 43 | + b.Run("1000 silences mute alert", func(b *testing.B) { |
| 44 | + benchmarkMutes(b, 1000) |
| 45 | + }) |
| 46 | + b.Run("10000 silences mute alert", func(b *testing.B) { |
| 47 | + benchmarkMutes(b, 10000) |
| 48 | + }) |
| 49 | +} |
| 50 | + |
| 51 | +func benchmarkMutes(b *testing.B, n int) { |
| 52 | + silences, err := New(Options{}) |
| 53 | + require.NoError(b, err) |
| 54 | + |
| 55 | + clock := clock.NewMock() |
| 56 | + silences.clock = clock |
| 57 | + now := clock.Now() |
| 58 | + |
| 59 | + var silenceIDs []string |
| 60 | + for i := 0; i < n; i++ { |
| 61 | + var silenceID string |
| 62 | + silenceID, err = silences.Set(&silencepb.Silence{ |
| 63 | + Matchers: []*silencepb.Matcher{{ |
| 64 | + Type: silencepb.Matcher_EQUAL, |
| 65 | + Name: "foo", |
| 66 | + Pattern: "bar", |
| 67 | + }}, |
| 68 | + StartsAt: now, |
| 69 | + EndsAt: now.Add(time.Minute), |
| 70 | + }) |
| 71 | + require.NoError(b, err) |
| 72 | + silenceIDs = append(silenceIDs, silenceID) |
| 73 | + } |
| 74 | + require.Len(b, silenceIDs, n) |
| 75 | + |
| 76 | + m := types.NewMarker(prometheus.NewRegistry()) |
| 77 | + s := NewSilencer(silences, m, log.NewNopLogger()) |
| 78 | + |
| 79 | + b.ResetTimer() |
| 80 | + for i := 0; i < b.N; i++ { |
| 81 | + s.Mutes(model.LabelSet{"foo": "bar"}) |
| 82 | + } |
| 83 | + b.StopTimer() |
| 84 | + |
| 85 | + // The alert should be marked as silenced for each silence. |
| 86 | + activeIDs, pendingIDs, _, silenced := m.Silenced(model.LabelSet{"foo": "bar"}.Fingerprint()) |
| 87 | + require.True(b, silenced) |
| 88 | + require.Empty(b, pendingIDs) |
| 89 | + require.Len(b, activeIDs, n) |
| 90 | +} |
| 91 | + |
| 92 | +// BenchmarkQuery benchmarks the Query method for the Silences struct |
| 93 | +// for different numbers of silences. Not all silences match the query |
| 94 | +// to prevent compiler and runtime optimizations from affecting the benchmarks. |
| 95 | +func BenchmarkQuery(b *testing.B) { |
| 96 | + b.Run("100 silences", func(b *testing.B) { |
| 97 | + benchmarkQuery(b, 100) |
| 98 | + }) |
| 99 | + b.Run("1000 silences", func(b *testing.B) { |
| 100 | + benchmarkQuery(b, 1000) |
| 101 | + }) |
| 102 | + b.Run("10000 silences", func(b *testing.B) { |
| 103 | + benchmarkQuery(b, 10000) |
| 104 | + }) |
| 105 | +} |
| 106 | + |
| 107 | +func benchmarkQuery(b *testing.B, numSilences int) { |
| 108 | + s, err := New(Options{}) |
| 109 | + require.NoError(b, err) |
| 110 | + |
| 111 | + clock := clock.NewMock() |
| 112 | + s.clock = clock |
| 113 | + now := clock.Now() |
| 114 | + |
| 115 | + lset := model.LabelSet{"aaaa": "AAAA", "bbbb": "BBBB", "cccc": "CCCC"} |
| 116 | + |
| 117 | + s.st = state{} |
| 118 | + for i := 0; i < numSilences; i++ { |
| 119 | + id := strconv.Itoa(i) |
| 120 | + // Include an offset to avoid optimizations. |
| 121 | + patA := "A{4}|" + id |
| 122 | + patB := id // Does not match. |
| 123 | + if i%10 == 0 { |
| 124 | + // Every 10th time, have an actually matching pattern. |
| 125 | + patB = "B(B|C)B.|" + id |
| 126 | + } |
| 127 | + |
| 128 | + s.st[id] = &silencepb.MeshSilence{Silence: &silencepb.Silence{ |
| 129 | + Id: id, |
| 130 | + Matchers: []*silencepb.Matcher{ |
| 131 | + {Type: silencepb.Matcher_REGEXP, Name: "aaaa", Pattern: patA}, |
| 132 | + {Type: silencepb.Matcher_REGEXP, Name: "bbbb", Pattern: patB}, |
| 133 | + }, |
| 134 | + StartsAt: now.Add(-time.Minute), |
| 135 | + EndsAt: now.Add(time.Hour), |
| 136 | + UpdatedAt: now.Add(-time.Hour), |
| 137 | + }} |
| 138 | + } |
| 139 | + |
| 140 | + // Run things once to populate the matcherCache. |
| 141 | + sils, _, err := s.Query( |
| 142 | + QState(types.SilenceStateActive), |
| 143 | + QMatches(lset), |
| 144 | + ) |
| 145 | + require.NoError(b, err) |
| 146 | + require.Len(b, sils, numSilences/10) |
| 147 | + |
| 148 | + b.ResetTimer() |
| 149 | + for i := 0; i < b.N; i++ { |
| 150 | + sils, _, err := s.Query( |
| 151 | + QState(types.SilenceStateActive), |
| 152 | + QMatches(lset), |
| 153 | + ) |
| 154 | + require.NoError(b, err) |
| 155 | + require.Len(b, sils, numSilences/10) |
| 156 | + } |
| 157 | +} |
0 commit comments