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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
// Copyright 2020 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package fs
import (
"context"
"encoding/json"
"fmt"
"path/filepath"
"strconv"
"strings"
"testing"
"github.com/docker/docker/api/types/mount"
"gvisor.dev/gvisor/pkg/test/dockerutil"
"gvisor.dev/gvisor/test/benchmarks/harness"
)
type fioTestCase struct {
test string // test to run: read, write, randread, randwrite.
size string // total size to be read/written of format N[GMK] (e.g. 5G).
blocksize string // blocksize to be read/write of format N[GMK] (e.g. 4K).
iodepth int // iodepth for reads/writes.
time int // time to run the test in seconds, usually for rand(read/write).
}
// makeCmdFromTestcase makes a fio command.
func (f *fioTestCase) makeCmdFromTestcase(filename string) []string {
cmd := []string{"fio", "--output-format=json", "--ioengine=sync"}
cmd = append(cmd, fmt.Sprintf("--name=%s", f.test))
cmd = append(cmd, fmt.Sprintf("--size=%s", f.size))
cmd = append(cmd, fmt.Sprintf("--blocksize=%s", f.blocksize))
cmd = append(cmd, fmt.Sprintf("--filename=%s", filename))
cmd = append(cmd, fmt.Sprintf("--iodepth=%d", f.iodepth))
cmd = append(cmd, fmt.Sprintf("--rw=%s", f.test))
if f.time != 0 {
cmd = append(cmd, "--time_based")
cmd = append(cmd, fmt.Sprintf("--runtime=%d", f.time))
}
return cmd
}
// BenchmarkFio runs fio on the runtime under test. There are 4 basic test
// cases each run on a tmpfs mount and a bind mount. Fio requires root so that
// caches can be dropped.
func BenchmarkFio(b *testing.B) {
testCases := []fioTestCase{
fioTestCase{
test: "write",
size: "5G",
blocksize: "1M",
iodepth: 4,
},
fioTestCase{
test: "read",
size: "5G",
blocksize: "1M",
iodepth: 4,
},
fioTestCase{
test: "randwrite",
size: "5G",
blocksize: "4K",
iodepth: 4,
time: 30,
},
fioTestCase{
test: "randread",
size: "5G",
blocksize: "4K",
iodepth: 4,
time: 30,
},
}
machine, err := h.GetMachine()
if err != nil {
b.Fatalf("failed to get machine with: %v", err)
}
defer machine.CleanUp()
for _, fsType := range []mount.Type{mount.TypeBind, mount.TypeTmpfs} {
for _, tc := range testCases {
testName := strings.Title(tc.test) + strings.Title(string(fsType))
b.Run(testName, func(b *testing.B) {
ctx := context.Background()
container := machine.GetContainer(ctx, b)
defer container.CleanUp(ctx)
// Directory and filename inside container where fio will read/write.
outdir := "/data"
outfile := filepath.Join(outdir, "test.txt")
// Make the required mount and grab a cleanup for bind mounts
// as they are backed by a temp directory (mktemp).
mnt, mountCleanup, err := makeMount(machine, fsType, outdir)
if err != nil {
b.Fatalf("failed to make mount: %v", err)
}
defer mountCleanup()
cmd := tc.makeCmdFromTestcase(outfile)
// Start the container with the mount.
if err := container.Spawn(
ctx,
dockerutil.RunOpts{
Image: "benchmarks/fio",
Mounts: []mount.Mount{
mnt,
},
},
// Sleep on the order of b.N.
"sleep", fmt.Sprintf("%d", 1000*b.N),
); err != nil {
b.Fatalf("failed to start fio container with: %v", err)
}
// For reads, we need a file to read so make one inside the container.
if strings.Contains(tc.test, "read") {
fallocateCmd := fmt.Sprintf("fallocate -l %s %s", tc.size, outfile)
if out, err := container.Exec(ctx, dockerutil.ExecOpts{},
strings.Split(fallocateCmd, " ")...); err != nil {
b.Fatalf("failed to create readable file on mount: %v, %s", err, out)
}
}
// Drop caches just before running.
if err := harness.DropCaches(machine); err != nil {
b.Skipf("failed to drop caches with %v. You probably need root.", err)
}
container.RestartProfiles()
b.ResetTimer()
for i := 0; i < b.N; i++ {
// Run fio.
data, err := container.Exec(ctx, dockerutil.ExecOpts{}, cmd...)
if err != nil {
b.Fatalf("failed to run cmd %v: %v", cmd, err)
}
b.StopTimer()
// Parse the output and report the metrics.
isRead := strings.Contains(tc.test, "read")
bw, err := parseBandwidth(data, isRead)
if err != nil {
b.Fatalf("failed to parse bandwidth from %s with: %v", data, err)
}
b.ReportMetric(bw, "bandwidth") // in b/s.
iops, err := parseIOps(data, isRead)
if err != nil {
b.Fatalf("failed to parse iops from %s with: %v", data, err)
}
b.ReportMetric(iops, "iops")
// If b.N is used (i.e. we run for an hour), we should drop caches
// after each run.
if err := harness.DropCaches(machine); err != nil {
b.Fatalf("failed to drop caches: %v", err)
}
b.StartTimer()
}
})
}
}
}
// makeMount makes a mount and cleanup based on the requested type. Bind
// and volume mounts are backed by a temp directory made with mktemp.
// tmpfs mounts require no such backing and are just made.
// It is up to the caller to call the returned cleanup.
func makeMount(machine harness.Machine, mountType mount.Type, target string) (mount.Mount, func(), error) {
switch mountType {
case mount.TypeVolume, mount.TypeBind:
dir, err := machine.RunCommand("mktemp", "-d")
if err != nil {
return mount.Mount{}, func() {}, fmt.Errorf("failed to create tempdir: %v", err)
}
dir = strings.TrimSuffix(dir, "\n")
out, err := machine.RunCommand("chmod", "777", dir)
if err != nil {
machine.RunCommand("rm", "-rf", dir)
return mount.Mount{}, func() {}, fmt.Errorf("failed modify directory: %v %s", err, out)
}
return mount.Mount{
Target: target,
Source: dir,
Type: mount.TypeBind,
}, func() { machine.RunCommand("rm", "-rf", dir) }, nil
case mount.TypeTmpfs:
return mount.Mount{
Target: target,
Type: mount.TypeTmpfs,
}, func() {}, nil
default:
return mount.Mount{}, func() {}, fmt.Errorf("illegal mount time not supported: %v", mountType)
}
}
// parseBandwidth reports the bandwidth in b/s.
func parseBandwidth(data string, isRead bool) (float64, error) {
if isRead {
result, err := parseFioJSON(data, "read", "bw")
if err != nil {
return 0, err
}
return 1024 * result, nil
}
result, err := parseFioJSON(data, "write", "bw")
if err != nil {
return 0, err
}
return 1024 * result, nil
}
// parseIOps reports the write IO per second metric.
func parseIOps(data string, isRead bool) (float64, error) {
if isRead {
return parseFioJSON(data, "read", "iops")
}
return parseFioJSON(data, "write", "iops")
}
// fioResult is for parsing FioJSON.
type fioResult struct {
Jobs []fioJob
}
// fioJob is for parsing FioJSON.
type fioJob map[string]json.RawMessage
// fioMetrics is for parsing FioJSON.
type fioMetrics map[string]json.RawMessage
// parseFioJSON parses data and grabs "op" (read or write) and "metric"
// (bw or iops) from the JSON.
func parseFioJSON(data, op, metric string) (float64, error) {
var result fioResult
if err := json.Unmarshal([]byte(data), &result); err != nil {
return 0, fmt.Errorf("could not unmarshal data: %v", err)
}
if len(result.Jobs) < 1 {
return 0, fmt.Errorf("no jobs present to parse")
}
var metrics fioMetrics
if err := json.Unmarshal(result.Jobs[0][op], &metrics); err != nil {
return 0, fmt.Errorf("could not unmarshal jobs: %v", err)
}
if _, ok := metrics[metric]; !ok {
return 0, fmt.Errorf("no metric found for op: %s", op)
}
return strconv.ParseFloat(string(metrics[metric]), 64)
}
// TestParsers tests that the parsers work on sampleData.
func TestParsers(t *testing.T) {
sampleData := `
{
"fio version" : "fio-3.1",
"timestamp" : 1554837456,
"timestamp_ms" : 1554837456621,
"time" : "Tue Apr 9 19:17:36 2019",
"jobs" : [
{
"jobname" : "test",
"groupid" : 0,
"error" : 0,
"eta" : 2147483647,
"elapsed" : 1,
"job options" : {
"name" : "test",
"ioengine" : "sync",
"size" : "1073741824",
"filename" : "/disk/file.dat",
"iodepth" : "4",
"bs" : "4096",
"rw" : "write"
},
"read" : {
"io_bytes" : 0,
"io_kbytes" : 0,
"bw" : 123456,
"iops" : 1234.5678,
"runtime" : 0,
"total_ios" : 0,
"short_ios" : 0,
"bw_min" : 0,
"bw_max" : 0,
"bw_agg" : 0.000000,
"bw_mean" : 0.000000,
"bw_dev" : 0.000000,
"bw_samples" : 0,
"iops_min" : 0,
"iops_max" : 0,
"iops_mean" : 0.000000,
"iops_stddev" : 0.000000,
"iops_samples" : 0
},
"write" : {
"io_bytes" : 1073741824,
"io_kbytes" : 1048576,
"bw" : 1753471,
"iops" : 438367.892977,
"runtime" : 598,
"total_ios" : 262144,
"bw_min" : 1731120,
"bw_max" : 1731120,
"bw_agg" : 98.725328,
"bw_mean" : 1731120.000000,
"bw_dev" : 0.000000,
"bw_samples" : 1,
"iops_min" : 432780,
"iops_max" : 432780,
"iops_mean" : 432780.000000,
"iops_stddev" : 0.000000,
"iops_samples" : 1
}
}
]
}
`
// WriteBandwidth.
got, err := parseBandwidth(sampleData, false)
var want float64 = 1753471.0 * 1024
if err != nil {
t.Fatalf("parse failed with err: %v", err)
} else if got != want {
t.Fatalf("got: %f, want: %f", got, want)
}
// ReadBandwidth.
got, err = parseBandwidth(sampleData, true)
want = 123456 * 1024
if err != nil {
t.Fatalf("parse failed with err: %v", err)
} else if got != want {
t.Fatalf("got: %f, want: %f", got, want)
}
// WriteIOps.
got, err = parseIOps(sampleData, false)
want = 438367.892977
if err != nil {
t.Fatalf("parse failed with err: %v", err)
} else if got != want {
t.Fatalf("got: %f, want: %f", got, want)
}
// ReadIOps.
got, err = parseIOps(sampleData, true)
want = 1234.5678
if err != nil {
t.Fatalf("parse failed with err: %v", err)
} else if got != want {
t.Fatalf("got: %f, want: %f", got, want)
}
}
|