Skip to content

Commit 7d6cebe

Browse files
authored
refactor: use b.Loop() to simplify the code and improve performance (#4642)
Signed-off-by: promalert <[email protected]>
1 parent 15defd4 commit 7d6cebe

File tree

4 files changed

+6
-9
lines changed

4 files changed

+6
-9
lines changed

inhibit/inhibit_bench_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,8 @@ func benchmarkMutes(b *testing.B, opts benchmarkOptions) {
204204

205205
// Wait some time for the inhibitor to seed its cache.
206206
<-time.After(time.Second)
207-
b.ResetTimer()
208207

209-
for i := 0; i < b.N; i++ {
208+
for b.Loop() {
210209
require.NoError(b, opts.benchFunc(ih.Mutes))
211210
}
212211
}

matcher/parse/bench_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ const (
2323
)
2424

2525
func BenchmarkParseSimple(b *testing.B) {
26-
for i := 0; i < b.N; i++ {
26+
for b.Loop() {
2727
if _, err := Matchers(simpleExample); err != nil {
2828
b.Fatal(err)
2929
}
3030
}
3131
}
3232

3333
func BenchmarkParseComplex(b *testing.B) {
34-
for i := 0; i < b.N; i++ {
34+
for b.Loop() {
3535
if _, err := Matchers(complexExample); err != nil {
3636
b.Fatal(err)
3737
}

notify/notify_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,7 @@ func BenchmarkHashAlert(b *testing.B) {
10761076
Labels: model.LabelSet{"foo": "the_first_value", "bar": "the_second_value", "another": "value"},
10771077
},
10781078
}
1079-
for i := 0; i < b.N; i++ {
1079+
for b.Loop() {
10801080
hashAlert(alert)
10811081
}
10821082
}

silence/silence_bench_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ func benchmarkMutes(b *testing.B, n int) {
7676
m := types.NewMarker(prometheus.NewRegistry())
7777
s := NewSilencer(silences, m, promslog.NewNopLogger())
7878

79-
b.ResetTimer()
80-
for i := 0; i < b.N; i++ {
79+
for b.Loop() {
8180
s.Mutes(model.LabelSet{"foo": "bar"})
8281
}
8382
b.StopTimer()
@@ -145,8 +144,7 @@ func benchmarkQuery(b *testing.B, numSilences int) {
145144
require.NoError(b, err)
146145
require.Len(b, sils, numSilences/10)
147146

148-
b.ResetTimer()
149-
for i := 0; i < b.N; i++ {
147+
for b.Loop() {
150148
sils, _, err := s.Query(
151149
QState(types.SilenceStateActive),
152150
QMatches(lset),

0 commit comments

Comments
 (0)