-
Notifications
You must be signed in to change notification settings - Fork 8
/
fio-bench_zh-cn.py
366 lines (332 loc) · 11.7 KB
/
fio-bench_zh-cn.py
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
#!/usr/bin/env python3
# coding: utf-8
import os
import sys
import shlex
import subprocess
import json
from prettytable import PrettyTable
import argparse
from argparse import RawTextHelpFormatter
# Init ENV
rwResult = {}
#Color
R = "\033[0;31;40m" #RED
G = "\033[0;32;40m" # GREEN
Y = "\033[0;33;40m" # Yellow
B = "\033[0;34;40m" # Blue
N = "\033[0m" # Reset
def bash(cmd):
return shlex.os.system(cmd)
def format_bytes(size):
size = float(size)
power = 2**10
n = 1
power_labels = {0: 'B/s', 1: 'KB/s', 2: 'MB/s', 3: 'GB/s', 4: 'TB/s'}
while size > power:
size /= power
n += 1
value = size
unit = "{}".format(power_labels[n])
return value, unit
def cleanup(filename=None):
os.remove(filename)
def printResult():
print(G + "\n测试结果:" + N)
table = PrettyTable(["Test Item", "读取 IOPS", "读取速度", "写入 IOPS", "写入速度"])
for k, v in rwResult.items():
list = [k, v["read_iops"], v["read_bw"], v["write_iops"], v["write_bw"]]
table.add_row(list)
table.align = "r"
table.align["Test Item"] = "l"
print(table.get_string(sortby="Test Item", reversesort=True))
def outputResult(filename=None):
if os.path.exists(filename):
os.remove(filename)
with open(filename, 'w+', encoding='utf-8') as fo:
fo.write(u"测试结果:\n")
table = PrettyTable(["Test Item", u"读取 IOPS", u"读取速度", u"写入 IOPS", u"写入速度"])
for k, v in rwResult.items():
list = [k, v["read_iops"], v["read_bw"], v["write_iops"], v["write_bw"]]
table.add_row(list)
table.align = "r"
table.align["Test Item"] = "l"
fo.write(table.get_string(sortby="Test Item", reversesort=True))
fo.write("\n")
print((G + "\n保存在 {}" + N).format(filename))
class FioTest(object):
def __init__(self,
name,
filename,
rw,
bs,
size,
direct=1,
iodepth=1,
ioengine="libaio",
runtime=60):
self.name = name
self.filename = filename
self.direct = direct
self.rw = rw
self.bs = bs
self.size = size
self.iodepth = iodepth
self.ioengine = ioengine
self.runtime = runtime
def exprCmd(self):
cmd = "fio --name=" + self.name + " --rw=" + self.rw + " --iodepth=" + str(
self.iodepth
) + " --ioengine=" + self.ioengine + " --thread --direct=" + str(
self.direct
) + " --norandommap --bs=" + self.bs + " --size=" + self.size + " --runtime=" + str(
self.runtime
) + " --filename=" + self.filename + " --minimal --output-format=json"
return cmd
def runCmd(self, cmd):
result_str = ''
process = subprocess.Popen(cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out = process.stdout
errors = process.stderr
err = errors.read()
if err:
print(R + "运行失败!" + N)
os._exit(1)
result_str = out.read().strip()
if out:
out.close()
if errors:
errors.close()
return result_str.decode('utf8')
def explain(self, result):
if self.rw == "read" or self.rw == "randread":
r = json.loads(result)
iops = r["jobs"][0]["read"]["iops"]
bw = r["jobs"][0]["read"]["bw"]
return iops, bw
else:
if self.rw == "write" or self.rw == "randwrite":
r = json.loads(result)
iops = r["jobs"][0]["write"]["iops"]
bw = r["jobs"][0]["write"]["bw"]
return iops, bw
def expResult(self, iops, bw):
name = self.name
rw_iops = ""
rw_bw = ""
if self.rw == "write":
rw_iops = "write_iops"
rw_bw = "write_bw"
elif self.rw == "read":
rw_iops = "read_iops"
rw_bw = "read_bw"
elif self.rw == "randwrite":
rw_iops = "write_iops"
rw_bw = "write_bw"
elif self.rw == "randread":
rw_iops = "read_iops"
rw_bw = "read_bw"
io_speed, unit = format_bytes(bw)
if name in rwResult.keys():
rwResult[name][rw_iops] = "{:d}".format(int(iops))
rwResult[name][rw_bw] = "{:.2f} {}".format(io_speed, unit) if (
unit == "GB/s" or unit == "TB/s") else "{:d} {}".format(
int(io_speed), unit)
else:
rwResult[name] = {}
rwResult[name][rw_iops] = "{:d}".format(int(iops))
rwResult[name][rw_bw] = "{:.2f} {}".format(io_speed, unit) if (
unit == "GB/s" or unit == "TB/s") else "{:d} {}".format(
int(io_speed), unit)
def saveResult(self):
cmd = self.exprCmd()
result = self.runCmd(cmd)
iops, bw = self.explain(result)
self.expResult(iops, bw)
if __name__ == '__main__':
defult_path = os.getcwd() + '/fio_test.bin'
# read parameters
parser = argparse.ArgumentParser(prog='fio 基准测试工具',
description='使用 fio 测量 HDD 和 SSD 的性能',
formatter_class=RawTextHelpFormatter)
parser.add_argument('-a',
'--all',
dest='alltest',
action='store_true',
required=False,
help='完整测试 [R/W in Seq Q32T1, 4K Q32T1, Seq, 4K] (默认).')
parser.add_argument(
'-t',
'--test',
metavar='integer',
dest='test_num',
type=int,
default=None,
required=False,
nargs='+',
help='可选测试 [1. Seq Q32T1], [2. 4K Q32T1], [3. Seq], [4. 4K].')
parser.add_argument('-s',
'--size',
metavar='String',
dest='test_size',
type=str,
default=None,
required=False,
nargs='+',
help='IO 测试文件大小 (默认 2g)')
parser.add_argument('-f',
'--file',
metavar='String',
dest='test_file',
type=str,
default=None,
required=False,
nargs='+',
help='IO 测试文件路径 (默认 {})'.format(defult_path))
parser.add_argument('-o',
'--output',
metavar='String',
dest='output_file',
type=str,
default=None,
required=False,
nargs='+',
help='将结果保存到文件')
args = parser.parse_args()
# get opts.
test1 = False
test2 = False
test3 = False
test4 = False
if args.test_num is None or args.alltest:
test1 = True
test2 = True
test3 = True
test4 = True
else:
if not all(elem in range(1, 4) for elem in args.test_num):
raise ValueError('无效测试选项')
else:
if 1 in args.test_num: test1 = True
if 2 in args.test_num: test2 = True
if 3 in args.test_num: test3 = True
if 4 in args.test_num: test4 = True
if args.test_size is None:
test_size = '2g'
else:
test_size = "".join(args.test_size)
if args.test_file is None:
test_file = defult_path
else:
test_file = "".join(args.test_file)
if args.output_file is None:
to_file=False
else:
to_file=True
output_file = "".join(args.output_file)
print(R + '开始进行下列测试:\n' + N)
if test1: print('- Seq Q32T1')
if test2: print('- 4K Q32T1')
if test3: print('- Seq')
if test4: print('- 4K')
print
if test1:
print(Y + '测试顺序读取 - 块大小 128KiB 队列深度 32 线程数 1' + N)
cmd = FioTest(name="Seq-Q32T1",
rw="read",
iodepth=32,
ioengine="libaio",
direct=1,
bs="128k",
size=test_size,
runtime=60,
filename=test_file)
cmd.saveResult()
print(Y + '测试顺序写入 - 块大小 128KiB 队列深度 32 线程数 1' + N)
cmd = FioTest(name="Seq-Q32T1",
rw="write",
iodepth=32,
ioengine="libaio",
direct=1,
bs="128k",
size=test_size,
runtime=60,
filename=test_file)
cmd.saveResult()
if test2:
print(Y + '测试 4K 随机读取 - 队列深度 32 线程数 1' + N)
cmd = FioTest(name="4K-Q32T1",
rw="randread",
iodepth=32,
ioengine="libaio",
direct=1,
bs="4k",
size=test_size,
runtime=60,
filename=test_file)
cmd.saveResult()
print(Y + '测试 4K 随机写入 - 队列深度 32 线程数 1' + N)
cmd = FioTest(name="4K-Q32T1",
rw="randwrite",
iodepth=32,
ioengine="libaio",
direct=1,
bs="4k",
size=test_size,
runtime=60,
filename=test_file)
cmd.saveResult()
if test3:
print(Y + '测试顺序读取 - 块大小 128MiB 队列深度 1 线程数 1' + N)
cmd = FioTest(name="Seq",
rw="read",
iodepth=1,
ioengine="libaio",
direct=1,
bs="1m",
size=test_size,
runtime=60,
filename=test_file)
cmd.saveResult()
print(Y + '测试顺序写入 - 块大小 128MiB 队列深度 1 线程数 1' + N)
cmd = FioTest(name="Seq",
rw="write",
iodepth=1,
ioengine="libaio",
direct=1,
bs="1m",
size=test_size,
runtime=60,
filename=test_file)
cmd.saveResult()
if test4:
print(Y + '测试 4K 随机读取 - 队列深度 1 线程数 1' + N)
cmd = FioTest(name="4K",
rw="randread",
iodepth=1,
ioengine="libaio",
direct=1,
bs="4k",
size=test_size,
runtime=60,
filename=test_file)
cmd.saveResult()
print(Y + '测试 4K 随机写入 - 队列深度 1 线程数 1' + N)
cmd = FioTest(name="4K",
rw="randwrite",
iodepth=1,
ioengine="libaio",
direct=1,
bs="4k",
size=test_size,
runtime=60,
filename=test_file)
cmd.saveResult()
cleanup(filename=test_file)
if to_file:
outputResult(filename=output_file)
else:
printResult()