Skip to content

[TESTS] TSDB: Faster WAL benchmarks#16440

Merged
bboreham merged 2 commits intoprometheus:mainfrom
bboreham:faster-benchmark-loadwls
Apr 22, 2025
Merged

[TESTS] TSDB: Faster WAL benchmarks#16440
bboreham merged 2 commits intoprometheus:mainfrom
bboreham:faster-benchmark-loadwls

Conversation

@bboreham
Copy link
Member

  • Reuse buffer for writing the WAL records. Less garbage collection, goes faster.
  • More efficient label creation - use the Builder abstraction instead of going via a map.

Before:

go test -run XXX -bench 'BenchmarkLoadWLs/batches=100,seriesPerBatch=1000,samplesPerSeries=480,exemplarsPerSeries=0,mmappedChunkT=3800,oooSeriesPct=0.200,oooSamplesPct=
0.300,oooCapMax=32,missingSeriesPct=0.000' ./tsdb/
goos: linux
goarch: amd64
pkg: github.com/prometheus/prometheus/tsdb
cpu: Intel(R) Core(TM) i7-14700K
BenchmarkLoadWLs/batches=100,seriesPerBatch=1000,samplesPerSeries=480,exemplarsPerSeries=0,mmappedChunkT=3800,oooSeriesPct=0.200,oooSamplesPct=0.300,oooCapMax=32,missingSeriesPct=0.000-28                    1        3104853168 ns/op
PASS
ok      github.com/prometheus/prometheus/tsdb   4.310s

After:

go test -run XXX -bench 'BenchmarkLoadWLs/batches=100,seriesPerBatch=1000,samplesPerSeries=480,exemplarsPerSeries=0,mmappedChunkT=3800,oooSeriesPct=0.200,oooSamplesPct=0.300,oooCapMax=32,missingSeriesPct=0.000' ./tsdb/
goos: linux
goarch: amd64
pkg: github.com/prometheus/prometheus/tsdb
cpu: Intel(R) Core(TM) i7-14700K
BenchmarkLoadWLs/batches=100,seriesPerBatch=1000,samplesPerSeries=480,exemplarsPerSeries=0,mmappedChunkT=3800,oooSeriesPct=0.200,oooSamplesPct=0.300,oooCapMax=32,missingSeriesPct=0.000-28                    1        3024799543 ns/op
PASS
ok      github.com/prometheus/prometheus/tsdb   3.863s

It makes even more of a difference if you push the number of series up.

Less garbage collection.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
Use the Builder abstraction instead of going via a map.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
@bboreham bboreham requested a review from jesusvazquez as a code owner April 16, 2025 17:14
Copy link
Member

@machine424 machine424 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran

diff --git a/tsdb/head_test.go b/tsdb/head_test.go
index 0e9fac18b..1e5d17c9a 100644
--- a/tsdb/head_test.go
+++ b/tsdb/head_test.go
@@ -436,6 +436,9 @@ func BenchmarkLoadWLs(b *testing.B) {
 
                                                b.ResetTimer()
 
+                                               // hash
+                                               fmt.Println(b.Name(), testutil.DirHash(b, dir))
+
                                                // Load the WAL.
                                                for i := 0; i < b.N; i++ {
                                                        opts := DefaultHeadOptions()
diff --git a/util/testutil/directory.go b/util/testutil/directory.go
index 38dabd183..108a04f50 100644
--- a/util/testutil/directory.go
+++ b/util/testutil/directory.go
@@ -131,7 +131,7 @@ func NewTemporaryDirectory(name string, t T) (handler TemporaryDirectory) {
 }
 
 // DirHash returns a hash of all files attributes and their content within a directory.
-func DirHash(t *testing.T, path string) []byte {
+func DirHash(t testing.TB, path string) []byte {
        hash := sha256.New()
        err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
                require.NoError(t, err)
@@ -149,14 +149,16 @@ func DirHash(t *testing.T, path string) []byte {
                _, err = io.WriteString(hash, strconv.Itoa(int(info.Size())))
                require.NoError(t, err)
 
-               _, err = io.WriteString(hash, info.Name())
-               require.NoError(t, err)
+               //_, err = io.WriteString(hash, info.Name())
+               //require.NoError(t, err)
 
-               modTime, err := info.ModTime().GobEncode()
-               require.NoError(t, err)
+               //modTime, err := info.ModTime().GobEncode()
+               //require.NoError(t, err)
 
-               _, err = hash.Write(modTime)
-               require.NoError(t, err)
+               //_, err = hash.Write(modTime)
+               //
+               // 
+               // require.NoError(t, err)
                return nil
        })
        require.NoError(t, err)

on both for some cases and it seems we still produce the same segments.
lgtm

@bboreham bboreham merged commit 8487ed8 into prometheus:main Apr 22, 2025
27 checks passed
@bboreham bboreham deleted the faster-benchmark-loadwls branch April 22, 2025 14:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants