gitlab.com/SiaPrime/SiaPrime@v1.4.1/modules/renter/siafile/snapshot_test.go (about)

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