2019-12-05 23:47:31 -08:00
|
|
|
// Copyright 2018 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package tsdb
|
|
|
|
|
|
|
|
import (
|
2020-07-30 04:36:56 -07:00
|
|
|
"context"
|
2019-12-13 02:33:16 -08:00
|
|
|
"fmt"
|
2019-12-05 23:47:31 -08:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
2020-10-29 02:43:23 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-10-22 02:00:08 -07:00
|
|
|
|
2019-12-05 23:47:31 -08:00
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Make entries ~50B in size, to emulate real-world high cardinality.
|
|
|
|
const (
|
|
|
|
postingsBenchSuffix = "aaaaaaaaaabbbbbbbbbbccccccccccdddddddddd"
|
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkPostingsForMatchers(b *testing.B) {
|
2020-05-06 08:30:00 -07:00
|
|
|
chunkDir, err := ioutil.TempDir("", "chunk_dir")
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(b, err)
|
2020-05-06 08:30:00 -07:00
|
|
|
defer func() {
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(b, os.RemoveAll(chunkDir))
|
2020-05-06 08:30:00 -07:00
|
|
|
}()
|
2021-02-09 06:12:48 -08:00
|
|
|
opts := DefaultHeadOptions()
|
|
|
|
opts.ChunkRange = 1000
|
|
|
|
opts.ChunkDirRoot = chunkDir
|
2021-06-05 07:29:32 -07:00
|
|
|
h, err := NewHead(nil, nil, nil, opts, nil)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(b, err)
|
2019-12-05 23:47:31 -08:00
|
|
|
defer func() {
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(b, h.Close())
|
2019-12-05 23:47:31 -08:00
|
|
|
}()
|
|
|
|
|
2020-07-30 04:36:56 -07:00
|
|
|
app := h.Appender(context.Background())
|
2019-12-05 23:47:31 -08:00
|
|
|
addSeries := func(l labels.Labels) {
|
2021-02-18 04:07:00 -08:00
|
|
|
app.Append(0, l, 0, 0)
|
2019-12-05 23:47:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
for n := 0; n < 10; n++ {
|
|
|
|
for i := 0; i < 100000; i++ {
|
|
|
|
addSeries(labels.FromStrings("i", strconv.Itoa(i)+postingsBenchSuffix, "n", strconv.Itoa(n)+postingsBenchSuffix, "j", "foo"))
|
|
|
|
// Have some series that won't be matched, to properly test inverted matches.
|
|
|
|
addSeries(labels.FromStrings("i", strconv.Itoa(i)+postingsBenchSuffix, "n", strconv.Itoa(n)+postingsBenchSuffix, "j", "bar"))
|
|
|
|
addSeries(labels.FromStrings("i", strconv.Itoa(i)+postingsBenchSuffix, "n", "0_"+strconv.Itoa(n)+postingsBenchSuffix, "j", "bar"))
|
|
|
|
addSeries(labels.FromStrings("i", strconv.Itoa(i)+postingsBenchSuffix, "n", "1_"+strconv.Itoa(n)+postingsBenchSuffix, "j", "bar"))
|
|
|
|
addSeries(labels.FromStrings("i", strconv.Itoa(i)+postingsBenchSuffix, "n", "2_"+strconv.Itoa(n)+postingsBenchSuffix, "j", "foo"))
|
|
|
|
}
|
|
|
|
}
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(b, app.Commit())
|
2019-12-05 23:47:31 -08:00
|
|
|
|
2020-03-25 12:13:47 -07:00
|
|
|
ir, err := h.Index()
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(b, err)
|
2019-12-05 23:47:31 -08:00
|
|
|
b.Run("Head", func(b *testing.B) {
|
|
|
|
benchmarkPostingsForMatchers(b, ir)
|
|
|
|
})
|
|
|
|
|
|
|
|
tmpdir, err := ioutil.TempDir("", "test_benchpostingsformatchers")
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(b, err)
|
2019-12-05 23:47:31 -08:00
|
|
|
defer func() {
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(b, os.RemoveAll(tmpdir))
|
2019-12-05 23:47:31 -08:00
|
|
|
}()
|
|
|
|
|
|
|
|
blockdir := createBlockFromHead(b, tmpdir, h)
|
|
|
|
block, err := OpenBlock(nil, blockdir, nil)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(b, err)
|
2019-12-05 23:47:31 -08:00
|
|
|
defer func() {
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(b, block.Close())
|
2019-12-05 23:47:31 -08:00
|
|
|
}()
|
2020-03-25 12:13:47 -07:00
|
|
|
ir, err = block.Index()
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(b, err)
|
2019-12-05 23:47:31 -08:00
|
|
|
defer ir.Close()
|
|
|
|
b.Run("Block", func(b *testing.B) {
|
|
|
|
benchmarkPostingsForMatchers(b, ir)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkPostingsForMatchers(b *testing.B, ir IndexReader) {
|
|
|
|
n1 := labels.MustNewMatcher(labels.MatchEqual, "n", "1"+postingsBenchSuffix)
|
|
|
|
|
|
|
|
jFoo := labels.MustNewMatcher(labels.MatchEqual, "j", "foo")
|
|
|
|
jNotFoo := labels.MustNewMatcher(labels.MatchNotEqual, "j", "foo")
|
|
|
|
|
|
|
|
iStar := labels.MustNewMatcher(labels.MatchRegexp, "i", "^.*$")
|
2020-06-26 02:49:09 -07:00
|
|
|
i1Star := labels.MustNewMatcher(labels.MatchRegexp, "i", "^1.*$")
|
|
|
|
iStar1 := labels.MustNewMatcher(labels.MatchRegexp, "i", "^.*1$")
|
2020-07-07 01:38:04 -07:00
|
|
|
iStar1Star := labels.MustNewMatcher(labels.MatchRegexp, "i", "^.*1.*$")
|
2019-12-05 23:47:31 -08:00
|
|
|
iPlus := labels.MustNewMatcher(labels.MatchRegexp, "i", "^.+$")
|
|
|
|
i1Plus := labels.MustNewMatcher(labels.MatchRegexp, "i", "^1.+$")
|
|
|
|
iEmptyRe := labels.MustNewMatcher(labels.MatchRegexp, "i", "^$")
|
|
|
|
iNotEmpty := labels.MustNewMatcher(labels.MatchNotEqual, "i", "")
|
|
|
|
iNot2 := labels.MustNewMatcher(labels.MatchNotEqual, "n", "2"+postingsBenchSuffix)
|
|
|
|
iNot2Star := labels.MustNewMatcher(labels.MatchNotRegexp, "i", "^2.*$")
|
2020-07-07 01:38:04 -07:00
|
|
|
iNotStar2Star := labels.MustNewMatcher(labels.MatchNotRegexp, "i", "^.*2.*$")
|
2019-12-05 23:47:31 -08:00
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
matchers []*labels.Matcher
|
|
|
|
}{
|
|
|
|
{`n="1"`, []*labels.Matcher{n1}},
|
|
|
|
{`n="1",j="foo"`, []*labels.Matcher{n1, jFoo}},
|
|
|
|
{`j="foo",n="1"`, []*labels.Matcher{jFoo, n1}},
|
|
|
|
{`n="1",j!="foo"`, []*labels.Matcher{n1, jNotFoo}},
|
|
|
|
{`i=~".*"`, []*labels.Matcher{iStar}},
|
2020-06-26 02:49:09 -07:00
|
|
|
{`i=~"1.*"`, []*labels.Matcher{i1Star}},
|
|
|
|
{`i=~".*1"`, []*labels.Matcher{iStar1}},
|
2019-12-05 23:47:31 -08:00
|
|
|
{`i=~".+"`, []*labels.Matcher{iPlus}},
|
|
|
|
{`i=~""`, []*labels.Matcher{iEmptyRe}},
|
|
|
|
{`i!=""`, []*labels.Matcher{iNotEmpty}},
|
|
|
|
{`n="1",i=~".*",j="foo"`, []*labels.Matcher{n1, iStar, jFoo}},
|
|
|
|
{`n="1",i=~".*",i!="2",j="foo"`, []*labels.Matcher{n1, iStar, iNot2, jFoo}},
|
|
|
|
{`n="1",i!=""`, []*labels.Matcher{n1, iNotEmpty}},
|
|
|
|
{`n="1",i!="",j="foo"`, []*labels.Matcher{n1, iNotEmpty, jFoo}},
|
|
|
|
{`n="1",i=~".+",j="foo"`, []*labels.Matcher{n1, iPlus, jFoo}},
|
|
|
|
{`n="1",i=~"1.+",j="foo"`, []*labels.Matcher{n1, i1Plus, jFoo}},
|
2020-07-07 01:38:04 -07:00
|
|
|
{`n="1",i=~".*1.*",j="foo"`, []*labels.Matcher{n1, iStar1Star, jFoo}},
|
2019-12-05 23:47:31 -08:00
|
|
|
{`n="1",i=~".+",i!="2",j="foo"`, []*labels.Matcher{n1, iPlus, iNot2, jFoo}},
|
|
|
|
{`n="1",i=~".+",i!~"2.*",j="foo"`, []*labels.Matcher{n1, iPlus, iNot2Star, jFoo}},
|
2020-07-07 01:38:04 -07:00
|
|
|
{`n="1",i=~".+",i!~".*2.*",j="foo"`, []*labels.Matcher{n1, iPlus, iNotStar2Star, jFoo}},
|
2019-12-05 23:47:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
b.Run(c.name, func(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
_, err := PostingsForMatchers(ir, c.matchers...)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(b, err)
|
2019-12-05 23:47:31 -08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-12-13 02:33:16 -08:00
|
|
|
|
|
|
|
func BenchmarkQuerierSelect(b *testing.B) {
|
2020-05-06 08:30:00 -07:00
|
|
|
chunkDir, err := ioutil.TempDir("", "chunk_dir")
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(b, err)
|
2020-05-06 08:30:00 -07:00
|
|
|
defer func() {
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(b, os.RemoveAll(chunkDir))
|
2020-05-06 08:30:00 -07:00
|
|
|
}()
|
2021-02-09 06:12:48 -08:00
|
|
|
opts := DefaultHeadOptions()
|
|
|
|
opts.ChunkRange = 1000
|
|
|
|
opts.ChunkDirRoot = chunkDir
|
2021-06-05 07:29:32 -07:00
|
|
|
h, err := NewHead(nil, nil, nil, opts, nil)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(b, err)
|
2019-12-13 02:33:16 -08:00
|
|
|
defer h.Close()
|
2020-07-30 04:36:56 -07:00
|
|
|
app := h.Appender(context.Background())
|
2019-12-13 02:33:16 -08:00
|
|
|
numSeries := 1000000
|
|
|
|
for i := 0; i < numSeries; i++ {
|
2021-02-18 04:07:00 -08:00
|
|
|
app.Append(0, labels.FromStrings("foo", "bar", "i", fmt.Sprintf("%d%s", i, postingsBenchSuffix)), int64(i), 0)
|
2019-12-13 02:33:16 -08:00
|
|
|
}
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(b, app.Commit())
|
2019-12-13 02:33:16 -08:00
|
|
|
|
2020-01-17 03:41:31 -08:00
|
|
|
bench := func(b *testing.B, br BlockReader, sorted bool) {
|
2019-12-13 02:33:16 -08:00
|
|
|
matcher := labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")
|
|
|
|
for s := 1; s <= numSeries; s *= 10 {
|
|
|
|
b.Run(fmt.Sprintf("%dof%d", s, numSeries), func(b *testing.B) {
|
2020-01-17 03:41:31 -08:00
|
|
|
q, err := NewBlockQuerier(br, 0, int64(s-1))
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(b, err)
|
2019-12-13 02:33:16 -08:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-06-09 09:57:31 -07:00
|
|
|
ss := q.Select(sorted, nil, matcher)
|
2019-12-13 02:33:16 -08:00
|
|
|
for ss.Next() {
|
|
|
|
}
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(b, ss.Err())
|
2019-12-13 02:33:16 -08:00
|
|
|
}
|
|
|
|
q.Close()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.Run("Head", func(b *testing.B) {
|
2020-01-17 02:57:52 -08:00
|
|
|
bench(b, h, false)
|
|
|
|
})
|
|
|
|
b.Run("SortedHead", func(b *testing.B) {
|
|
|
|
bench(b, h, true)
|
2019-12-13 02:33:16 -08:00
|
|
|
})
|
|
|
|
|
|
|
|
tmpdir, err := ioutil.TempDir("", "test_benchquerierselect")
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(b, err)
|
2019-12-13 02:33:16 -08:00
|
|
|
defer func() {
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(b, os.RemoveAll(tmpdir))
|
2019-12-13 02:33:16 -08:00
|
|
|
}()
|
|
|
|
|
|
|
|
blockdir := createBlockFromHead(b, tmpdir, h)
|
|
|
|
block, err := OpenBlock(nil, blockdir, nil)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(b, err)
|
2019-12-13 02:33:16 -08:00
|
|
|
defer func() {
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(b, block.Close())
|
2019-12-13 02:33:16 -08:00
|
|
|
}()
|
|
|
|
|
|
|
|
b.Run("Block", func(b *testing.B) {
|
2020-01-17 02:57:52 -08:00
|
|
|
bench(b, block, false)
|
2019-12-13 02:33:16 -08:00
|
|
|
})
|
|
|
|
}
|