2018-11-06 10:19:42 -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.
|
|
|
|
|
2018-10-22 03:52:02 -07:00
|
|
|
package tsdb
|
|
|
|
|
|
|
|
import (
|
2020-05-06 08:30:00 -07:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2018-10-22 03:52:02 -07:00
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
2021-03-18 08:23:50 -07:00
|
|
|
"github.com/pkg/errors"
|
2020-10-29 02:43:23 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-07-30 00:45:42 -07:00
|
|
|
"go.uber.org/atomic"
|
|
|
|
|
2019-11-18 11:53:33 -08:00
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
2018-10-22 03:52:02 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkHeadStripeSeriesCreate(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
|
|
|
}()
|
2018-10-22 03:52:02 -07:00
|
|
|
// Put a series, select it. GC it and then access it.
|
2021-02-09 06:12:48 -08:00
|
|
|
opts := DefaultHeadOptions()
|
|
|
|
opts.ChunkRange = 1000
|
|
|
|
opts.ChunkDirRoot = chunkDir
|
|
|
|
h, err := NewHead(nil, nil, nil, opts)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(b, err)
|
2018-10-22 03:52:02 -07:00
|
|
|
defer h.Close()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
h.getOrCreate(uint64(i), labels.FromStrings("a", strconv.Itoa(i)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkHeadStripeSeriesCreateParallel(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
|
|
|
}()
|
2018-10-22 03:52:02 -07:00
|
|
|
// Put a series, select it. GC it and then access it.
|
2021-02-09 06:12:48 -08:00
|
|
|
opts := DefaultHeadOptions()
|
|
|
|
opts.ChunkRange = 1000
|
|
|
|
opts.ChunkDirRoot = chunkDir
|
|
|
|
h, err := NewHead(nil, nil, nil, opts)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(b, err)
|
2018-10-22 03:52:02 -07:00
|
|
|
defer h.Close()
|
|
|
|
|
2020-07-30 00:45:42 -07:00
|
|
|
var count atomic.Int64
|
2018-10-22 03:52:02 -07:00
|
|
|
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
2020-07-30 00:45:42 -07:00
|
|
|
i := count.Inc()
|
2018-10-22 03:52:02 -07:00
|
|
|
h.getOrCreate(uint64(i), labels.FromStrings("a", strconv.Itoa(int(i))))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-03-18 08:23:50 -07:00
|
|
|
|
|
|
|
func BenchmarkHeadStripeSeriesCreate_PreCreationFailure(b *testing.B) {
|
|
|
|
chunkDir, err := ioutil.TempDir("", "chunk_dir")
|
|
|
|
require.NoError(b, err)
|
|
|
|
defer func() {
|
|
|
|
require.NoError(b, os.RemoveAll(chunkDir))
|
|
|
|
}()
|
|
|
|
// Put a series, select it. GC it and then access it.
|
|
|
|
opts := DefaultHeadOptions()
|
|
|
|
opts.ChunkRange = 1000
|
|
|
|
opts.ChunkDirRoot = chunkDir
|
|
|
|
|
|
|
|
// Mock the PreCreation() callback to fail on each series.
|
|
|
|
opts.SeriesCallback = failingSeriesLifecycleCallback{}
|
|
|
|
|
|
|
|
h, err := NewHead(nil, nil, nil, opts)
|
|
|
|
require.NoError(b, err)
|
|
|
|
defer h.Close()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
h.getOrCreate(uint64(i), labels.FromStrings("a", strconv.Itoa(i)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type failingSeriesLifecycleCallback struct{}
|
|
|
|
|
|
|
|
func (failingSeriesLifecycleCallback) PreCreation(labels.Labels) error { return errors.New("failed") }
|
|
|
|
func (failingSeriesLifecycleCallback) PostCreation(labels.Labels) {}
|
|
|
|
func (failingSeriesLifecycleCallback) PostDeletion(...labels.Labels) {}
|