summaryrefslogtreecommitdiff
path: root/internal/backend/test/benchmarks.go
blob: 150ef3987467679d208b833d2e35e4e9a69f0653 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package test

import (
	"bytes"
	"context"
	"io"
	"testing"

	"github.com/restic/restic/internal/restic"
	"github.com/restic/restic/internal/test"
)

func saveRandomFile(t testing.TB, be restic.Backend, length int) ([]byte, restic.Handle) {
	data := test.Random(23, length)
	id := restic.Hash(data)
	handle := restic.Handle{Type: restic.PackFile, Name: id.String()}
	err := be.Save(context.TODO(), handle, restic.NewByteReader(data, be.Hasher()))
	if err != nil {
		t.Fatalf("Save() error: %+v", err)
	}
	return data, handle
}

func remove(t testing.TB, be restic.Backend, h restic.Handle) {
	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
// 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)

	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
		})

		t.StopTimer()
		switch {
		case err != nil:
			t.Fatal(err)
		case n != length:
			t.Fatalf("wrong number of bytes read: want %v, got %v", length, n)
		case !bytes.Equal(data, buf):
			t.Fatalf("wrong bytes returned")
		}
		t.StartTimer()
	}
}

// BenchmarkLoadPartialFile benchmarks the Load() method of a backend by
// 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
// 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()

	}
}

// BenchmarkSave benchmarks the Save() method of a backend.
func (s *Suite[C]) BenchmarkSave(t *testing.B) {
	be := s.open(t)
	defer s.close(t, be)

	length := 1<<24 + 2123
	data := test.Random(23, length)
	id := restic.Hash(data)
	handle := restic.Handle{Type: restic.PackFile, Name: id.String()}

	rd := restic.NewByteReader(data, be.Hasher())
	t.SetBytes(int64(length))
	t.ResetTimer()

	for i := 0; i < t.N; i++ {
		if err := be.Save(context.TODO(), handle, rd); err != nil {
			t.Fatal(err)
		}

		if err := be.Remove(context.TODO(), handle); err != nil {
			t.Fatal(err)
		}
	}
}