2014-12-05 20:45:49 +00:00
|
|
|
package restic_test
|
2014-11-16 20:41:05 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-02-10 20:59:18 +00:00
|
|
|
"flag"
|
2014-11-16 20:41:05 +00:00
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
|
2014-12-05 20:45:49 +00:00
|
|
|
"github.com/restic/restic"
|
2015-02-17 21:39:44 +00:00
|
|
|
"github.com/restic/restic/backend"
|
2014-12-05 20:45:49 +00:00
|
|
|
"github.com/restic/restic/chunker"
|
2015-05-03 16:04:13 +00:00
|
|
|
"github.com/restic/restic/crypto"
|
2015-04-26 15:44:38 +00:00
|
|
|
"github.com/restic/restic/pack"
|
2015-04-09 19:15:48 +00:00
|
|
|
. "github.com/restic/restic/test"
|
2014-11-16 20:41:05 +00:00
|
|
|
)
|
|
|
|
|
2015-03-10 14:58:23 +00:00
|
|
|
var benchArchiveDirectory = flag.String("test.benchdir", ".", "benchmark archiving a real directory (default: .)")
|
2015-04-05 22:22:19 +00:00
|
|
|
var testPol = chunker.Pol(0x3DA3358B4DC173)
|
2015-02-10 20:59:18 +00:00
|
|
|
|
2015-02-08 21:54:45 +00:00
|
|
|
const bufSize = chunker.MiB
|
|
|
|
|
2015-02-17 19:02:43 +00:00
|
|
|
type Rdr interface {
|
2015-02-09 22:39:16 +00:00
|
|
|
io.ReadSeeker
|
|
|
|
io.ReaderAt
|
2015-02-17 19:02:43 +00:00
|
|
|
}
|
2015-02-09 22:39:16 +00:00
|
|
|
|
2015-05-03 16:04:13 +00:00
|
|
|
func benchmarkChunkEncrypt(b testing.TB, buf, buf2 []byte, rd Rdr, key *crypto.Key) {
|
2015-02-09 22:39:16 +00:00
|
|
|
ch := restic.GetChunker("BenchmarkChunkEncrypt")
|
|
|
|
rd.Seek(0, 0)
|
2015-04-05 22:22:19 +00:00
|
|
|
ch.Reset(rd, testPol)
|
2015-02-09 22:39:16 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
chunk, err := ch.Next()
|
|
|
|
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(b, err)
|
2015-02-09 22:39:16 +00:00
|
|
|
|
2015-02-17 19:02:43 +00:00
|
|
|
// reduce length of buf
|
|
|
|
buf = buf[:chunk.Length]
|
|
|
|
n, err := io.ReadFull(chunk.Reader(rd), buf)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(b, err)
|
|
|
|
Assert(b, uint(n) == chunk.Length, "invalid length: got %d, expected %d", n, chunk.Length)
|
2015-02-09 22:39:16 +00:00
|
|
|
|
2015-05-03 16:04:13 +00:00
|
|
|
_, err = crypto.Encrypt(key, buf2, buf)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(b, err)
|
2015-02-09 22:39:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
restic.FreeChunker("BenchmarkChunkEncrypt", ch)
|
|
|
|
}
|
|
|
|
|
2014-11-16 20:41:05 +00:00
|
|
|
func BenchmarkChunkEncrypt(b *testing.B) {
|
2015-04-12 07:36:14 +00:00
|
|
|
data := Random(23, 10<<20) // 10MiB
|
2015-02-08 21:54:45 +00:00
|
|
|
rd := bytes.NewReader(data)
|
2014-11-16 20:41:05 +00:00
|
|
|
|
2015-05-03 14:36:52 +00:00
|
|
|
s := SetupBackend(b)
|
|
|
|
defer TeardownBackend(b, s)
|
2014-11-16 20:41:05 +00:00
|
|
|
|
2015-02-17 19:02:43 +00:00
|
|
|
buf := restic.GetChunkBuf("BenchmarkChunkEncrypt")
|
2015-04-24 22:54:15 +00:00
|
|
|
buf2 := restic.GetChunkBuf("BenchmarkChunkEncrypt")
|
2015-02-17 19:02:43 +00:00
|
|
|
|
2014-11-16 20:41:05 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
b.SetBytes(int64(len(data)))
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2015-05-03 14:36:52 +00:00
|
|
|
benchmarkChunkEncrypt(b, buf, buf2, rd, s.Key())
|
2015-02-09 22:39:16 +00:00
|
|
|
}
|
2014-11-16 20:41:05 +00:00
|
|
|
|
2015-02-17 19:02:43 +00:00
|
|
|
restic.FreeChunkBuf("BenchmarkChunkEncrypt", buf)
|
2015-04-24 22:54:15 +00:00
|
|
|
restic.FreeChunkBuf("BenchmarkChunkEncrypt", buf2)
|
2015-02-17 19:02:43 +00:00
|
|
|
}
|
2015-02-09 22:39:16 +00:00
|
|
|
|
2015-05-03 16:04:13 +00:00
|
|
|
func benchmarkChunkEncryptP(b *testing.PB, buf []byte, rd Rdr, key *crypto.Key) {
|
2015-02-09 22:39:16 +00:00
|
|
|
ch := restic.GetChunker("BenchmarkChunkEncryptP")
|
|
|
|
rd.Seek(0, 0)
|
2015-04-05 22:22:19 +00:00
|
|
|
ch.Reset(rd, testPol)
|
2014-11-16 20:41:05 +00:00
|
|
|
|
2015-02-09 22:39:16 +00:00
|
|
|
for {
|
|
|
|
chunk, err := ch.Next()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
2014-11-16 20:41:05 +00:00
|
|
|
|
2015-02-09 22:39:16 +00:00
|
|
|
// reduce length of chunkBuf
|
2015-02-17 19:02:43 +00:00
|
|
|
buf = buf[:chunk.Length]
|
|
|
|
io.ReadFull(chunk.Reader(rd), buf)
|
2015-05-03 16:04:13 +00:00
|
|
|
crypto.Encrypt(key, buf, buf)
|
2015-02-09 22:39:16 +00:00
|
|
|
}
|
2014-11-16 20:41:05 +00:00
|
|
|
|
2015-02-09 22:39:16 +00:00
|
|
|
restic.FreeChunker("BenchmarkChunkEncryptP", ch)
|
|
|
|
}
|
2015-02-08 21:54:45 +00:00
|
|
|
|
2015-02-09 22:39:16 +00:00
|
|
|
func BenchmarkChunkEncryptParallel(b *testing.B) {
|
2015-05-03 14:36:52 +00:00
|
|
|
s := SetupBackend(b)
|
|
|
|
defer TeardownBackend(b, s)
|
2015-02-09 22:39:16 +00:00
|
|
|
|
2015-04-12 07:36:14 +00:00
|
|
|
data := Random(23, 10<<20) // 10MiB
|
2015-02-09 22:39:16 +00:00
|
|
|
|
2015-02-17 19:02:43 +00:00
|
|
|
buf := restic.GetChunkBuf("BenchmarkChunkEncryptParallel")
|
|
|
|
|
2015-02-09 22:39:16 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
b.SetBytes(int64(len(data)))
|
|
|
|
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
|
|
|
rd := bytes.NewReader(data)
|
2015-05-03 14:36:52 +00:00
|
|
|
benchmarkChunkEncryptP(pb, buf, rd, s.Key())
|
2014-11-16 20:41:05 +00:00
|
|
|
}
|
2015-02-09 22:39:16 +00:00
|
|
|
})
|
2015-02-17 19:02:43 +00:00
|
|
|
|
|
|
|
restic.FreeChunkBuf("BenchmarkChunkEncryptParallel", buf)
|
2014-11-16 20:41:05 +00:00
|
|
|
}
|
2015-02-10 20:59:18 +00:00
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
func archiveDirectory(b testing.TB) {
|
2015-04-26 12:46:15 +00:00
|
|
|
server := SetupBackend(b)
|
|
|
|
defer TeardownBackend(b, server)
|
2015-02-10 20:59:18 +00:00
|
|
|
|
2015-04-30 01:41:51 +00:00
|
|
|
arch := restic.NewArchiver(server)
|
2015-02-10 20:59:18 +00:00
|
|
|
|
2015-03-02 13:48:47 +00:00
|
|
|
_, id, err := arch.Snapshot(nil, []string{*benchArchiveDirectory}, nil)
|
2015-04-30 01:41:51 +00:00
|
|
|
OK(b, err)
|
2015-02-10 20:59:18 +00:00
|
|
|
|
|
|
|
b.Logf("snapshot archived as %v", id)
|
|
|
|
}
|
2015-02-17 21:39:44 +00:00
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
func TestArchiveDirectory(t *testing.T) {
|
|
|
|
if *benchArchiveDirectory == "" {
|
|
|
|
t.Skip("benchdir not set, skipping TestArchiveDirectory")
|
|
|
|
}
|
|
|
|
|
|
|
|
archiveDirectory(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkArchiveDirectory(b *testing.B) {
|
|
|
|
if *benchArchiveDirectory == "" {
|
|
|
|
b.Skip("benchdir not set, skipping BenchmarkArchiveDirectory")
|
|
|
|
}
|
|
|
|
|
2015-05-01 20:58:50 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
archiveDirectory(b)
|
|
|
|
}
|
2015-02-17 21:39:44 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
func archiveWithDedup(t testing.TB) {
|
2015-02-17 22:40:37 +00:00
|
|
|
if *benchArchiveDirectory == "" {
|
2015-04-26 15:44:38 +00:00
|
|
|
t.Skip("benchdir not set, skipping TestArchiverDedup")
|
2015-02-17 22:40:37 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 12:46:15 +00:00
|
|
|
server := SetupBackend(t)
|
|
|
|
defer TeardownBackend(t, server)
|
2015-02-17 21:39:44 +00:00
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
var cnt struct {
|
|
|
|
before, after, after2 struct {
|
|
|
|
packs, dataBlobs, treeBlobs uint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-17 21:39:44 +00:00
|
|
|
// archive a few files
|
2015-04-26 12:46:15 +00:00
|
|
|
sn := SnapshotDir(t, server, *benchArchiveDirectory, nil)
|
2015-03-09 21:58:17 +00:00
|
|
|
t.Logf("archived snapshot %v", sn.ID().Str())
|
2015-02-17 21:39:44 +00:00
|
|
|
|
|
|
|
// get archive stats
|
2015-04-26 15:44:38 +00:00
|
|
|
cnt.before.packs = server.Count(backend.Data)
|
|
|
|
cnt.before.dataBlobs = server.Index().Count(pack.Data)
|
|
|
|
cnt.before.treeBlobs = server.Index().Count(pack.Tree)
|
|
|
|
t.Logf("packs %v, data blobs %v, tree blobs %v",
|
|
|
|
cnt.before.packs, cnt.before.dataBlobs, cnt.before.treeBlobs)
|
2015-02-17 21:39:44 +00:00
|
|
|
|
2015-03-09 21:58:17 +00:00
|
|
|
// archive the same files again, without parent snapshot
|
2015-04-26 12:46:15 +00:00
|
|
|
sn2 := SnapshotDir(t, server, *benchArchiveDirectory, nil)
|
2015-03-09 21:58:17 +00:00
|
|
|
t.Logf("archived snapshot %v", sn2.ID().Str())
|
2015-02-17 21:39:44 +00:00
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
// get archive stats again
|
|
|
|
cnt.after.packs = server.Count(backend.Data)
|
|
|
|
cnt.after.dataBlobs = server.Index().Count(pack.Data)
|
|
|
|
cnt.after.treeBlobs = server.Index().Count(pack.Tree)
|
|
|
|
t.Logf("packs %v, data blobs %v, tree blobs %v",
|
|
|
|
cnt.after.packs, cnt.after.dataBlobs, cnt.after.treeBlobs)
|
|
|
|
|
|
|
|
// if there are more packs or blobs, something is wrong
|
|
|
|
if cnt.after.packs > cnt.before.packs {
|
|
|
|
t.Fatalf("TestArchiverDedup: too many packs in repository: before %d, after %d",
|
|
|
|
cnt.before.packs, cnt.after.packs)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
if cnt.after.treeBlobs > cnt.before.treeBlobs {
|
|
|
|
t.Fatalf("TestArchiverDedup: too many tree blobs in repository: before %d, after %d",
|
|
|
|
cnt.before.treeBlobs, cnt.after.treeBlobs)
|
2015-03-09 21:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// archive the same files again, with a parent snapshot
|
2015-04-26 12:46:15 +00:00
|
|
|
sn3 := SnapshotDir(t, server, *benchArchiveDirectory, sn2.ID())
|
2015-03-09 21:58:17 +00:00
|
|
|
t.Logf("archived snapshot %v, parent %v", sn3.ID().Str(), sn2.ID().Str())
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
// get archive stats again
|
|
|
|
cnt.after2.packs = server.Count(backend.Data)
|
|
|
|
cnt.after2.dataBlobs = server.Index().Count(pack.Data)
|
|
|
|
cnt.after2.treeBlobs = server.Index().Count(pack.Tree)
|
|
|
|
t.Logf("packs %v, data blobs %v, tree blobs %v",
|
|
|
|
cnt.after2.packs, cnt.after2.dataBlobs, cnt.after2.treeBlobs)
|
|
|
|
|
|
|
|
// if there are more packs or blobs, something is wrong
|
|
|
|
if cnt.after2.packs > cnt.before.packs {
|
|
|
|
t.Fatalf("TestArchiverDedup: too many packs in repository: before %d, after %d",
|
|
|
|
cnt.before.packs, cnt.after2.packs)
|
2015-02-18 21:46:09 +00:00
|
|
|
}
|
2015-04-26 15:44:38 +00:00
|
|
|
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-18 21:46:09 +00:00
|
|
|
}
|
2015-04-26 15:44:38 +00:00
|
|
|
if cnt.after2.treeBlobs > cnt.before.treeBlobs {
|
|
|
|
t.Fatalf("TestArchiverDedup: too many tree blobs in repository: before %d, after %d",
|
|
|
|
cnt.before.treeBlobs, cnt.after2.treeBlobs)
|
2015-02-17 21:39:44 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-21 23:09:57 +00:00
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
func TestArchiveDedup(t *testing.T) {
|
|
|
|
archiveWithDedup(t)
|
|
|
|
}
|
|
|
|
|
2015-02-21 23:09:57 +00:00
|
|
|
func BenchmarkLoadTree(t *testing.B) {
|
|
|
|
if *benchArchiveDirectory == "" {
|
2015-04-26 15:44:38 +00:00
|
|
|
t.Skip("benchdir not set, skipping TestArchiverDedup")
|
2015-02-21 23:09:57 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 12:46:15 +00:00
|
|
|
s := SetupBackend(t)
|
|
|
|
defer TeardownBackend(t, s)
|
2015-02-21 23:09:57 +00:00
|
|
|
|
|
|
|
// archive a few files
|
2015-04-30 01:41:51 +00:00
|
|
|
arch := restic.NewArchiver(s)
|
2015-03-02 13:48:47 +00:00
|
|
|
sn, _, err := arch.Snapshot(nil, []string{*benchArchiveDirectory}, nil)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-02-21 23:09:57 +00:00
|
|
|
t.Logf("archived snapshot %v", sn.ID())
|
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
list := make([]backend.ID, 0, 10)
|
|
|
|
done := make(chan struct{})
|
|
|
|
|
2015-04-29 20:30:00 +00:00
|
|
|
for blob := range s.Index().Each(done) {
|
|
|
|
if blob.Type != pack.Tree {
|
2015-03-28 10:50:23 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-04-29 20:30:00 +00:00
|
|
|
list = append(list, blob.ID)
|
2015-03-28 10:50:23 +00:00
|
|
|
if len(list) == cap(list) {
|
|
|
|
close(done)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-21 23:09:57 +00:00
|
|
|
// start benchmark
|
|
|
|
t.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < t.N; i++ {
|
2015-03-28 14:07:08 +00:00
|
|
|
for _, id := range list {
|
2015-04-26 15:44:38 +00:00
|
|
|
_, err := restic.LoadTree(s, id)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-03-28 10:50:23 +00:00
|
|
|
}
|
2015-02-21 23:09:57 +00:00
|
|
|
}
|
|
|
|
}
|