mirror of
https://github.com/prometheus/prometheus.git
synced 2024-11-09 23:24:05 -08:00
Check context every 128 labels instead of 100 (#14118)
Follow up on https://github.com/prometheus/prometheus/pull/14096 As promised, I bring a benchmark, which shows a very small improvement if context is checked every 128 iterations of label instead of every 100. It's much easier for a computer to check modulo 128 than modulo 100. This is a very small 0-2% improvement but I'd say this is one of the hottest paths of the app so this is still relevant. Signed-off-by: Oleg Zaytsev <mail@olegzaytsev.com>
This commit is contained in:
parent
114dc5c393
commit
fe9cb5a803
|
@ -53,7 +53,7 @@ const (
|
|||
seriesByteAlign = 16
|
||||
|
||||
// checkContextEveryNIterations is used in some tight loops to check if the context is done.
|
||||
checkContextEveryNIterations = 100
|
||||
checkContextEveryNIterations = 128
|
||||
)
|
||||
|
||||
type indexWriterSeries struct {
|
||||
|
|
|
@ -22,8 +22,10 @@ import (
|
|||
"math/rand"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/regexp"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/prometheus/prometheus/model/labels"
|
||||
|
@ -1284,6 +1286,58 @@ func BenchmarkListPostings(b *testing.B) {
|
|||
}
|
||||
}
|
||||
|
||||
func slowRegexpString() string {
|
||||
nums := map[int]struct{}{}
|
||||
for i := 10_000; i < 20_000; i++ {
|
||||
if i%3 == 0 {
|
||||
nums[i] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
var sb strings.Builder
|
||||
sb.WriteString(".*(9999")
|
||||
for i := range nums {
|
||||
sb.WriteString("|")
|
||||
sb.WriteString(strconv.Itoa(i))
|
||||
}
|
||||
sb.WriteString(").*")
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
func BenchmarkMemPostings_PostingsForLabelMatching(b *testing.B) {
|
||||
fast := regexp.MustCompile("^(100|200)$")
|
||||
slowRegexp := "^" + slowRegexpString() + "$"
|
||||
b.Logf("Slow regexp length = %d", len(slowRegexp))
|
||||
slow := regexp.MustCompile(slowRegexp)
|
||||
|
||||
for _, labelValueCount := range []int{1_000, 10_000, 100_000} {
|
||||
b.Run(fmt.Sprintf("labels=%d", labelValueCount), func(b *testing.B) {
|
||||
mp := NewMemPostings()
|
||||
for i := 0; i < labelValueCount; i++ {
|
||||
mp.Add(storage.SeriesRef(i), labels.FromStrings("label", strconv.Itoa(i)))
|
||||
}
|
||||
|
||||
fp, err := ExpandPostings(mp.PostingsForLabelMatching(context.Background(), "label", fast.MatchString))
|
||||
require.NoError(b, err)
|
||||
b.Logf("Fast matcher matches %d series", len(fp))
|
||||
b.Run("matcher=fast", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
mp.PostingsForLabelMatching(context.Background(), "label", fast.MatchString).Next()
|
||||
}
|
||||
})
|
||||
|
||||
sp, err := ExpandPostings(mp.PostingsForLabelMatching(context.Background(), "label", slow.MatchString))
|
||||
require.NoError(b, err)
|
||||
b.Logf("Slow matcher matches %d series", len(sp))
|
||||
b.Run("matcher=slow", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
mp.PostingsForLabelMatching(context.Background(), "label", slow.MatchString).Next()
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMemPostings_PostingsForLabelMatchingHonorsContextCancel(t *testing.T) {
|
||||
memP := NewMemPostings()
|
||||
seriesCount := 10 * checkContextEveryNIterations
|
||||
|
|
Loading…
Reference in a new issue