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 (
|
2023-11-16 10:54:41 -08:00
|
|
|
"errors"
|
2018-10-22 03:52:02 -07:00
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
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"
|
|
|
|
|
2021-11-08 06:23:17 -08:00
|
|
|
"github.com/prometheus/prometheus/model/labels"
|
2023-08-03 01:56:27 -07:00
|
|
|
"github.com/prometheus/prometheus/tsdb/chunks"
|
2018-10-22 03:52:02 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkHeadStripeSeriesCreate(b *testing.B) {
|
2022-01-22 01:55:01 -08:00
|
|
|
chunkDir := b.TempDir()
|
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
|
2022-09-20 10:05:50 -07:00
|
|
|
h, err := NewHead(nil, nil, nil, nil, opts, nil)
|
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++ {
|
2024-10-11 06:21:15 -07:00
|
|
|
h.getOrCreate(uint64(i), labels.FromStrings(labels.MetricName, "test", "a", strconv.Itoa(i), "b", strconv.Itoa(i%10), "c", strconv.Itoa(i%100), "d", strconv.Itoa(i/2), "e", strconv.Itoa(i/4)))
|
2018-10-22 03:52:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkHeadStripeSeriesCreateParallel(b *testing.B) {
|
2022-01-22 01:55:01 -08:00
|
|
|
chunkDir := b.TempDir()
|
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
|
2022-09-20 10:05:50 -07:00
|
|
|
h, err := NewHead(nil, nil, nil, nil, opts, nil)
|
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() {
|
2024-10-11 06:21:15 -07:00
|
|
|
i := int(count.Inc())
|
|
|
|
h.getOrCreate(uint64(i), labels.FromStrings(labels.MetricName, "test", "a", strconv.Itoa(i), "b", strconv.Itoa(i%10), "c", strconv.Itoa(i%100), "d", strconv.Itoa(i/2), "e", strconv.Itoa(i/4)))
|
2018-10-22 03:52:02 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-03-18 08:23:50 -07:00
|
|
|
|
|
|
|
func BenchmarkHeadStripeSeriesCreate_PreCreationFailure(b *testing.B) {
|
2022-01-22 01:55:01 -08:00
|
|
|
chunkDir := b.TempDir()
|
2021-03-18 08:23:50 -07:00
|
|
|
// 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{}
|
|
|
|
|
2022-09-20 10:05:50 -07:00
|
|
|
h, err := NewHead(nil, nil, nil, nil, opts, nil)
|
2021-03-18 08:23:50 -07:00
|
|
|
require.NoError(b, err)
|
|
|
|
defer h.Close()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2024-10-11 06:21:15 -07:00
|
|
|
h.getOrCreate(uint64(i), labels.FromStrings(labels.MetricName, "test", "a", strconv.Itoa(i), "b", strconv.Itoa(i%10), "c", strconv.Itoa(i%100), "d", strconv.Itoa(i/2), "e", strconv.Itoa(i/4)))
|
2021-03-18 08:23:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type failingSeriesLifecycleCallback struct{}
|
|
|
|
|
2023-08-03 01:56:27 -07:00
|
|
|
func (failingSeriesLifecycleCallback) PreCreation(labels.Labels) error { return errors.New("failed") }
|
|
|
|
func (failingSeriesLifecycleCallback) PostCreation(labels.Labels) {}
|
|
|
|
func (failingSeriesLifecycleCallback) PostDeletion(map[chunks.HeadSeriesRef]labels.Labels) {}
|