forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_test.go
302 lines (281 loc) · 7.66 KB
/
benchmark_test.go
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Copyright 2016 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
// These benchmarks can be run with:
// go test -test.bench=. -benchmem
// go test -test.bench=. -benchmem -tags fuse
// go test -test.bench=. -benchmem -tags dokan
package test
import (
"fmt"
"testing"
"time"
"github.com/keybase/kbfs/libkbfs"
)
// BenchmarkWriteSeq512 writes to a large file in 512 byte writes.
func BenchmarkWriteSeq512(b *testing.B) {
benchmarkWriteSeqN(b, 512, 0xFFFFFFFFFFFF)
}
func BenchmarkWriteSeq4k(b *testing.B) {
benchmarkWriteSeqN(b, 4*1024, 0xFFFFFFFFFFFF)
}
func BenchmarkWriteSeq64k(b *testing.B) {
benchmarkWriteSeqN(b, 64*1024, 0xFFFFFFFFFFFF)
}
func BenchmarkWriteSeq512k(b *testing.B) {
benchmarkWriteSeqN(b, 512*1024, 0xFFFFFFFFFFFF)
}
// BenchmarkWrite1mb512 writes to a 1mb file in 512 byte writes.
func BenchmarkWrite1mb512(b *testing.B) {
benchmarkWriteSeqN(b, 512, 0xFFFFF)
}
func BenchmarkWrite1mb4k(b *testing.B) {
benchmarkWriteSeqN(b, 4*1024, 0xFFFFF)
}
func BenchmarkWrite1mb64k(b *testing.B) {
benchmarkWriteSeqN(b, 64*1024, 0xFFFFF)
}
func BenchmarkWrite1mb512k(b *testing.B) {
benchmarkWriteSeqN(b, 512*1024, 0xFFFFF)
}
func benchmarkWriteSeqN(b *testing.B, n int64, mask int64) {
buf := make([]byte, n)
b.SetBytes(n)
benchmark(b, silentBenchmark{b},
users("alice"),
as(alice,
custom(func(cb func(fileOp) error) error {
err := cb(mkfile("bench", ""))
if err != nil {
return err
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
err = cb(pwriteBS("bench", buf, (int64(i)*n)&mask))
if err != nil {
return err
}
}
b.StopTimer()
return nil
}),
),
)
}
// BenchmarkReadHoleSeq512 reads from a large file in 512 byte reads.
func BenchmarkReadHoleSeq512(b *testing.B) {
benchmarkReadSeqHoleN(b, 512, 0xFFFFFFF)
}
func BenchmarkReadHoleSeq4k(b *testing.B) {
benchmarkReadSeqHoleN(b, 4*1024, 0xFFFFFFF)
}
func BenchmarkReadHoleSeq64k(b *testing.B) {
benchmarkReadSeqHoleN(b, 64*1024, 0xFFFFFFF)
}
func BenchmarkReadHoleSeq512k(b *testing.B) {
benchmarkReadSeqHoleN(b, 512*1024, 0xFFFFFFF)
}
// BenchmarkReadHole1mb512 reads from a 1mb file in 512 byte reads.
func BenchmarkReadHole1mb512(b *testing.B) {
benchmarkReadSeqHoleN(b, 512, 0xFFFFF)
}
func BenchmarkReadHole1mb4k(b *testing.B) {
benchmarkReadSeqHoleN(b, 4*1024, 0xFFFFF)
}
func BenchmarkReadHole1mb64k(b *testing.B) {
benchmarkReadSeqHoleN(b, 64*1024, 0xFFFFF)
}
func BenchmarkReadHole1mb512k(b *testing.B) {
benchmarkReadSeqHoleN(b, 512*1024, 0xFFFFF)
}
func benchmarkReadSeqHoleN(b *testing.B, n int64, mask int64) {
buf := make([]byte, n)
b.SetBytes(n)
benchmark(b, silentBenchmark{b},
users("alice"),
as(alice,
custom(func(cb func(fileOp) error) error {
err := cb(mkfile("bench", ""))
if err != nil {
return err
}
err = cb(truncate("bench", uint64(mask+1)))
if err != nil {
return err
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
err = cb(preadBS("bench", buf, (int64(i)*n)&mask))
if err != nil {
return err
}
}
b.StopTimer()
return nil
}),
),
)
}
func benchmarkDoBenchWrites(b *testing.B, cb func(fileOp) error,
numWritesPerFile int, buf []byte, startIter int) error {
for i := startIter; i < b.N+startIter; i++ {
name := fmt.Sprintf("bench%d", i)
err := cb(mkfile(name, ""))
if err != nil {
return err
}
for j := 0; j < numWritesPerFile; j++ {
// make each block unique
for k := 0; k < 1+len(buf)/libkbfs.MaxBlockSizeBytesDefault; k++ {
buf[k] = byte(i)
buf[k+1] = byte(j)
buf[k+2] = byte(k)
}
// Only sync after the last write
sync := j+1 == numWritesPerFile
err = cb(pwriteBSSync(name, buf,
int64(j)*int64(len(buf)), sync))
if err != nil {
return err
}
}
}
return nil
}
func benchmarkWriteWithBandwidthHelper(b *testing.B, fileBytes int64,
perWriteBytes int64, writebwKBps int, doWarmUp bool) {
buf := make([]byte, perWriteBytes)
b.SetBytes(fileBytes)
numWritesPerFile := int(fileBytes / perWriteBytes)
benchmark(b, silentBenchmark{b},
users("alice"),
blockSize(512<<10),
bandwidth(writebwKBps),
opTimeout(19*time.Second),
as(alice,
custom(func(cb func(fileOp) error) error {
startIter := 0
if doWarmUp {
if err := benchmarkDoBenchWrites(b, cb,
numWritesPerFile, buf, 0); err != nil {
return err
}
startIter = b.N
}
b.ResetTimer()
defer b.StopTimer()
return benchmarkDoBenchWrites(b, cb, numWritesPerFile, buf,
startIter)
}),
),
)
}
func benchmarkWriteWithBandwidthPlusWarmup(b *testing.B, fileBytes int64,
perWriteBytes int64, writebwKBps int) {
benchmarkWriteWithBandwidthHelper(b, fileBytes, perWriteBytes,
writebwKBps, true)
}
func benchmarkWriteWithBandwidth(b *testing.B, fileBytes int64,
perWriteBytes int64, writebwKBps int) {
benchmarkWriteWithBandwidthHelper(b, fileBytes, perWriteBytes,
writebwKBps, false)
}
func BenchmarkWriteMediumFileLowBandwidth(b *testing.B) {
benchmarkWriteWithBandwidth(b, 10<<20 /* 10 MB */, 1<<16, /* 65 KB writes */
100 /* 100 KBps */)
}
func BenchmarkWriteBigFileNormalBandwidth(b *testing.B) {
// Warm up to get the buffer as large as possible
benchmarkWriteWithBandwidthPlusWarmup(b, 100<<20, /* 100 MB */
1<<16 /* 65 KB writes */, 11*1024/8 /* 11 Mbps */)
}
func BenchmarkWriteBigFileBigBandwidth(b *testing.B) {
// Warm up to get the buffer as large as possible
benchmarkWriteWithBandwidthPlusWarmup(b, 1<<30, /* 1 GB */
1<<20 /*1 MB writes */, 100*1024/8 /* 100 Mbps */)
}
func BenchmarkWriteMixedFilesNormalBandwidth(b *testing.B) {
perWriteBytes := int64(1 << 16) // 65 KB writes
buf := make([]byte, perWriteBytes)
// Files:
// * 100 MB to set the buffer size appropriately
// * A bunch of 2 MB files
// * Another 100 MB to make sure the buffer is still sized right
fileSizes := []int64{100 << 20}
for i := 0; i < 50; i++ {
fileSizes = append(fileSizes, 2<<20)
}
fileSizes = append(fileSizes, 100<<20)
var totalSize int64
for _, size := range fileSizes {
totalSize += size
}
b.SetBytes(totalSize)
benchmark(b, silentBenchmark{b},
users("alice"),
blockSize(512<<10),
bandwidth(11*1024/8 /* 11 Mbps */),
opTimeout(19*time.Second),
as(alice,
custom(func(cb func(fileOp) error) error {
b.ResetTimer()
defer b.StopTimer()
currIter := 0
for _, fileSize := range fileSizes {
numWrites := int(fileSize / perWriteBytes)
if err := benchmarkDoBenchWrites(b, cb,
numWrites, buf, currIter); err != nil {
return err
}
currIter += b.N
}
return nil
}),
),
)
}
func BenchmarkMultiFileSync(b *testing.B) {
numFiles := 20
fileSize := 5
benchmark(b, silentBenchmark{b},
journal(),
users("alice"),
as(alice,
enableJournal(),
custom(func(cb func(fileOp) error) error {
files := make([]string, numFiles*b.N)
bufs := make([][]byte, numFiles*b.N)
for i := 0; i < numFiles*b.N; i++ {
files[i] = fmt.Sprintf("a/b/c/file%d", i)
bufs[i] = make([]byte, fileSize)
for j := 0; j < fileSize; j++ {
bufs[i][j] = byte(i*fileSize + j)
}
err := cb(truncate(files[i], 0))
if err != nil {
return err
}
}
b.ResetTimer()
defer b.StopTimer()
for iter := 0; iter < b.N; iter++ {
// Write to each file without syncing.
for i := iter * numFiles; i < numFiles; i++ {
err := cb(pwriteBSSync(files[i], bufs[i], 0, false))
if err != nil {
return err
}
}
// Sync each file by doing a no-op truncate.
for i := iter * numFiles; i < numFiles; i++ {
err := cb(truncate(files[i], uint64(fileSize)))
if err != nil {
return err
}
}
}
return nil
}),
),
)
}