Reduce memory consuption of TestCreateSnapshot

This commit is contained in:
Alexander Neumann 2017-01-17 12:56:20 +01:00
parent 73ad3d418d
commit c4f44c7bcb
2 changed files with 28 additions and 8 deletions

View file

@ -23,16 +23,26 @@ type fakeFileSystem struct {
repo Repository repo Repository
knownBlobs IDSet knownBlobs IDSet
duplication float32 duplication float32
buf []byte
chunker *chunker.Chunker
} }
// saveFile reads from rd and saves the blobs in the repository. The list of // saveFile reads from rd and saves the blobs in the repository. The list of
// IDs is returned. // IDs is returned.
func (fs fakeFileSystem) saveFile(rd io.Reader) (blobs IDs) { func (fs *fakeFileSystem) saveFile(rd io.Reader) (blobs IDs) {
blobs = IDs{} if fs.buf == nil {
ch := chunker.New(rd, fs.repo.Config().ChunkerPolynomial) fs.buf = make([]byte, chunker.MaxSize)
}
if fs.chunker == nil {
fs.chunker = chunker.New(rd, fs.repo.Config().ChunkerPolynomial)
} else {
fs.chunker.Reset(rd, fs.repo.Config().ChunkerPolynomial)
}
blobs = IDs{}
for { for {
chunk, err := ch.Next(getBuf()) chunk, err := fs.chunker.Next(fs.buf)
if errors.Cause(err) == io.EOF { if errors.Cause(err) == io.EOF {
break break
} }
@ -50,7 +60,6 @@ func (fs fakeFileSystem) saveFile(rd io.Reader) (blobs IDs) {
fs.knownBlobs.Insert(id) fs.knownBlobs.Insert(id)
} }
freeBuf(chunk.Data)
blobs = append(blobs, id) blobs = append(blobs, id)
} }
@ -64,7 +73,7 @@ const (
maxNodes = 32 maxNodes = 32
) )
func (fs fakeFileSystem) treeIsKnown(tree *Tree) (bool, []byte, ID) { func (fs *fakeFileSystem) treeIsKnown(tree *Tree) (bool, []byte, ID) {
data, err := json.Marshal(tree) data, err := json.Marshal(tree)
if err != nil { if err != nil {
fs.t.Fatalf("json.Marshal(tree) returned error: %v", err) fs.t.Fatalf("json.Marshal(tree) returned error: %v", err)
@ -76,7 +85,7 @@ func (fs fakeFileSystem) treeIsKnown(tree *Tree) (bool, []byte, ID) {
return fs.blobIsKnown(id, TreeBlob), data, id return fs.blobIsKnown(id, TreeBlob), data, id
} }
func (fs fakeFileSystem) blobIsKnown(id ID, t BlobType) bool { func (fs *fakeFileSystem) blobIsKnown(id ID, t BlobType) bool {
if rand.Float32() < fs.duplication { if rand.Float32() < fs.duplication {
return false return false
} }
@ -94,7 +103,7 @@ func (fs fakeFileSystem) blobIsKnown(id ID, t BlobType) bool {
} }
// saveTree saves a tree of fake files in the repo and returns the ID. // saveTree saves a tree of fake files in the repo and returns the ID.
func (fs fakeFileSystem) saveTree(seed int64, depth int) ID { func (fs *fakeFileSystem) saveTree(seed int64, depth int) ID {
rnd := rand.NewSource(seed) rnd := rand.NewSource(seed)
numNodes := int(rnd.Int63() % maxNodes) numNodes := int(rnd.Int63() % maxNodes)

View file

@ -47,3 +47,14 @@ func TestCreateSnapshot(t *testing.T) {
checker.TestCheckRepo(t, repo) checker.TestCheckRepo(t, repo)
} }
func BenchmarkTestCreateSnapshot(t *testing.B) {
repo, cleanup := repository.TestRepository(t)
defer cleanup()
t.ResetTimer()
for i := 0; i < t.N; i++ {
restic.TestCreateSnapshot(t, repo, testSnapshotTime.Add(time.Duration(i)*time.Second), testDepth, 0)
}
}