Skip to content

Commit 6c70b5c

Browse files
Silences: Add benchmarks for Mutes (#3771)
* Add benchmarks for Mutes This commit updates the existing benchmarks for silences to also benchmark Mutes. This complements the existing Query benchmarks by also measuring the time taken to mark silenced alerts. --------- Signed-off-by: George Robinson <[email protected]>
1 parent 342f6a5 commit 6c70b5c

File tree

2 files changed

+157
-65
lines changed

2 files changed

+157
-65
lines changed

silence/silence_bench_test.go

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
}

silence/silence_test.go

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ package silence
1515

1616
import (
1717
"bytes"
18-
"fmt"
1918
"os"
2019
"runtime"
2120
"sort"
@@ -1596,70 +1595,6 @@ func TestStateDecodingError(t *testing.T) {
15961595
require.Equal(t, ErrInvalidState, err)
15971596
}
15981597

1599-
func benchmarkSilencesQuery(b *testing.B, numSilences int) {
1600-
s, err := New(Options{})
1601-
require.NoError(b, err)
1602-
1603-
clock := clock.NewMock()
1604-
s.clock = clock
1605-
now := clock.Now()
1606-
1607-
lset := model.LabelSet{"aaaa": "AAAA", "bbbb": "BBBB", "cccc": "CCCC"}
1608-
1609-
s.st = state{}
1610-
for i := 0; i < numSilences; i++ {
1611-
id := fmt.Sprint("ID", i)
1612-
// Patterns also contain the ID to bust any caches that might be used under the hood.
1613-
patA := "A{4}|" + id
1614-
patB := id // Does not match.
1615-
if i%10 == 0 {
1616-
// Every 10th time, have an actually matching pattern.
1617-
patB = "B(B|C)B.|" + id
1618-
}
1619-
1620-
s.st[id] = &pb.MeshSilence{Silence: &pb.Silence{
1621-
Id: id,
1622-
Matchers: []*pb.Matcher{
1623-
{Type: pb.Matcher_REGEXP, Name: "aaaa", Pattern: patA},
1624-
{Type: pb.Matcher_REGEXP, Name: "bbbb", Pattern: patB},
1625-
},
1626-
StartsAt: now.Add(-time.Minute),
1627-
EndsAt: now.Add(time.Hour),
1628-
UpdatedAt: now.Add(-time.Hour),
1629-
}}
1630-
}
1631-
1632-
// Run things once to populate the matcherCache.
1633-
sils, _, err := s.Query(
1634-
QState(types.SilenceStateActive),
1635-
QMatches(lset),
1636-
)
1637-
require.NoError(b, err)
1638-
require.Len(b, sils, numSilences/10)
1639-
1640-
b.ResetTimer()
1641-
for i := 0; i < b.N; i++ {
1642-
sils, _, err := s.Query(
1643-
QState(types.SilenceStateActive),
1644-
QMatches(lset),
1645-
)
1646-
require.NoError(b, err)
1647-
require.Len(b, sils, numSilences/10)
1648-
}
1649-
}
1650-
1651-
func Benchmark100SilencesQuery(b *testing.B) {
1652-
benchmarkSilencesQuery(b, 100)
1653-
}
1654-
1655-
func Benchmark1000SilencesQuery(b *testing.B) {
1656-
benchmarkSilencesQuery(b, 1000)
1657-
}
1658-
1659-
func Benchmark10000SilencesQuery(b *testing.B) {
1660-
benchmarkSilencesQuery(b, 10000)
1661-
}
1662-
16631598
// runtime.Gosched() does not "suspend" the current goroutine so there's no guarantee that the main goroutine won't
16641599
// be able to continue. For more see https://pkg.go.dev/runtime#Gosched.
16651600
func gosched() {

0 commit comments

Comments
 (0)