From 3b7a3711e6bf6131e1cb874f4120cdf58a78bba6 Mon Sep 17 00:00:00 2001 From: Alexander Weiss Date: Sat, 4 Jul 2020 07:05:51 +0200 Subject: [PATCH] Add more realistic index benchmarks + reduce test size of BenchmarkMasterIndexLookupParallel --- internal/repository/index_test.go | 16 ++++---- internal/repository/master_index_test.go | 47 +++++++++--------------- 2 files changed, 25 insertions(+), 38 deletions(-) diff --git a/internal/repository/index_test.go b/internal/repository/index_test.go index 513f4c5d1..07328058c 100644 --- a/internal/repository/index_test.go +++ b/internal/repository/index_test.go @@ -336,7 +336,7 @@ var ( ) func initBenchmarkIndexJSON() { - idx, _ := createRandomIndex(rand.New(rand.NewSource(0))) + idx, _ := createRandomIndex(rand.New(rand.NewSource(0)), 200000) var buf bytes.Buffer idx.Encode(&buf) benchmarkIndexJSON = buf.Bytes() @@ -418,11 +418,11 @@ func NewRandomTestID(rng *rand.Rand) restic.ID { return id } -func createRandomIndex(rng *rand.Rand) (idx *repository.Index, lookupID restic.ID) { +func createRandomIndex(rng *rand.Rand, packfiles int) (idx *repository.Index, lookupID restic.ID) { idx = repository.NewIndex() - // create index with 200k pack files - for i := 0; i < 200000; i++ { + // create index with given number of pack files + for i := 0; i < packfiles; i++ { packID := NewRandomTestID(rng) var blobs []restic.Blob offset := 0 @@ -449,7 +449,7 @@ func createRandomIndex(rng *rand.Rand) (idx *repository.Index, lookupID restic.I } func BenchmarkIndexHasUnknown(b *testing.B) { - idx, _ := createRandomIndex(rand.New(rand.NewSource(0))) + idx, _ := createRandomIndex(rand.New(rand.NewSource(0)), 200000) lookupID := restic.NewRandomID() b.ResetTimer() @@ -460,7 +460,7 @@ func BenchmarkIndexHasUnknown(b *testing.B) { } func BenchmarkIndexHasKnown(b *testing.B) { - idx, lookupID := createRandomIndex(rand.New(rand.NewSource(0))) + idx, lookupID := createRandomIndex(rand.New(rand.NewSource(0)), 200000) b.ResetTimer() @@ -474,7 +474,7 @@ func BenchmarkIndexAlloc(b *testing.B) { b.ReportAllocs() for i := 0; i < b.N; i++ { - createRandomIndex(rng) + createRandomIndex(rng, 200000) } } @@ -484,7 +484,7 @@ func BenchmarkIndexAllocParallel(b *testing.B) { b.RunParallel(func(pb *testing.PB) { rng := rand.New(rand.NewSource(0)) for pb.Next() { - createRandomIndex(rng) + createRandomIndex(rng, 200000) } }) } diff --git a/internal/repository/master_index_test.go b/internal/repository/master_index_test.go index 3b858253a..3d72bc4d1 100644 --- a/internal/repository/master_index_test.go +++ b/internal/repository/master_index_test.go @@ -57,11 +57,19 @@ func TestMasterIndexLookup(t *testing.T) { rtest.Assert(t, blobs == nil, "Expected no blobs when fetching with a random id") } -func BenchmarkMasterIndexLookupSingleIndex(b *testing.B) { - idx1, lookupID := createRandomIndex(rand.New(rand.NewSource(0))) - +func createRandomMasterIndex(rng *rand.Rand, num, size int) (*repository.MasterIndex, restic.ID) { mIdx := repository.NewMasterIndex() + for i := 0; i < num-1; i++ { + idx, _ := createRandomIndex(rng, size) + mIdx.Insert(idx) + } + idx1, lookupID := createRandomIndex(rng, size) mIdx.Insert(idx1) + return mIdx, lookupID +} + +func BenchmarkMasterIndexLookupSingleIndex(b *testing.B) { + mIdx, lookupID := createRandomMasterIndex(rand.New(rand.NewSource(0)), 1, 200000) b.ResetTimer() @@ -71,16 +79,7 @@ func BenchmarkMasterIndexLookupSingleIndex(b *testing.B) { } func BenchmarkMasterIndexLookupMultipleIndex(b *testing.B) { - rng := rand.New(rand.NewSource(0)) - mIdx := repository.NewMasterIndex() - - for i := 0; i < 5; i++ { - idx, _ := createRandomIndex(rng) - mIdx.Insert(idx) - } - - idx1, lookupID := createRandomIndex(rng) - mIdx.Insert(idx1) + mIdx, lookupID := createRandomMasterIndex(rand.New(rand.NewSource(0)), 100, 10000) b.ResetTimer() @@ -90,11 +89,9 @@ func BenchmarkMasterIndexLookupMultipleIndex(b *testing.B) { } func BenchmarkMasterIndexLookupSingleIndexUnknown(b *testing.B) { - lookupID := restic.NewRandomID() - idx1, _ := createRandomIndex(rand.New(rand.NewSource(0))) - mIdx := repository.NewMasterIndex() - mIdx.Insert(idx1) + lookupID := restic.NewRandomID() + mIdx, _ := createRandomMasterIndex(rand.New(rand.NewSource(0)), 1, 200000) b.ResetTimer() @@ -104,14 +101,8 @@ func BenchmarkMasterIndexLookupSingleIndexUnknown(b *testing.B) { } func BenchmarkMasterIndexLookupMultipleIndexUnknown(b *testing.B) { - rng := rand.New(rand.NewSource(0)) lookupID := restic.NewRandomID() - mIdx := repository.NewMasterIndex() - - for i := 0; i < 6; i++ { - idx, _ := createRandomIndex(rng) - mIdx.Insert(idx) - } + mIdx, _ := createRandomMasterIndex(rand.New(rand.NewSource(0)), 100, 10000) b.ResetTimer() @@ -123,16 +114,12 @@ func BenchmarkMasterIndexLookupMultipleIndexUnknown(b *testing.B) { func BenchmarkMasterIndexLookupParallel(b *testing.B) { mIdx := repository.NewMasterIndex() - for _, numindices := range []int{5, 10, 20} { + for _, numindices := range []int{25, 50, 100} { var lookupID restic.ID b.StopTimer() rng := rand.New(rand.NewSource(0)) - for i := 0; i < numindices; i++ { - var idx *repository.Index - idx, lookupID = createRandomIndex(rng) - mIdx.Insert(idx) - } + mIdx, lookupID = createRandomMasterIndex(rng, numindices, 10000) b.StartTimer() name := fmt.Sprintf("known,indices=%d", numindices)