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"
|
|
|
|
"github.com/restic/restic/chunker"
|
2014-11-16 20:41:05 +00:00
|
|
|
)
|
|
|
|
|
2015-02-10 20:59:18 +00:00
|
|
|
var benchArchiveDirectory = flag.String("test.benchdir", "", "benchmark archiving a real directory")
|
|
|
|
|
2014-11-16 20:41:05 +00:00
|
|
|
func get_random(seed, count int) []byte {
|
|
|
|
buf := make([]byte, count)
|
|
|
|
|
|
|
|
rnd := rand.New(rand.NewSource(23))
|
|
|
|
for i := 0; i < count; i += 4 {
|
|
|
|
r := rnd.Uint32()
|
|
|
|
buf[i] = byte(r)
|
|
|
|
buf[i+1] = byte(r >> 8)
|
|
|
|
buf[i+2] = byte(r >> 16)
|
|
|
|
buf[i+3] = byte(r >> 24)
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
2015-02-08 21:54:45 +00:00
|
|
|
const bufSize = chunker.MiB
|
|
|
|
|
2015-02-09 22:39:16 +00:00
|
|
|
func benchmarkChunkEncrypt(b testing.TB, rd interface {
|
|
|
|
io.ReadSeeker
|
|
|
|
io.ReaderAt
|
|
|
|
}, key *restic.Key) {
|
|
|
|
chunkBuf := make([]byte, restic.CiphertextExtension+chunker.MaxSize)
|
|
|
|
|
|
|
|
ch := restic.GetChunker("BenchmarkChunkEncrypt")
|
|
|
|
rd.Seek(0, 0)
|
|
|
|
ch.Reset(rd)
|
|
|
|
|
|
|
|
for {
|
|
|
|
chunk, err := ch.Next()
|
|
|
|
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
ok(b, err)
|
|
|
|
|
|
|
|
// reduce length of chunkBuf
|
|
|
|
chunkBuf = chunkBuf[:chunk.Length]
|
|
|
|
n, err := io.ReadFull(chunk.Reader(rd), chunkBuf)
|
|
|
|
ok(b, err)
|
|
|
|
assert(b, uint(n) == chunk.Length, "invalid length: got %d, expected %d", n, chunk.Length)
|
|
|
|
|
|
|
|
_, err = key.Encrypt(chunkBuf, chunkBuf)
|
|
|
|
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")
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.SetBytes(int64(len(data)))
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2015-02-09 22:39:16 +00:00
|
|
|
benchmarkChunkEncrypt(b, rd, key)
|
|
|
|
}
|
|
|
|
}
|
2014-11-16 20:41:05 +00:00
|
|
|
|
2015-02-09 22:39:16 +00:00
|
|
|
func benchmarkChunkEncryptP(b *testing.PB, rd interface {
|
|
|
|
io.ReadSeeker
|
|
|
|
io.ReaderAt
|
|
|
|
}, key *restic.Key) {
|
|
|
|
chunkBuf := make([]byte, restic.CiphertextExtension+chunker.MaxSize)
|
|
|
|
|
|
|
|
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
|
|
|
|
chunkBuf = chunkBuf[:chunk.Length]
|
|
|
|
io.ReadFull(chunk.Reader(rd), chunkBuf)
|
|
|
|
key.Encrypt(chunkBuf, chunkBuf)
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.SetBytes(int64(len(data)))
|
|
|
|
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
|
|
|
rd := bytes.NewReader(data)
|
|
|
|
benchmarkChunkEncryptP(pb, rd, key)
|
2014-11-16 20:41:05 +00:00
|
|
|
}
|
2015-02-09 22:39:16 +00:00
|
|
|
})
|
2014-11-16 20:41:05 +00:00
|
|
|
}
|
2015-02-10 20:59:18 +00:00
|
|
|
|
|
|
|
func BenchmarkScanner(b *testing.B) {
|
|
|
|
if *benchArchiveDirectory == "" {
|
|
|
|
b.Skip("benchdir not set, skipping BenchmarkScanner")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := restic.NewScanner(nil).Scan(*benchArchiveDirectory)
|
|
|
|
ok(b, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
tree, err := restic.NewScanner(nil).Scan(*benchArchiveDirectory)
|
|
|
|
ok(b, err)
|
|
|
|
|
|
|
|
arch, err := restic.NewArchiver(server, nil)
|
|
|
|
ok(b, err)
|
|
|
|
|
|
|
|
_, id, err := arch.Snapshot(*benchArchiveDirectory, tree, nil)
|
|
|
|
|
|
|
|
b.Logf("snapshot archived as %v", id)
|
|
|
|
}
|