gitlab.com/SkynetLabs/skyd@v1.6.9/skymodules/renter/filesystem/siafile/snapshot_test.go (about) 1 package siafile 2 3 import ( 4 "bytes" 5 "reflect" 6 "testing" 7 8 "gitlab.com/SkynetLabs/skyd/skymodules" 9 "go.sia.tech/siad/crypto" 10 "go.sia.tech/siad/types" 11 ) 12 13 // TestSnapshot tests if a snapshot is created correctly from a SiaFile. 14 func TestSnapshot(t *testing.T) { 15 if testing.Short() { 16 t.SkipNow() 17 } 18 t.Parallel() 19 20 // Create a random file for testing and create a snapshot from it. 21 sf := newTestFile() 22 sp := skymodules.RandomSiaPath() 23 snap, err := sf.Snapshot(sp) 24 if err != nil { 25 t.Fatal(err) 26 } 27 28 // Make sure the snapshot has the same fields as the SiaFile. 29 if sf.numChunks != len(snap.staticChunks) { 30 t.Errorf("expected %v chunks but got %v", sf.numChunks, len(snap.staticChunks)) 31 } 32 if sf.staticMetadata.FileSize != snap.staticFileSize { 33 t.Errorf("staticFileSize was %v but should be %v", 34 snap.staticFileSize, sf.staticMetadata.FileSize) 35 } 36 if sf.staticMetadata.StaticPieceSize != snap.staticPieceSize { 37 t.Errorf("staticPieceSize was %v but should be %v", 38 snap.staticPieceSize, sf.staticMetadata.StaticPieceSize) 39 } 40 if sf.staticMetadata.staticErasureCode.MinPieces() != snap.staticErasureCode.MinPieces() { 41 t.Errorf("minPieces was %v but should be %v", 42 sf.staticMetadata.staticErasureCode.MinPieces(), snap.staticErasureCode.MinPieces()) 43 } 44 if sf.staticMetadata.staticErasureCode.NumPieces() != snap.staticErasureCode.NumPieces() { 45 t.Errorf("numPieces was %v but should be %v", 46 sf.staticMetadata.staticErasureCode.NumPieces(), snap.staticErasureCode.NumPieces()) 47 } 48 if !bytes.Equal(sf.staticMetadata.StaticMasterKey, snap.staticMasterKey.Key()) { 49 t.Error("masterkeys don't match") 50 } 51 if sf.staticMetadata.StaticMasterKeyType != snap.staticMasterKey.Type() { 52 t.Error("masterkey types don't match") 53 } 54 if sf.staticMetadata.Mode != snap.staticMode { 55 t.Error("modes don't match") 56 } 57 if len(sf.pubKeyTable) > 0 && len(snap.staticPubKeyTable) > 0 && 58 !reflect.DeepEqual(sf.pubKeyTable, snap.staticPubKeyTable) { 59 t.Error("pubkeytables don't match", sf.pubKeyTable, snap.staticPubKeyTable) 60 } 61 if sp != snap.staticSiaPath { 62 t.Error("siapaths don't match") 63 } 64 // Compare the pieces. 65 err = sf.iterateChunksReadonly(func(chunk chunk) error { 66 sfPieces, err := sf.Pieces(uint64(chunk.Index)) 67 if err != nil { 68 t.Fatal(err) 69 } 70 snapPieces := snap.Pieces(uint64(chunk.Index)) 71 if !reflect.DeepEqual(sfPieces, snapPieces) { 72 t.Error("Pieces don't match") 73 } 74 return nil 75 }) 76 if err != nil { 77 t.Fatal(err) 78 } 79 } 80 81 // BenchmarkSnapshot10MB benchmarks the creation of snapshots for Siafiles 82 // which hold the metadata of a 10 MB file. 83 func BenchmarkSnapshot10MB(b *testing.B) { 84 benchmarkSnapshot(b, uint64(1e7)) 85 } 86 87 // BenchmarkSnapshot100MB benchmarks the creation of snapshots for Siafiles 88 // which hold the metadata of a 100 MB file. 89 func BenchmarkSnapshot100MB(b *testing.B) { 90 benchmarkSnapshot(b, uint64(1e8)) 91 } 92 93 // BenchmarkSnapshot1GB benchmarks the creation of snapshots for Siafiles 94 // which hold the metadata of a 1 GB file. 95 func BenchmarkSnapshot1GB(b *testing.B) { 96 benchmarkSnapshot(b, uint64(1e9)) 97 } 98 99 // BenchmarkSnapshot10GB benchmarks the creation of snapshots for Siafiles 100 // which hold the metadata of a 10 GB file. 101 func BenchmarkSnapshot10GB(b *testing.B) { 102 benchmarkSnapshot(b, uint64(1e10)) 103 } 104 105 // benchmarkSnapshot is a helper function for benchmarking the creation of 106 // snapshots of Siafiles with different sizes. 107 func benchmarkSnapshot(b *testing.B, fileSize uint64) { 108 // Create the file. 109 sf := newBlankTestFile() 110 rc := sf.staticMetadata.staticErasureCode 111 // Add a host key to the table. 112 sf.addRandomHostKeys(1) 113 // Add numPieces to each chunk. 114 for i := uint64(0); i < sf.NumChunks(); i++ { 115 for j := uint64(0); j < uint64(rc.NumPieces()); j++ { 116 if err := sf.AddPiece(types.SiaPublicKey{}, i, j, crypto.Hash{}); err != nil { 117 b.Fatal(err) 118 } 119 } 120 } 121 // Reset the timer. 122 b.ResetTimer() 123 124 // Create snapshots as fast as possible. 125 for i := 0; i < b.N; i++ { 126 _, err := sf.Snapshot(skymodules.RootSiaPath()) 127 if err != nil { 128 b.Fatal(err) 129 } 130 } 131 }