From e33ed5d0c1863a5bb3ce6bd52d781d69cee297cf Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sat, 14 Feb 2026 21:07:30 +0100 Subject: [PATCH] index: make tests more representative --- internal/repository/index/index_test.go | 34 +++++++++++++++++-------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/internal/repository/index/index_test.go b/internal/repository/index/index_test.go index 30a662a37..bdc666d3f 100644 --- a/internal/repository/index/index_test.go +++ b/internal/repository/index/index_test.go @@ -463,22 +463,34 @@ func createRandomIndex(rng *rand.Rand, packfiles int) (idx *index.Index, lookupB func BenchmarkIndexHasUnknown(b *testing.B) { idx, _ := createRandomIndex(rand.New(rand.NewSource(0)), 200000) - lookupBh := restic.NewRandomBlobHandle() + handles := make([]restic.BlobHandle, 0, 100000) + for i := 0; i < cap(handles); i++ { + handles = append(handles, restic.NewRandomBlobHandle()) + } - b.ResetTimer() - - for i := 0; i < b.N; i++ { - idx.Has(lookupBh) + for b.Loop() { + // use multiple handles to reduce cache effects + for _, handle := range handles { + idx.Has(handle) + } } } func BenchmarkIndexHasKnown(b *testing.B) { - idx, lookupBh := createRandomIndex(rand.New(rand.NewSource(0)), 200000) + idx, _ := createRandomIndex(rand.New(rand.NewSource(0)), 200000) + handles := make([]restic.BlobHandle, 0, 100000) + for handle := range idx.Values() { + handles = append(handles, handle.BlobHandle) + if len(handles) == cap(handles) { + break + } + } - b.ResetTimer() - - for i := 0; i < b.N; i++ { - idx.Has(lookupBh) + for b.Loop() { + // use multiple handles to reduce cache effects + for _, handle := range handles { + idx.Has(handle) + } } } @@ -486,7 +498,7 @@ func BenchmarkIndexAlloc(b *testing.B) { rng := rand.New(rand.NewSource(0)) b.ReportAllocs() - for i := 0; i < b.N; i++ { + for b.Loop() { createRandomIndex(rng, 200000) } }