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"
|
|
|
|
"math/rand"
|
|
|
|
"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"
|
2014-11-16 20:41:05 +00:00
|
|
|
)
|
|
|
|
|
2015-02-17 22:23:12 +00:00
|
|
|
var benchArchiveDirectory = flag.String("test.benchdir", "", "benchmark archiving a real directory")
|
2015-02-10 20:59:18 +00:00
|
|
|
|
2014-11-16 20:41:05 +00:00
|
|
|
func get_random(seed, count int) []byte {
|
|
|
|
buf := make([]byte, count)
|
|
|
|
|
2015-02-11 16:38:51 +00:00
|
|
|
rnd := rand.New(rand.NewSource(int64(seed)))
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
buf[i] = byte(rnd.Uint32())
|
2014-11-16 20:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
2015-02-11 16:38:51 +00:00
|
|
|
func randomReader(seed, size int) *bytes.Reader {
|
|
|
|
return bytes.NewReader(get_random(seed, size))
|
|
|
|
}
|
|
|
|
|
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-02-17 19:02:43 +00:00
|
|
|
func benchmarkChunkEncrypt(b testing.TB, buf []byte, rd Rdr, key *restic.Key) {
|
2015-02-09 22:39:16 +00:00
|
|
|
ch := restic.GetChunker("BenchmarkChunkEncrypt")
|
|
|
|
rd.Seek(0, 0)
|
|
|
|
ch.Reset(rd)
|
|
|
|
|
|
|
|
for {
|
|
|
|
chunk, err := ch.Next()
|
|
|
|
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
ok(b, err)
|
|
|
|
|
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-02-09 22:39:16 +00:00
|
|
|
ok(b, err)
|
|
|
|
assert(b, uint(n) == chunk.Length, "invalid length: got %d, expected %d", n, chunk.Length)
|
|
|
|
|
2015-02-17 19:02:43 +00:00
|
|
|
_, err = key.Encrypt(buf, buf)
|
2015-02-09 22:39:16 +00:00
|
|
|
ok(b, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
restic.FreeChunker("BenchmarkChunkEncrypt", ch)
|
|
|
|
}
|
|
|
|
|
2014-11-16 20:41:05 +00:00
|
|
|
func BenchmarkChunkEncrypt(b *testing.B) {
|
|
|
|
data := get_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
|
|
|
|
|
|
|
be := setupBackend(b)
|
|
|
|
defer teardownBackend(b, be)
|
|
|
|
key := setupKey(b, be, "geheim")
|
|
|
|
|
2015-02-17 19:02:43 +00:00
|
|
|
buf := restic.GetChunkBuf("BenchmarkChunkEncrypt")
|
|
|
|
|
2014-11-16 20:41:05 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
b.SetBytes(int64(len(data)))
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2015-02-17 19:02:43 +00:00
|
|
|
benchmarkChunkEncrypt(b, buf, rd, 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-02-09 22:39:16 +00:00
|
|
|
|
2015-02-17 19:02:43 +00:00
|
|
|
func benchmarkChunkEncryptP(b *testing.PB, buf []byte, rd Rdr, key *restic.Key) {
|
2015-02-09 22:39:16 +00:00
|
|
|
ch := restic.GetChunker("BenchmarkChunkEncryptP")
|
|
|
|
rd.Seek(0, 0)
|
|
|
|
ch.Reset(rd)
|
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)
|
|
|
|
key.Encrypt(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) {
|
|
|
|
be := setupBackend(b)
|
|
|
|
defer teardownBackend(b, be)
|
|
|
|
key := setupKey(b, be, "geheim")
|
|
|
|
|
|
|
|
data := get_random(23, 10<<20) // 10MiB
|
|
|
|
|
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-02-17 19:02:43 +00:00
|
|
|
benchmarkChunkEncryptP(pb, buf, rd, 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
|
|
|
|
|
|
|
func BenchmarkArchiveDirectory(b *testing.B) {
|
|
|
|
if *benchArchiveDirectory == "" {
|
|
|
|
b.Skip("benchdir not set, skipping BenchmarkArchiveDirectory")
|
|
|
|
}
|
|
|
|
|
|
|
|
be := setupBackend(b)
|
|
|
|
defer teardownBackend(b, be)
|
|
|
|
key := setupKey(b, be, "geheim")
|
|
|
|
server := restic.NewServerWithKey(be, key)
|
|
|
|
|
|
|
|
arch, err := restic.NewArchiver(server, nil)
|
|
|
|
ok(b, err)
|
|
|
|
|
2015-02-15 13:44:54 +00:00
|
|
|
_, id, err := arch.Snapshot(*benchArchiveDirectory, nil)
|
2015-02-10 20:59:18 +00:00
|
|
|
|
|
|
|
b.Logf("snapshot archived as %v", id)
|
|
|
|
}
|
2015-02-17 21:39:44 +00:00
|
|
|
|
|
|
|
func snapshot(t *testing.T, server restic.Server, path string) *restic.Snapshot {
|
|
|
|
arch, err := restic.NewArchiver(server, nil)
|
|
|
|
ok(t, err)
|
|
|
|
ok(t, arch.Preload())
|
|
|
|
sn, _, err := arch.Snapshot(path, nil)
|
|
|
|
ok(t, err)
|
|
|
|
return sn
|
|
|
|
}
|
|
|
|
|
|
|
|
func countBlobs(t *testing.T, server restic.Server) int {
|
|
|
|
blobs := 0
|
|
|
|
err := server.EachID(backend.Tree, func(id backend.ID) {
|
|
|
|
tree, err := restic.LoadTree(server, id)
|
|
|
|
ok(t, err)
|
|
|
|
|
|
|
|
blobs += tree.Map.Len()
|
|
|
|
})
|
|
|
|
ok(t, err)
|
|
|
|
|
|
|
|
return blobs
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestArchiverPreload(t *testing.T) {
|
2015-02-17 22:40:37 +00:00
|
|
|
if *benchArchiveDirectory == "" {
|
|
|
|
t.Skip("benchdir not set, skipping TestArchiverPreload")
|
|
|
|
}
|
|
|
|
|
2015-02-17 21:39:44 +00:00
|
|
|
be := setupBackend(t)
|
|
|
|
defer teardownBackend(t, be)
|
|
|
|
key := setupKey(t, be, "geheim")
|
|
|
|
server := restic.NewServerWithKey(be, key)
|
|
|
|
|
|
|
|
// archive a few files
|
|
|
|
sn := snapshot(t, server, *benchArchiveDirectory)
|
|
|
|
t.Logf("archived snapshot %v", sn.ID)
|
|
|
|
|
|
|
|
// get archive stats
|
|
|
|
blobsBefore := countBlobs(t, server)
|
|
|
|
t.Logf("found %v blobs", blobsBefore)
|
|
|
|
|
|
|
|
// archive the same files again
|
|
|
|
sn2 := snapshot(t, server, *benchArchiveDirectory)
|
|
|
|
t.Logf("archived snapshot %v", sn2.ID)
|
|
|
|
|
|
|
|
// get archive stats
|
|
|
|
blobsAfter := countBlobs(t, server)
|
|
|
|
t.Logf("found %v blobs", blobsAfter)
|
|
|
|
|
|
|
|
// if there are more than 10% more blobs, something is wrong
|
|
|
|
if blobsAfter > (blobsBefore + blobsBefore/10) {
|
|
|
|
t.Fatalf("TestArchiverPreload: too many blobs in repository: before %d, after %d, threshhold %d",
|
|
|
|
blobsBefore, blobsAfter, (blobsBefore + blobsBefore/10))
|
|
|
|
}
|
|
|
|
}
|