github.com/mckael/restic@v0.8.3/internal/repository/master_index_test.go (about) 1 package repository_test 2 3 import ( 4 "math/rand" 5 "testing" 6 7 "github.com/restic/restic/internal/repository" 8 "github.com/restic/restic/internal/restic" 9 rtest "github.com/restic/restic/internal/test" 10 ) 11 12 func TestMasterIndexLookup(t *testing.T) { 13 idInIdx1 := restic.NewRandomID() 14 idInIdx2 := restic.NewRandomID() 15 16 blob1 := restic.PackedBlob{ 17 PackID: restic.NewRandomID(), 18 Blob: restic.Blob{ 19 Type: restic.DataBlob, 20 ID: idInIdx1, 21 Length: 10, 22 Offset: 0, 23 }, 24 } 25 26 blob2 := restic.PackedBlob{ 27 PackID: restic.NewRandomID(), 28 Blob: restic.Blob{ 29 Type: restic.DataBlob, 30 ID: idInIdx2, 31 Length: 100, 32 Offset: 10, 33 }, 34 } 35 36 idx1 := repository.NewIndex() 37 idx1.Store(blob1) 38 39 idx2 := repository.NewIndex() 40 idx2.Store(blob2) 41 42 mIdx := repository.NewMasterIndex() 43 mIdx.Insert(idx1) 44 mIdx.Insert(idx2) 45 46 blobs, found := mIdx.Lookup(idInIdx1, restic.DataBlob) 47 rtest.Assert(t, found, "Expected to find blob id %v from index 1", idInIdx1) 48 rtest.Equals(t, []restic.PackedBlob{blob1}, blobs) 49 50 blobs, found = mIdx.Lookup(idInIdx2, restic.DataBlob) 51 rtest.Assert(t, found, "Expected to find blob id %v from index 2", idInIdx2) 52 rtest.Equals(t, []restic.PackedBlob{blob2}, blobs) 53 54 blobs, found = mIdx.Lookup(restic.NewRandomID(), restic.DataBlob) 55 rtest.Assert(t, !found, "Expected to not find a blob when fetching with a random id") 56 rtest.Assert(t, blobs == nil, "Expected no blobs when fetching with a random id") 57 } 58 59 func BenchmarkMasterIndexLookupSingleIndex(b *testing.B) { 60 idx1, lookupID := createRandomIndex(rand.New(rand.NewSource(0))) 61 62 mIdx := repository.NewMasterIndex() 63 mIdx.Insert(idx1) 64 65 b.ResetTimer() 66 67 for i := 0; i < b.N; i++ { 68 mIdx.Lookup(lookupID, restic.DataBlob) 69 } 70 } 71 72 func BenchmarkMasterIndexLookupMultipleIndex(b *testing.B) { 73 rng := rand.New(rand.NewSource(0)) 74 mIdx := repository.NewMasterIndex() 75 76 for i := 0; i < 5; i++ { 77 idx, _ := createRandomIndex(rand.New(rng)) 78 mIdx.Insert(idx) 79 } 80 81 idx1, lookupID := createRandomIndex(rand.New(rng)) 82 mIdx.Insert(idx1) 83 84 b.ResetTimer() 85 86 for i := 0; i < b.N; i++ { 87 mIdx.Lookup(lookupID, restic.DataBlob) 88 } 89 } 90 91 func BenchmarkMasterIndexLookupSingleIndexUnknown(b *testing.B) { 92 lookupID := restic.NewRandomID() 93 idx1, _ := createRandomIndex(rand.New(rand.NewSource(0))) 94 95 mIdx := repository.NewMasterIndex() 96 mIdx.Insert(idx1) 97 98 b.ResetTimer() 99 100 for i := 0; i < b.N; i++ { 101 mIdx.Lookup(lookupID, restic.DataBlob) 102 } 103 } 104 105 func BenchmarkMasterIndexLookupMultipleIndexUnknown(b *testing.B) { 106 rng := rand.New(rand.NewSource(0)) 107 lookupID := restic.NewRandomID() 108 mIdx := repository.NewMasterIndex() 109 110 for i := 0; i < 5; i++ { 111 idx, _ := createRandomIndex(rand.New(rng)) 112 mIdx.Insert(idx) 113 } 114 115 idx1, _ := createRandomIndex(rand.New(rng)) 116 mIdx.Insert(idx1) 117 118 b.ResetTimer() 119 120 for i := 0; i < b.N; i++ { 121 mIdx.Lookup(lookupID, restic.DataBlob) 122 } 123 }