mirror of
https://github.com/restic/restic.git
synced 2024-12-26 17:57:50 +00:00
be5a0ff59f
Benchmark results for internal/repository: name old time/op new time/op delta LoadTree-8 479µs ± 2% 478µs ± 1% ~ (p=0.780 n=10+9) LoadBlob-8 11.6ms ± 2% 11.6ms ± 1% ~ (p=0.631 n=10+10) LoadAndDecrypt-8 13.2ms ± 2% 13.3ms ± 3% ~ (p=0.631 n=10+10) name old alloc/op new alloc/op delta LoadTree-8 41.2kB ± 0% 41.2kB ± 0% ~ (all equal) LoadBlob-8 2.28kB ± 0% 2.28kB ± 0% ~ (all equal) LoadAndDecrypt-8 2.10MB ± 0% 2.10MB ± 0% ~ (all equal) name old allocs/op new allocs/op delta LoadTree-8 652 ± 0% 652 ± 0% ~ (all equal) LoadBlob-8 24.0 ± 0% 24.0 ± 0% ~ (all equal) LoadAndDecrypt-8 30.0 ± 0% 30.0 ± 0% ~ (all equal) name old speed new speed delta LoadBlob-8 86.2MB/s ± 2% 86.4MB/s ± 1% ~ (p=0.594 n=10+10) LoadAndDecrypt-8 75.7MB/s ± 2% 75.4MB/s ± 3% ~ (p=0.617 n=10+10)
195 lines
4.6 KiB
Go
195 lines
4.6 KiB
Go
// +build !netbsd
|
|
// +build !openbsd
|
|
// +build !solaris
|
|
// +build !windows
|
|
|
|
package fuse
|
|
|
|
import (
|
|
"bytes"
|
|
"math/rand"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
"github.com/restic/restic/internal/repository"
|
|
"github.com/restic/restic/internal/restic"
|
|
|
|
"bazil.org/fuse"
|
|
"bazil.org/fuse/fs"
|
|
|
|
rtest "github.com/restic/restic/internal/test"
|
|
)
|
|
|
|
func testRead(t testing.TB, f *file, offset, length int, data []byte) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
req := &fuse.ReadRequest{
|
|
Offset: int64(offset),
|
|
Size: length,
|
|
}
|
|
resp := &fuse.ReadResponse{
|
|
Data: data,
|
|
}
|
|
rtest.OK(t, f.Read(ctx, req, resp))
|
|
}
|
|
|
|
func firstSnapshotID(t testing.TB, repo restic.Repository) (first restic.ID) {
|
|
err := repo.List(context.TODO(), restic.SnapshotFile, func(id restic.ID, size int64) error {
|
|
if first.IsNull() {
|
|
first = id
|
|
}
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
return first
|
|
}
|
|
|
|
func loadFirstSnapshot(t testing.TB, repo restic.Repository) *restic.Snapshot {
|
|
id := firstSnapshotID(t, repo)
|
|
sn, err := restic.LoadSnapshot(context.TODO(), repo, id)
|
|
rtest.OK(t, err)
|
|
return sn
|
|
}
|
|
|
|
func loadTree(t testing.TB, repo restic.Repository, id restic.ID) *restic.Tree {
|
|
tree, err := repo.LoadTree(context.TODO(), id)
|
|
rtest.OK(t, err)
|
|
return tree
|
|
}
|
|
|
|
func TestFuseFile(t *testing.T) {
|
|
repo, cleanup := repository.TestRepository(t)
|
|
defer cleanup()
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
timestamp, err := time.Parse(time.RFC3339, "2017-01-24T10:42:56+01:00")
|
|
rtest.OK(t, err)
|
|
restic.TestCreateSnapshot(t, repo, timestamp, 2, 0.1)
|
|
|
|
sn := loadFirstSnapshot(t, repo)
|
|
tree := loadTree(t, repo, *sn.Tree)
|
|
|
|
var content restic.IDs
|
|
for _, node := range tree.Nodes {
|
|
content = append(content, node.Content...)
|
|
}
|
|
t.Logf("tree loaded, content: %v", content)
|
|
|
|
var (
|
|
filesize uint64
|
|
memfile []byte
|
|
)
|
|
for _, id := range content {
|
|
size, found := repo.LookupBlobSize(id, restic.DataBlob)
|
|
rtest.Assert(t, found, "Expected to find blob id %v", id)
|
|
filesize += uint64(size)
|
|
|
|
buf, err := repo.LoadBlob(context.TODO(), restic.DataBlob, id, nil)
|
|
rtest.OK(t, err)
|
|
|
|
if len(buf) != int(size) {
|
|
t.Fatalf("not enough bytes read for id %v: want %v, got %v", id.Str(), size, len(buf))
|
|
}
|
|
|
|
if uint(len(buf)) != size {
|
|
t.Fatalf("buffer has wrong length for id %v: want %v, got %v", id.Str(), size, len(buf))
|
|
}
|
|
|
|
memfile = append(memfile, buf...)
|
|
}
|
|
|
|
t.Logf("filesize is %v, memfile has size %v", filesize, len(memfile))
|
|
|
|
node := &restic.Node{
|
|
Name: "foo",
|
|
Inode: 23,
|
|
Mode: 0742,
|
|
Size: filesize,
|
|
Content: content,
|
|
}
|
|
root := &Root{
|
|
blobSizeCache: NewBlobSizeCache(context.TODO(), repo.Index()),
|
|
repo: repo,
|
|
}
|
|
|
|
t.Logf("blob cache has %d entries", len(root.blobSizeCache.m))
|
|
|
|
inode := fs.GenerateDynamicInode(1, "foo")
|
|
f, err := newFile(context.TODO(), root, inode, node)
|
|
rtest.OK(t, err)
|
|
|
|
attr := fuse.Attr{}
|
|
rtest.OK(t, f.Attr(ctx, &attr))
|
|
|
|
rtest.Equals(t, inode, attr.Inode)
|
|
rtest.Equals(t, node.Mode, attr.Mode)
|
|
rtest.Equals(t, node.Size, attr.Size)
|
|
rtest.Equals(t, (node.Size/uint64(attr.BlockSize))+1, attr.Blocks)
|
|
|
|
for i := 0; i < 200; i++ {
|
|
offset := rand.Intn(int(filesize))
|
|
length := rand.Intn(int(filesize)-offset) + 100
|
|
|
|
b := memfile[offset : offset+length]
|
|
|
|
buf := make([]byte, length)
|
|
|
|
testRead(t, f, offset, length, buf)
|
|
if !bytes.Equal(b, buf) {
|
|
t.Errorf("test %d failed, wrong data returned (offset %v, length %v)", i, offset, length)
|
|
}
|
|
}
|
|
|
|
rtest.OK(t, f.Release(ctx, nil))
|
|
}
|
|
|
|
// Test top-level directories for their UID and GID.
|
|
func TestTopUidGid(t *testing.T) {
|
|
repo, cleanup := repository.TestRepository(t)
|
|
defer cleanup()
|
|
|
|
restic.TestCreateSnapshot(t, repo, time.Unix(1460289341, 207401672), 0, 0)
|
|
|
|
testTopUidGid(t, Config{}, repo, uint32(os.Getuid()), uint32(os.Getgid()))
|
|
testTopUidGid(t, Config{OwnerIsRoot: true}, repo, 0, 0)
|
|
}
|
|
|
|
func testTopUidGid(t *testing.T, cfg Config, repo restic.Repository, uid, gid uint32) {
|
|
t.Helper()
|
|
|
|
ctx := context.Background()
|
|
root, err := NewRoot(ctx, repo, cfg)
|
|
rtest.OK(t, err)
|
|
|
|
var attr fuse.Attr
|
|
err = root.Attr(ctx, &attr)
|
|
rtest.OK(t, err)
|
|
rtest.Equals(t, uid, attr.Uid)
|
|
rtest.Equals(t, gid, attr.Gid)
|
|
|
|
idsdir, err := root.Lookup(ctx, "ids")
|
|
rtest.OK(t, err)
|
|
|
|
err = idsdir.Attr(ctx, &attr)
|
|
rtest.OK(t, err)
|
|
rtest.Equals(t, uid, attr.Uid)
|
|
rtest.Equals(t, gid, attr.Gid)
|
|
|
|
snapID := loadFirstSnapshot(t, repo).ID().Str()
|
|
snapshotdir, err := idsdir.(fs.NodeStringLookuper).Lookup(ctx, snapID)
|
|
|
|
err = snapshotdir.Attr(ctx, &attr)
|
|
rtest.OK(t, err)
|
|
rtest.Equals(t, uid, attr.Uid)
|
|
rtest.Equals(t, gid, attr.Gid)
|
|
}
|