prometheus/tsdb/record/buffers_test.go
Bartlomiej Plotka a73202012b
tsdb/wlog[PERF]: optimize WAL watcher reads (up to 540x less B/op; 13000x less allocs/op) (#18250)
See the detailed analysis https://docs.google.com/document/d/1efVAMcEw7-R_KatHHcobcFBlNsre-DoThVHI8AO2SDQ/edit?tab=t.0

I ran extensive benchmarks using synthetic data as well as real WAL segments pulled from the prombench runs.

All benchmarks are here https://github.com/prometheus/prometheus/compare/bwplotka/wal-reuse?expand=1

* optimization(tsdb/wlog): reuse Ref* buffers across WAL watchers' reads

Signed-off-by: bwplotka <bwplotka@gmail.com>

* optimization(tsdb/wlog): avoid expensive error wraps

Signed-off-by: bwplotka <bwplotka@gmail.com>

* optimization(tsdb/wlog): reuse array for filtering

Signed-off-by: bwplotka <bwplotka@gmail.com>

* fmt

Signed-off-by: bwplotka <bwplotka@gmail.com>

* lint fix

Signed-off-by: bwplotka <bwplotka@gmail.com>

* tsdb/record: add test for clear() on histograms

Signed-off-by: bwplotka <bwplotka@gmail.com>

* updated WriteTo with what's currently expected

Signed-off-by: bwplotka <bwplotka@gmail.com>

---------

Signed-off-by: bwplotka <bwplotka@gmail.com>
2026-03-11 09:17:13 +00:00

51 lines
1.4 KiB
Go

// Copyright 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 record
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/prometheus/prometheus/model/histogram"
)
func TestBuffersPool_PtrClear(t *testing.T) {
pool := NewBuffersPool()
h := pool.GetHistograms(1)
h = append(h, RefHistogramSample{
H: &histogram.Histogram{Schema: 1244124},
})
pool.PutHistograms(h)
h2 := pool.GetHistograms(1)
require.Empty(t, h2)
require.Equal(t, 1, cap(h2))
h2 = h2[:1] // extend to capacity to check previously stored item
require.Nil(t, h2[0].H)
fh := pool.GetFloatHistograms(1)
fh = append(fh, RefFloatHistogramSample{
FH: &histogram.FloatHistogram{Schema: 1244521},
})
pool.PutFloatHistograms(fh)
fh2 := pool.GetFloatHistograms(1)
require.Empty(t, fh2)
require.Equal(t, 1, cap(fh2))
fh2 = fh2[:1] // extend to capacity
require.Nil(t, fh2[0].FH)
}