2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-11 07:41:03 +00:00

tests: Add benchmarks for partial file (+offset)

This commit is contained in:
Alexander Neumann 2017-05-13 22:24:15 +02:00
parent 77ebb95d3d
commit 8fc25cc567
2 changed files with 100 additions and 11 deletions

View File

@ -8,24 +8,29 @@ import (
"testing" "testing"
) )
// BackendBenchmarkLoad benchmarks the backend's Load function. func saveRandomFile(t testing.TB, be restic.Backend, length int) ([]byte, restic.Handle) {
func BackendBenchmarkLoadFile(t *testing.B, s *Suite) {
be := s.open(t)
defer s.close(t, be)
length := 1<<24 + 2123
data := test.Random(23, length) data := test.Random(23, length)
id := restic.Hash(data) id := restic.Hash(data)
handle := restic.Handle{Type: restic.DataFile, Name: id.String()} handle := restic.Handle{Type: restic.DataFile, Name: id.String()}
if err := be.Save(handle, bytes.NewReader(data)); err != nil { if err := be.Save(handle, bytes.NewReader(data)); err != nil {
t.Fatalf("Save() error: %+v", err) t.Fatalf("Save() error: %+v", err)
} }
return data, handle
}
defer func() { func remove(t testing.TB, be restic.Backend, h restic.Handle) {
if err := be.Remove(handle); err != nil { if err := be.Remove(h); err != nil {
t.Fatalf("Remove() returned error: %v", err) t.Fatalf("Remove() returned error: %v", err)
} }
}() }
func BackendBenchmarkLoadFile(t *testing.B, s *Suite) {
be := s.open(t)
defer s.close(t, be)
length := 1<<24 + 2123
data, handle := saveRandomFile(t, be, length)
defer remove(t, be, handle)
buf := make([]byte, length) buf := make([]byte, length)
@ -54,6 +59,88 @@ func BackendBenchmarkLoadFile(t *testing.B, s *Suite) {
if !bytes.Equal(data, buf) { if !bytes.Equal(data, buf) {
t.Fatalf("wrong bytes returned") t.Fatalf("wrong bytes returned")
} }
}
}
func BackendBenchmarkLoadPartialFile(t *testing.B, s *Suite) {
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++ {
rd, err := be.Load(handle, testLength, 0)
if err != nil {
t.Fatal(err)
}
n, err := io.ReadFull(rd, buf)
if err != nil {
t.Fatal(err)
}
if err = rd.Close(); err != nil {
t.Fatalf("Close() returned error: %v", err)
}
if n != testLength {
t.Fatalf("wrong number of bytes read: want %v, got %v", testLength, n)
}
if !bytes.Equal(data[:testLength], buf) {
t.Fatalf("wrong bytes returned")
}
}
}
func BackendBenchmarkLoadPartialFileOffset(t *testing.B, s *Suite) {
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++ {
rd, err := be.Load(handle, testLength, int64(testOffset))
if err != nil {
t.Fatal(err)
}
n, err := io.ReadFull(rd, buf)
if err != nil {
t.Fatal(err)
}
if err = rd.Close(); err != nil {
t.Fatalf("Close() returned error: %v", err)
}
if n != testLength {
t.Fatalf("wrong number of bytes read: want %v, got %v", testLength, n)
}
if !bytes.Equal(data[testOffset:testOffset+testLength], buf) {
t.Fatalf("wrong bytes returned")
}
} }
} }

View File

@ -25,4 +25,6 @@ var benchmarkFunctions = []struct {
Fn func(*testing.B, *Suite) Fn func(*testing.B, *Suite)
}{ }{
{"LoadFile", BackendBenchmarkLoadFile}, {"LoadFile", BackendBenchmarkLoadFile},
{"LoadPartialFile", BackendBenchmarkLoadPartialFile},
{"LoadPartialFileOffset", BackendBenchmarkLoadPartialFileOffset},
} }