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
1 changed files with 12 additions and 5 deletions

View File

@ -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()