restic/src/restic/archiver/archiver_test.go

330 lines
7.4 KiB
Go
Raw Normal View History

2016-08-31 22:39:36 +02:00
package archiver_test
2014-11-16 21:41:05 +01:00
import (
"bytes"
"io"
"testing"
"time"
2014-11-16 21:41:05 +01:00
"restic"
"restic/backend"
"restic/checker"
"restic/crypto"
"restic/pack"
. "restic/test"
"github.com/pkg/errors"
"github.com/restic/chunker"
2014-11-16 21:41:05 +01:00
)
var testPol = chunker.Pol(0x3DA3358B4DC173)
2015-02-17 20:02:43 +01:00
type Rdr interface {
io.ReadSeeker
io.ReaderAt
2015-02-17 20:02:43 +01:00
}
func benchmarkChunkEncrypt(b testing.TB, buf, buf2 []byte, rd Rdr, key *crypto.Key) {
rd.Seek(0, 0)
ch := chunker.New(rd, testPol)
for {
chunk, err := ch.Next(buf)
if errors.Cause(err) == io.EOF {
break
}
2015-04-09 21:15:48 +02:00
OK(b, err)
2015-02-17 20:02:43 +01:00
// reduce length of buf
Assert(b, uint(len(chunk.Data)) == chunk.Length,
"invalid length: got %d, expected %d", len(chunk.Data), chunk.Length)
_, err = crypto.Encrypt(key, buf2, chunk.Data)
2015-04-09 21:15:48 +02:00
OK(b, err)
}
}
2014-11-16 21:41:05 +01:00
func BenchmarkChunkEncrypt(b *testing.B) {
repo := SetupRepo()
defer TeardownRepo(repo)
data := Random(23, 10<<20) // 10MiB
rd := bytes.NewReader(data)
2014-11-16 21:41:05 +01:00
2015-05-05 00:14:07 +02:00
buf := make([]byte, chunker.MaxSize)
buf2 := make([]byte, chunker.MaxSize)
2015-02-17 20:02:43 +01:00
2014-11-16 21:41:05 +01:00
b.ResetTimer()
b.SetBytes(int64(len(data)))
for i := 0; i < b.N; i++ {
benchmarkChunkEncrypt(b, buf, buf2, rd, repo.Key())
}
2015-02-17 20:02:43 +01:00
}
func benchmarkChunkEncryptP(b *testing.PB, buf []byte, rd Rdr, key *crypto.Key) {
ch := chunker.New(rd, testPol)
2014-11-16 21:41:05 +01:00
for {
chunk, err := ch.Next(buf)
if errors.Cause(err) == io.EOF {
break
}
2014-11-16 21:41:05 +01:00
// reduce length of chunkBuf
crypto.Encrypt(key, chunk.Data, chunk.Data)
}
}
func BenchmarkChunkEncryptParallel(b *testing.B) {
repo := SetupRepo()
defer TeardownRepo(repo)
data := Random(23, 10<<20) // 10MiB
2015-05-05 00:14:07 +02:00
buf := make([]byte, chunker.MaxSize)
2015-02-17 20:02:43 +01:00
b.ResetTimer()
b.SetBytes(int64(len(data)))
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
rd := bytes.NewReader(data)
benchmarkChunkEncryptP(pb, buf, rd, repo.Key())
2014-11-16 21:41:05 +01:00
}
})
2014-11-16 21:41:05 +01:00
}
func archiveDirectory(b testing.TB) {
repo := SetupRepo()
defer TeardownRepo(repo)
2015-05-09 13:32:52 +02:00
arch := restic.NewArchiver(repo)
2015-06-28 13:15:35 +02:00
_, id, err := arch.Snapshot(nil, []string{BenchArchiveDirectory}, nil)
2015-04-30 03:41:51 +02:00
OK(b, err)
b.Logf("snapshot archived as %v", id)
}
2015-02-17 22:39:44 +01:00
func TestArchiveDirectory(t *testing.T) {
2015-06-28 13:15:35 +02:00
if BenchArchiveDirectory == "" {
t.Skip("benchdir not set, skipping TestArchiveDirectory")
}
archiveDirectory(t)
}
func BenchmarkArchiveDirectory(b *testing.B) {
2015-06-28 13:15:35 +02:00
if BenchArchiveDirectory == "" {
b.Skip("benchdir not set, skipping BenchmarkArchiveDirectory")
}
2015-05-01 22:58:50 +02:00
for i := 0; i < b.N; i++ {
archiveDirectory(b)
}
2015-02-17 22:39:44 +01:00
}
func archiveWithDedup(t testing.TB) {
repo := SetupRepo()
defer TeardownRepo(repo)
2015-06-28 13:15:35 +02:00
if BenchArchiveDirectory == "" {
t.Skip("benchdir not set, skipping TestArchiverDedup")
2015-02-17 23:40:37 +01:00
}
var cnt struct {
before, after, after2 struct {
packs, dataBlobs, treeBlobs uint
}
}
2015-02-17 22:39:44 +01:00
// archive a few files
2015-06-28 13:15:35 +02:00
sn := SnapshotDir(t, repo, BenchArchiveDirectory, nil)
2015-03-09 22:58:17 +01:00
t.Logf("archived snapshot %v", sn.ID().Str())
2015-02-17 22:39:44 +01:00
// get archive stats
2016-08-31 22:39:36 +02:00
cnt.before.packs = repo.Count(restic.DataFile)
cnt.before.dataBlobs = repo.Index().Count(restic.DataBlob)
cnt.before.treeBlobs = repo.Index().Count(restic.TreeBlob)
t.Logf("packs %v, data blobs %v, tree blobs %v",
cnt.before.packs, cnt.before.dataBlobs, cnt.before.treeBlobs)
2015-02-17 22:39:44 +01:00
2015-03-09 22:58:17 +01:00
// archive the same files again, without parent snapshot
2015-06-28 13:15:35 +02:00
sn2 := SnapshotDir(t, repo, BenchArchiveDirectory, nil)
2015-03-09 22:58:17 +01:00
t.Logf("archived snapshot %v", sn2.ID().Str())
2015-02-17 22:39:44 +01:00
// get archive stats again
2016-08-31 22:39:36 +02:00
cnt.after.packs = repo.Count(restic.DataFile)
cnt.after.dataBlobs = repo.Index().Count(restic.DataBlob)
cnt.after.treeBlobs = repo.Index().Count(restic.TreeBlob)
t.Logf("packs %v, data blobs %v, tree blobs %v",
cnt.after.packs, cnt.after.dataBlobs, cnt.after.treeBlobs)
// if there are more data blobs, something is wrong
if cnt.after.dataBlobs > cnt.before.dataBlobs {
t.Fatalf("TestArchiverDedup: too many data blobs in repository: before %d, after %d",
cnt.before.dataBlobs, cnt.after.dataBlobs)
}
2015-03-09 22:58:17 +01:00
// archive the same files again, with a parent snapshot
2015-06-28 13:15:35 +02:00
sn3 := SnapshotDir(t, repo, BenchArchiveDirectory, sn2.ID())
2015-03-09 22:58:17 +01:00
t.Logf("archived snapshot %v, parent %v", sn3.ID().Str(), sn2.ID().Str())
// get archive stats again
2016-08-31 22:39:36 +02:00
cnt.after2.packs = repo.Count(restic.DataFile)
cnt.after2.dataBlobs = repo.Index().Count(restic.DataBlob)
cnt.after2.treeBlobs = repo.Index().Count(restic.TreeBlob)
t.Logf("packs %v, data blobs %v, tree blobs %v",
cnt.after2.packs, cnt.after2.dataBlobs, cnt.after2.treeBlobs)
// if there are more data blobs, something is wrong
if cnt.after2.dataBlobs > cnt.before.dataBlobs {
t.Fatalf("TestArchiverDedup: too many data blobs in repository: before %d, after %d",
cnt.before.dataBlobs, cnt.after2.dataBlobs)
}
2015-02-17 22:39:44 +01:00
}
2015-02-22 00:09:57 +01:00
func TestArchiveDedup(t *testing.T) {
archiveWithDedup(t)
}
2015-02-22 00:09:57 +01:00
func BenchmarkLoadTree(t *testing.B) {
repo := SetupRepo()
defer TeardownRepo(repo)
2015-06-28 13:15:35 +02:00
if BenchArchiveDirectory == "" {
t.Skip("benchdir not set, skipping TestArchiverDedup")
2015-02-22 00:09:57 +01:00
}
// archive a few files
arch := restic.NewArchiver(repo)
2015-06-28 13:15:35 +02:00
sn, _, err := arch.Snapshot(nil, []string{BenchArchiveDirectory}, nil)
2015-04-09 21:15:48 +02:00
OK(t, err)
2015-02-22 00:09:57 +01:00
t.Logf("archived snapshot %v", sn.ID())
2015-03-28 11:50:23 +01:00
list := make([]backend.ID, 0, 10)
done := make(chan struct{})
2015-10-12 22:34:12 +02:00
for _, idx := range repo.Index().All() {
for blob := range idx.Each(done) {
2016-08-31 22:39:36 +02:00
if blob.Type != restic.TreeBlob {
2015-10-12 22:34:12 +02:00
continue
}
list = append(list, blob.ID)
if len(list) == cap(list) {
close(done)
break
}
2015-03-28 11:50:23 +01:00
}
}
2015-02-22 00:09:57 +01:00
// start benchmark
t.ResetTimer()
for i := 0; i < t.N; i++ {
for _, id := range list {
_, err := restic.LoadTree(repo, id)
2015-04-09 21:15:48 +02:00
OK(t, err)
2015-03-28 11:50:23 +01:00
}
2015-02-22 00:09:57 +01:00
}
}
// Saves several identical chunks concurrently and later checks that there are no
// unreferenced packs in the repository. See also #292 and #358.
func TestParallelSaveWithDuplication(t *testing.T) {
for seed := 0; seed < 10; seed++ {
testParallelSaveWithDuplication(t, seed)
}
}
func testParallelSaveWithDuplication(t *testing.T, seed int) {
repo := SetupRepo()
defer TeardownRepo(repo)
dataSizeMb := 128
duplication := 7
arch := restic.NewArchiver(repo)
chunks := getRandomData(seed, dataSizeMb*1024*1024)
errChannels := [](<-chan error){}
// interweaved processing of subsequent chunks
maxParallel := 2*duplication - 1
barrier := make(chan struct{}, maxParallel)
for _, c := range chunks {
for dupIdx := 0; dupIdx < duplication; dupIdx++ {
errChan := make(chan error)
errChannels = append(errChannels, errChan)
go func(c chunker.Chunk, errChan chan<- error) {
barrier <- struct{}{}
id := backend.Hash(c.Data)
time.Sleep(time.Duration(id[0]))
2016-08-31 22:39:36 +02:00
err := arch.Save(restic.DataBlob, c.Data, id)
<-barrier
errChan <- err
}(c, errChan)
}
}
for _, errChan := range errChannels {
OK(t, <-errChan)
}
OK(t, repo.Flush())
OK(t, repo.SaveIndex())
chkr := createAndInitChecker(t, repo)
assertNoUnreferencedPacks(t, chkr)
}
func getRandomData(seed int, size int) []chunker.Chunk {
buf := Random(seed, size)
var chunks []chunker.Chunk
chunker := chunker.New(bytes.NewReader(buf), testPol)
for {
c, err := chunker.Next(nil)
if errors.Cause(err) == io.EOF {
break
}
chunks = append(chunks, c)
}
return chunks
}
2016-08-31 20:29:54 +02:00
func createAndInitChecker(t *testing.T, repo Repository) *checker.Checker {
chkr := checker.New(repo)
hints, errs := chkr.LoadIndex()
if len(errs) > 0 {
t.Fatalf("expected no errors, got %v: %v", len(errs), errs)
}
if len(hints) > 0 {
t.Errorf("expected no hints, got %v: %v", len(hints), hints)
}
return chkr
}
func assertNoUnreferencedPacks(t *testing.T, chkr *checker.Checker) {
done := make(chan struct{})
defer close(done)
errChan := make(chan error)
go chkr.Packs(errChan, done)
for err := range errChan {
OK(t, err)
}
}