Merge pull request #2760 from greatroar/backend-benchmark

Fix backend benchmarks + a micro-optimization
This commit is contained in:
MichaelEischer 2020-06-17 23:17:05 +02:00 committed by GitHub
commit 212607dc8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 31 deletions

View File

@ -48,17 +48,17 @@ func (s *Suite) BenchmarkLoadFile(t *testing.B) {
n, ierr = io.ReadFull(rd, buf) n, ierr = io.ReadFull(rd, buf)
return ierr return ierr
}) })
if err != nil {
t.StopTimer()
switch {
case err != nil:
t.Fatal(err) t.Fatal(err)
} case n != length:
if n != length {
t.Fatalf("wrong number of bytes read: want %v, got %v", length, n) t.Fatalf("wrong number of bytes read: want %v, got %v", length, n)
} case !bytes.Equal(data, buf):
if !bytes.Equal(data, buf) {
t.Fatalf("wrong bytes returned") t.Fatalf("wrong bytes returned")
} }
t.StartTimer()
} }
} }
@ -85,18 +85,17 @@ func (s *Suite) BenchmarkLoadPartialFile(t *testing.B) {
n, ierr = io.ReadFull(rd, buf) n, ierr = io.ReadFull(rd, buf)
return ierr return ierr
}) })
if err != nil {
t.StopTimer()
switch {
case err != nil:
t.Fatal(err) t.Fatal(err)
} case n != testLength:
if n != testLength {
t.Fatalf("wrong number of bytes read: want %v, got %v", testLength, n) t.Fatalf("wrong number of bytes read: want %v, got %v", testLength, n)
} case !bytes.Equal(data[:testLength], buf):
if !bytes.Equal(data[:testLength], buf) {
t.Fatalf("wrong bytes returned") t.Fatalf("wrong bytes returned")
} }
t.StartTimer()
} }
} }
@ -124,17 +123,17 @@ func (s *Suite) BenchmarkLoadPartialFileOffset(t *testing.B) {
n, ierr = io.ReadFull(rd, buf) n, ierr = io.ReadFull(rd, buf)
return ierr return ierr
}) })
if err != nil {
t.StopTimer()
switch {
case err != nil:
t.Fatal(err) t.Fatal(err)
} case n != testLength:
if n != testLength {
t.Fatalf("wrong number of bytes read: want %v, got %v", testLength, n) t.Fatalf("wrong number of bytes read: want %v, got %v", testLength, n)
} case !bytes.Equal(data[testOffset:testOffset+testLength], buf):
if !bytes.Equal(data[testOffset:testOffset+testLength], buf) {
t.Fatalf("wrong bytes returned") t.Fatalf("wrong bytes returned")
} }
t.StartTimer()
} }
} }

View File

@ -32,19 +32,14 @@ func LoadAll(ctx context.Context, buf []byte, be restic.Backend, h restic.Handle
// LimitedReadCloser wraps io.LimitedReader and exposes the Close() method. // LimitedReadCloser wraps io.LimitedReader and exposes the Close() method.
type LimitedReadCloser struct { type LimitedReadCloser struct {
io.ReadCloser io.Closer
io.Reader io.LimitedReader
} }
// Read reads data from the limited reader. // LimitReadCloser returns a new reader wraps r in an io.LimitedReader, but also
func (l *LimitedReadCloser) Read(p []byte) (int, error) {
return l.Reader.Read(p)
}
// LimitReadCloser returns a new reader wraps r in an io.LimitReader, but also
// exposes the Close() method. // exposes the Close() method.
func LimitReadCloser(r io.ReadCloser, n int64) *LimitedReadCloser { func LimitReadCloser(r io.ReadCloser, n int64) *LimitedReadCloser {
return &LimitedReadCloser{ReadCloser: r, Reader: io.LimitReader(r, n)} return &LimitedReadCloser{Closer: r, LimitedReader: io.LimitedReader{R: r, N: n}}
} }
// DefaultLoad implements Backend.Load using lower-level openReader func // DefaultLoad implements Backend.Load using lower-level openReader func