Skip to content

Commit 14cbe63

Browse files
Add more benchmarks for inhibition rules (#3773)
* Add more benchmarks for inhibition rules This commit adds more benchmarks for inhibition rules where just the last rule in the benchmark inhibits the labels. Signed-off-by: George Robinson <[email protected]> --------- Signed-off-by: George Robinson <[email protected]>
1 parent f41fccb commit 14cbe63

File tree

1 file changed

+73
-22
lines changed

1 file changed

+73
-22
lines changed

inhibit/inhibit_bench_test.go

Lines changed: 73 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,34 +35,46 @@ import (
3535
// for different numbers of inhibition rules.
3636
func BenchmarkMutes(b *testing.B) {
3737
b.Run("1 inhibition rule, 1 inhibiting alert", func(b *testing.B) {
38-
benchmarkMutes(b, defaultBenchmark(b, 1, 1))
38+
benchmarkMutes(b, allRulesMatchBenchmark(b, 1, 1))
3939
})
4040
b.Run("10 inhibition rules, 1 inhibiting alert", func(b *testing.B) {
41-
benchmarkMutes(b, defaultBenchmark(b, 10, 1))
41+
benchmarkMutes(b, allRulesMatchBenchmark(b, 10, 1))
4242
})
4343
b.Run("100 inhibition rules, 1 inhibiting alert", func(b *testing.B) {
44-
benchmarkMutes(b, defaultBenchmark(b, 100, 1))
44+
benchmarkMutes(b, allRulesMatchBenchmark(b, 100, 1))
4545
})
4646
b.Run("1000 inhibition rules, 1 inhibiting alert", func(b *testing.B) {
47-
benchmarkMutes(b, defaultBenchmark(b, 1000, 1))
47+
benchmarkMutes(b, allRulesMatchBenchmark(b, 1000, 1))
4848
})
4949
b.Run("10000 inhibition rules, 1 inhibiting alert", func(b *testing.B) {
50-
benchmarkMutes(b, defaultBenchmark(b, 10000, 1))
50+
benchmarkMutes(b, allRulesMatchBenchmark(b, 10000, 1))
5151
})
5252
b.Run("1 inhibition rule, 10 inhibiting alerts", func(b *testing.B) {
53-
benchmarkMutes(b, defaultBenchmark(b, 1, 10))
53+
benchmarkMutes(b, allRulesMatchBenchmark(b, 1, 10))
5454
})
5555
b.Run("1 inhibition rule, 100 inhibiting alerts", func(b *testing.B) {
56-
benchmarkMutes(b, defaultBenchmark(b, 1, 100))
56+
benchmarkMutes(b, allRulesMatchBenchmark(b, 1, 100))
5757
})
5858
b.Run("1 inhibition rule, 1000 inhibiting alerts", func(b *testing.B) {
59-
benchmarkMutes(b, defaultBenchmark(b, 1, 1000))
59+
benchmarkMutes(b, allRulesMatchBenchmark(b, 1, 1000))
6060
})
6161
b.Run("1 inhibition rule, 10000 inhibiting alerts", func(b *testing.B) {
62-
benchmarkMutes(b, defaultBenchmark(b, 1, 10000))
62+
benchmarkMutes(b, allRulesMatchBenchmark(b, 1, 10000))
6363
})
6464
b.Run("100 inhibition rules, 1000 inhibiting alerts", func(b *testing.B) {
65-
benchmarkMutes(b, defaultBenchmark(b, 100, 1000))
65+
benchmarkMutes(b, allRulesMatchBenchmark(b, 100, 1000))
66+
})
67+
b.Run("10 inhibition rules, last rule matches", func(b *testing.B) {
68+
benchmarkMutes(b, lastRuleMatchesBenchmark(b, 10))
69+
})
70+
b.Run("100 inhibition rules, last rule matches", func(b *testing.B) {
71+
benchmarkMutes(b, lastRuleMatchesBenchmark(b, 100))
72+
})
73+
b.Run("1000 inhibition rules, last rule matches", func(b *testing.B) {
74+
benchmarkMutes(b, lastRuleMatchesBenchmark(b, 1000))
75+
})
76+
b.Run("10000 inhibition rules, last rule matches", func(b *testing.B) {
77+
benchmarkMutes(b, lastRuleMatchesBenchmark(b, 10000))
6678
})
6779
}
6880

@@ -79,19 +91,20 @@ type benchmarkOptions struct {
7991
benchFunc func(mutesFunc func(model.LabelSet) bool) error
8092
}
8193

82-
// defaultBenchmark returns the default benchmark. It supports a number of
83-
// variations, including customization of the number of inhibition rules,
84-
// and the number of inhibiting alerts per inhibition rule.
94+
// allRulesMatchBenchmark returns a new benchmark where all inhibition rules
95+
// inhibit the label dst=0. It supports a number of variations, including
96+
// customization of the number of inhibition rules, and the number of
97+
// inhibiting alerts per inhibition rule.
8598
//
8699
// The source matchers are suffixed with the position of the inhibition rule
87-
// in the list. For example, src=1, src=2, etc. The target matchers are
88-
// the same across all inhibition rules (dst=0).
100+
// in the list (e.g. src=1, src=2, etc...). The target matchers are the same
101+
// across all inhibition rules (dst=0).
89102
//
90103
// Each inhibition rule can have zero or more alerts that match the source
91104
// matchers, and is determined with numInhibitingAlerts.
92105
//
93-
// The default benchmark expects dst=0 to be muted and will fail if not.
94-
func defaultBenchmark(b *testing.B, numInhibitionRules, numInhibitingAlerts int) benchmarkOptions {
106+
// It expects dst=0 to be muted and will fail if not.
107+
func allRulesMatchBenchmark(b *testing.B, numInhibitionRules, numInhibitingAlerts int) benchmarkOptions {
95108
return benchmarkOptions{
96109
n: numInhibitionRules,
97110
newRuleFunc: func(idx int) config.InhibitRule {
@@ -126,6 +139,48 @@ func defaultBenchmark(b *testing.B, numInhibitionRules, numInhibitingAlerts int)
126139
}
127140
}
128141

142+
// lastRuleMatchesBenchmark returns a new benchmark where the last inhibition
143+
// rule inhibits the label dst=0. All other inhibition rules are no-ops.
144+
//
145+
// The source matchers are suffixed with the position of the inhibition rule
146+
// in the list (e.g. src=1, src=2, etc...). The target matchers are the same
147+
// across all inhibition rules (dst=0).
148+
//
149+
// It expects dst=0 to be muted and will fail if not.
150+
func lastRuleMatchesBenchmark(b *testing.B, n int) benchmarkOptions {
151+
return benchmarkOptions{
152+
n: n,
153+
newRuleFunc: func(idx int) config.InhibitRule {
154+
return config.InhibitRule{
155+
SourceMatchers: config.Matchers{
156+
mustNewMatcher(b, labels.MatchEqual, "src", strconv.Itoa(idx)),
157+
},
158+
TargetMatchers: config.Matchers{
159+
mustNewMatcher(b, labels.MatchEqual, "dst", "0"),
160+
},
161+
}
162+
},
163+
newAlertsFunc: func(idx int, _ config.InhibitRule) []types.Alert {
164+
// Do not create an alert unless it is the last inhibition rule.
165+
if idx < n-1 {
166+
return nil
167+
}
168+
return []types.Alert{{
169+
Alert: model.Alert{
170+
Labels: model.LabelSet{
171+
"src": model.LabelValue(strconv.Itoa(idx)),
172+
},
173+
},
174+
}}
175+
}, benchFunc: func(mutesFunc func(set model.LabelSet) bool) error {
176+
if ok := mutesFunc(model.LabelSet{"dst": "0"}); !ok {
177+
return errors.New("expected dst=0 to be muted")
178+
}
179+
return nil
180+
},
181+
}
182+
}
183+
129184
func benchmarkMutes(b *testing.B, opts benchmarkOptions) {
130185
r := prometheus.NewRegistry()
131186
m := types.NewMarker(r)
@@ -148,11 +203,7 @@ func benchmarkMutes(b *testing.B, opts benchmarkOptions) {
148203
go ih.Run()
149204

150205
// Wait some time for the inhibitor to seed its cache.
151-
waitDuration := time.Millisecond * time.Duration(len(alerts))
152-
if waitDuration > time.Second {
153-
waitDuration = time.Second
154-
}
155-
<-time.After(waitDuration)
206+
<-time.After(time.Second)
156207
b.ResetTimer()
157208

158209
for i := 0; i < b.N; i++ {

0 commit comments

Comments
 (0)