2016-03-05 14:58:39 +00:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
2017-06-05 21:56:59 +00:00
|
|
|
"context"
|
2016-03-05 14:58:39 +00:00
|
|
|
"io"
|
|
|
|
"math/rand"
|
2016-03-06 11:26:25 +00:00
|
|
|
"os"
|
2016-03-05 14:58:39 +00:00
|
|
|
"testing"
|
2017-07-23 12:21:03 +00:00
|
|
|
|
|
|
|
"github.com/restic/restic/internal"
|
|
|
|
"github.com/restic/restic/internal/backend/mem"
|
|
|
|
"github.com/restic/restic/internal/crypto"
|
|
|
|
"github.com/restic/restic/internal/fs"
|
|
|
|
"github.com/restic/restic/internal/mock"
|
2016-03-05 14:58:39 +00:00
|
|
|
)
|
|
|
|
|
2016-03-06 12:59:06 +00:00
|
|
|
type randReader struct {
|
|
|
|
src rand.Source
|
|
|
|
rand *rand.Rand
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRandReader(src rand.Source) *randReader {
|
|
|
|
return &randReader{
|
|
|
|
src: src,
|
|
|
|
rand: rand.New(src),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read generates len(p) random bytes and writes them into p. It
|
|
|
|
// always returns len(p) and a nil error.
|
|
|
|
func (r *randReader) Read(p []byte) (n int, err error) {
|
|
|
|
for i := 0; i < len(p); i += 7 {
|
|
|
|
val := r.src.Int63()
|
|
|
|
for j := 0; i+j < len(p) && j < 7; j++ {
|
|
|
|
p[i+j] = byte(val)
|
|
|
|
val >>= 8
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
2016-08-31 18:29:54 +00:00
|
|
|
func randomID(rd io.Reader) restic.ID {
|
|
|
|
id := restic.ID{}
|
2016-03-05 14:58:39 +00:00
|
|
|
_, err := io.ReadFull(rd, id[:])
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
2016-03-06 11:26:25 +00:00
|
|
|
const maxBlobSize = 1 << 20
|
|
|
|
|
2017-01-25 16:51:34 +00:00
|
|
|
func saveFile(t testing.TB, be Saver, f *os.File, id restic.ID) {
|
2017-01-22 16:53:00 +00:00
|
|
|
h := restic.Handle{Type: restic.DataFile, Name: id.String()}
|
|
|
|
t.Logf("save file %v", h)
|
2016-03-06 11:26:25 +00:00
|
|
|
|
2017-06-05 21:56:59 +00:00
|
|
|
if err := be.Save(context.TODO(), h, f); err != nil {
|
2017-01-23 17:49:12 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-01-25 16:51:34 +00:00
|
|
|
if err := f.Close(); err != nil {
|
2016-03-06 11:26:25 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-05-10 17:48:22 +00:00
|
|
|
if err := fs.RemoveIfExists(f.Name()); err != nil {
|
2016-03-06 11:26:25 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-06 12:59:06 +00:00
|
|
|
func fillPacks(t testing.TB, rnd *randReader, be Saver, pm *packerManager, buf []byte) (bytes int) {
|
2016-03-05 14:58:39 +00:00
|
|
|
for i := 0; i < 100; i++ {
|
2016-03-06 12:59:06 +00:00
|
|
|
l := rnd.rand.Intn(1 << 20)
|
|
|
|
seed := rnd.rand.Int63()
|
2016-03-05 14:58:39 +00:00
|
|
|
|
|
|
|
packer, err := pm.findPacker(uint(l))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-03-06 13:21:02 +00:00
|
|
|
rd := newRandReader(rand.NewSource(seed))
|
2016-03-05 14:58:39 +00:00
|
|
|
id := randomID(rd)
|
2016-03-06 11:26:25 +00:00
|
|
|
buf = buf[:l]
|
|
|
|
_, err = io.ReadFull(rd, buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-03-05 14:58:39 +00:00
|
|
|
|
2016-08-31 21:07:50 +00:00
|
|
|
n, err := packer.Add(restic.DataBlob, id, buf)
|
2016-03-06 11:26:25 +00:00
|
|
|
if n != l {
|
2016-03-05 14:58:39 +00:00
|
|
|
t.Errorf("Add() returned invalid number of bytes: want %v, got %v", n, l)
|
|
|
|
}
|
|
|
|
bytes += l
|
|
|
|
|
|
|
|
if packer.Size() < minPackSize && pm.countPacker() < maxPackers {
|
|
|
|
pm.insertPacker(packer)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-01-22 16:53:00 +00:00
|
|
|
_, err = packer.Finalize()
|
2016-03-05 14:58:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-01-25 16:51:34 +00:00
|
|
|
if _, err = packer.tmpfile.Seek(0, 0); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-01-22 16:53:00 +00:00
|
|
|
packID := restic.IDFromHash(packer.hw.Sum(nil))
|
2017-01-25 16:51:34 +00:00
|
|
|
saveFile(t, be, packer.tmpfile, packID)
|
2016-03-05 14:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bytes
|
|
|
|
}
|
|
|
|
|
2016-03-06 12:59:06 +00:00
|
|
|
func flushRemainingPacks(t testing.TB, rnd *randReader, be Saver, pm *packerManager) (bytes int) {
|
2016-03-05 14:58:39 +00:00
|
|
|
if pm.countPacker() > 0 {
|
2017-01-22 16:53:00 +00:00
|
|
|
for _, packer := range pm.packers {
|
2016-03-06 11:26:25 +00:00
|
|
|
n, err := packer.Finalize()
|
2016-03-05 14:58:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-03-06 11:26:25 +00:00
|
|
|
bytes += int(n)
|
2016-03-05 14:58:39 +00:00
|
|
|
|
2017-01-22 16:53:00 +00:00
|
|
|
packID := restic.IDFromHash(packer.hw.Sum(nil))
|
2017-01-25 16:51:34 +00:00
|
|
|
saveFile(t, be, packer.tmpfile, packID)
|
2016-03-05 14:58:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bytes
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPackerManager(t *testing.T) {
|
2016-03-06 12:59:06 +00:00
|
|
|
rnd := newRandReader(rand.NewSource(23))
|
2016-03-05 14:58:39 +00:00
|
|
|
|
|
|
|
be := mem.New()
|
2016-03-06 13:20:48 +00:00
|
|
|
pm := newPackerManager(be, crypto.NewRandomKey())
|
2016-03-05 14:58:39 +00:00
|
|
|
|
2016-03-06 11:26:25 +00:00
|
|
|
blobBuf := make([]byte, maxBlobSize)
|
|
|
|
|
|
|
|
bytes := fillPacks(t, rnd, be, pm, blobBuf)
|
2016-03-05 14:58:39 +00:00
|
|
|
bytes += flushRemainingPacks(t, rnd, be, pm)
|
|
|
|
|
|
|
|
t.Logf("saved %d bytes", bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkPackerManager(t *testing.B) {
|
2016-03-06 12:59:06 +00:00
|
|
|
rnd := newRandReader(rand.NewSource(23))
|
2016-03-05 14:58:39 +00:00
|
|
|
|
2017-01-22 11:32:20 +00:00
|
|
|
be := &mock.Backend{
|
2017-06-05 21:56:59 +00:00
|
|
|
SaveFn: func(context.Context, restic.Handle, io.Reader) error { return nil },
|
2017-01-22 11:32:20 +00:00
|
|
|
}
|
2016-03-06 11:26:25 +00:00
|
|
|
blobBuf := make([]byte, maxBlobSize)
|
2016-03-05 14:58:39 +00:00
|
|
|
|
|
|
|
t.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < t.N; i++ {
|
2017-01-22 16:53:00 +00:00
|
|
|
bytes := 0
|
|
|
|
pm := newPackerManager(be, crypto.NewRandomKey())
|
2016-03-06 11:26:25 +00:00
|
|
|
bytes += fillPacks(t, rnd, be, pm, blobBuf)
|
2017-01-22 16:53:00 +00:00
|
|
|
bytes += flushRemainingPacks(t, rnd, be, pm)
|
|
|
|
t.Logf("saved %d bytes", bytes)
|
2016-03-05 14:58:39 +00:00
|
|
|
}
|
|
|
|
}
|