From ebce4b2581d2899fb6adbf9a6971045723108cb8 Mon Sep 17 00:00:00 2001 From: Matthew Dawson Date: Sat, 13 Jan 2018 19:43:37 -0500 Subject: [PATCH] repository/index: Speed up benchmarks and tests When setting up the index used for benchmarking, use math/rand instead of crypto/rand since the generated ids don't need to be evenly distributed, and not be secure against guessing. As such, use a different random id function (only available during tests) that uses math/rand instead. --- internal/repository/index_test.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/internal/repository/index_test.go b/internal/repository/index_test.go index f2e0746c0..e5ed6489d 100644 --- a/internal/repository/index_test.go +++ b/internal/repository/index_test.go @@ -383,16 +383,23 @@ func TestIndexPacks(t *testing.T) { const maxPackSize = 16 * 1024 * 1024 -func createRandomIndex() (idx *repository.Index, lookupID restic.ID) { +// This function generates a (insecure) random ID, similar to NewRandomID +func NewRandomTestID(rng *rand.Rand) restic.ID { + id := restic.ID{} + rng.Read(id[:]) + return id +} + +func createRandomIndex(rng *rand.Rand) (idx *repository.Index, lookupID restic.ID) { idx = repository.NewIndex() // create index with 200k pack files for i := 0; i < 200000; i++ { - packID := restic.NewRandomID() + packID := NewRandomTestID(rng) offset := 0 for offset < maxPackSize { size := 2000 + rand.Intn(4*1024*1024) - id := restic.NewRandomID() + id := NewRandomTestID(rng) idx.Store(restic.PackedBlob{ PackID: packID, Blob: restic.Blob{ @@ -415,7 +422,7 @@ func createRandomIndex() (idx *repository.Index, lookupID restic.ID) { } func BenchmarkIndexHasUnknown(b *testing.B) { - idx, _ := createRandomIndex() + idx, _ := createRandomIndex(rand.New(rand.NewSource(0))) lookupID := restic.NewRandomID() b.ResetTimer() @@ -426,7 +433,7 @@ func BenchmarkIndexHasUnknown(b *testing.B) { } func BenchmarkIndexHasKnown(b *testing.B) { - idx, lookupID := createRandomIndex() + idx, lookupID := createRandomIndex(rand.New(rand.NewSource(0))) b.ResetTimer()