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.
This commit is contained in:
Matthew Dawson 2018-01-13 19:43:37 -05:00
parent b63de7c798
commit ebce4b2581
No known key found for this signature in database
GPG key ID: 404D7F645F682028

View file

@ -383,16 +383,23 @@ func TestIndexPacks(t *testing.T) {
const maxPackSize = 16 * 1024 * 1024 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() idx = repository.NewIndex()
// create index with 200k pack files // create index with 200k pack files
for i := 0; i < 200000; i++ { for i := 0; i < 200000; i++ {
packID := restic.NewRandomID() packID := NewRandomTestID(rng)
offset := 0 offset := 0
for offset < maxPackSize { for offset < maxPackSize {
size := 2000 + rand.Intn(4*1024*1024) size := 2000 + rand.Intn(4*1024*1024)
id := restic.NewRandomID() id := NewRandomTestID(rng)
idx.Store(restic.PackedBlob{ idx.Store(restic.PackedBlob{
PackID: packID, PackID: packID,
Blob: restic.Blob{ Blob: restic.Blob{
@ -415,7 +422,7 @@ func createRandomIndex() (idx *repository.Index, lookupID restic.ID) {
} }
func BenchmarkIndexHasUnknown(b *testing.B) { func BenchmarkIndexHasUnknown(b *testing.B) {
idx, _ := createRandomIndex() idx, _ := createRandomIndex(rand.New(rand.NewSource(0)))
lookupID := restic.NewRandomID() lookupID := restic.NewRandomID()
b.ResetTimer() b.ResetTimer()
@ -426,7 +433,7 @@ func BenchmarkIndexHasUnknown(b *testing.B) {
} }
func BenchmarkIndexHasKnown(b *testing.B) { func BenchmarkIndexHasKnown(b *testing.B) {
idx, lookupID := createRandomIndex() idx, lookupID := createRandomIndex(rand.New(rand.NewSource(0)))
b.ResetTimer() b.ResetTimer()