2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-13 14:43:12 +00:00
restic/internal/backend/test/benchmarks.go

166 lines
3.9 KiB
Go
Raw Normal View History

2017-05-13 19:37:07 +00:00
package test
import (
"bytes"
2017-06-03 15:39:57 +00:00
"context"
2017-05-13 19:37:07 +00:00
"io"
"testing"
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/backend"
2017-07-24 15:42:25 +00:00
"github.com/restic/restic/internal/restic"
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/test"
2017-05-13 19:37:07 +00:00
)
func saveRandomFile(t testing.TB, be backend.Backend, length int) ([]byte, backend.Handle) {
2017-05-13 19:37:07 +00:00
data := test.Random(23, length)
id := restic.Hash(data)
handle := backend.Handle{Type: backend.PackFile, Name: id.String()}
err := be.Save(context.TODO(), handle, backend.NewByteReader(data, be.Hasher()))
if err != nil {
2017-05-13 19:37:07 +00:00
t.Fatalf("Save() error: %+v", err)
}
return data, handle
}
2017-05-13 19:37:07 +00:00
func remove(t testing.TB, be backend.Backend, h backend.Handle) {
2017-06-03 15:39:57 +00:00
if err := be.Remove(context.TODO(), h); err != nil {
t.Fatalf("Remove() returned error: %v", err)
}
}
// BenchmarkLoadFile benchmarks the Load() method of a backend by
2017-05-14 08:58:09 +00:00
// loading a complete file.
func (s *Suite[C]) BenchmarkLoadFile(t *testing.B) {
be := s.open(t)
defer s.close(t, be)
length := 1<<24 + 2123
data, handle := saveRandomFile(t, be, length)
defer remove(t, be, handle)
2017-05-13 19:37:07 +00:00
buf := make([]byte, length)
t.SetBytes(int64(length))
t.ResetTimer()
for i := 0; i < t.N; i++ {
var n int
err := be.Load(context.TODO(), handle, 0, 0, func(rd io.Reader) (ierr error) {
n, ierr = io.ReadFull(rd, buf)
return ierr
})
2017-05-13 19:37:07 +00:00
t.StopTimer()
switch {
case err != nil:
t.Fatal(err)
case n != length:
2017-05-13 19:37:07 +00:00
t.Fatalf("wrong number of bytes read: want %v, got %v", length, n)
case !bytes.Equal(data, buf):
2017-05-13 19:37:07 +00:00
t.Fatalf("wrong bytes returned")
}
t.StartTimer()
}
}
// BenchmarkLoadPartialFile benchmarks the Load() method of a backend by
2017-05-14 08:58:09 +00:00
// loading the remainder of a file starting at a given offset.
func (s *Suite[C]) BenchmarkLoadPartialFile(t *testing.B) {
be := s.open(t)
defer s.close(t, be)
datalength := 1<<24 + 2123
data, handle := saveRandomFile(t, be, datalength)
defer remove(t, be, handle)
testLength := datalength/4 + 555
buf := make([]byte, testLength)
t.SetBytes(int64(testLength))
t.ResetTimer()
for i := 0; i < t.N; i++ {
var n int
err := be.Load(context.TODO(), handle, testLength, 0, func(rd io.Reader) (ierr error) {
n, ierr = io.ReadFull(rd, buf)
return ierr
})
t.StopTimer()
switch {
case err != nil:
t.Fatal(err)
case n != testLength:
t.Fatalf("wrong number of bytes read: want %v, got %v", testLength, n)
case !bytes.Equal(data[:testLength], buf):
t.Fatalf("wrong bytes returned")
}
t.StartTimer()
}
}
// BenchmarkLoadPartialFileOffset benchmarks the Load() method of a
2017-05-14 08:58:09 +00:00
// backend by loading a number of bytes of a file starting at a given offset.
func (s *Suite[C]) BenchmarkLoadPartialFileOffset(t *testing.B) {
be := s.open(t)
defer s.close(t, be)
datalength := 1<<24 + 2123
data, handle := saveRandomFile(t, be, datalength)
defer remove(t, be, handle)
testLength := datalength/4 + 555
testOffset := 8273
buf := make([]byte, testLength)
t.SetBytes(int64(testLength))
t.ResetTimer()
for i := 0; i < t.N; i++ {
var n int
err := be.Load(context.TODO(), handle, testLength, int64(testOffset), func(rd io.Reader) (ierr error) {
n, ierr = io.ReadFull(rd, buf)
return ierr
})
t.StopTimer()
switch {
case err != nil:
t.Fatal(err)
case n != testLength:
t.Fatalf("wrong number of bytes read: want %v, got %v", testLength, n)
case !bytes.Equal(data[testOffset:testOffset+testLength], buf):
t.Fatalf("wrong bytes returned")
}
t.StartTimer()
2017-05-13 19:37:07 +00:00
}
}
2017-05-13 21:04:30 +00:00
// BenchmarkSave benchmarks the Save() method of a backend.
func (s *Suite[C]) BenchmarkSave(t *testing.B) {
2017-05-13 21:04:30 +00:00
be := s.open(t)
defer s.close(t, be)
length := 1<<24 + 2123
data := test.Random(23, length)
id := restic.Hash(data)
handle := backend.Handle{Type: backend.PackFile, Name: id.String()}
2017-05-13 21:04:30 +00:00
rd := backend.NewByteReader(data, be.Hasher())
2017-05-13 21:04:30 +00:00
t.SetBytes(int64(length))
t.ResetTimer()
for i := 0; i < t.N; i++ {
2017-06-03 15:39:57 +00:00
if err := be.Save(context.TODO(), handle, rd); err != nil {
2017-05-13 21:04:30 +00:00
t.Fatal(err)
}
2017-06-03 15:39:57 +00:00
if err := be.Remove(context.TODO(), handle); err != nil {
2017-05-13 21:04:30 +00:00
t.Fatal(err)
}
}
}