koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 1 | import re |
| 2 | import os |
| 3 | import sys |
| 4 | import stat |
| 5 | import time |
| 6 | import json |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 7 | import Queue |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 8 | import os.path |
| 9 | import argparse |
| 10 | import warnings |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 11 | import threading |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 12 | import subprocess |
| 13 | |
| 14 | |
| 15 | class BenchmarkOption(object): |
| 16 | def __init__(self, concurence, iodepth, action, blocksize, size): |
| 17 | self.iodepth = iodepth |
| 18 | self.action = action |
| 19 | self.blocksize = blocksize |
| 20 | self.concurence = concurence |
| 21 | self.size = size |
| 22 | self.direct_io = False |
| 23 | self.use_hight_io_priority = True |
| 24 | self.sync = False |
| 25 | |
| 26 | |
koder aka kdanilov | 98615bf | 2015-02-02 00:59:07 +0200 | [diff] [blame] | 27 | def which(program): |
| 28 | def is_exe(fpath): |
| 29 | return os.path.isfile(fpath) and os.access(fpath, os.X_OK) |
| 30 | |
| 31 | fpath, fname = os.path.split(program) |
| 32 | if fpath: |
| 33 | if is_exe(program): |
| 34 | return program |
| 35 | else: |
| 36 | for path in os.environ["PATH"].split(os.pathsep): |
| 37 | path = path.strip('"') |
| 38 | exe_file = os.path.join(path, program) |
| 39 | if is_exe(exe_file): |
| 40 | return exe_file |
| 41 | |
| 42 | return None |
| 43 | |
| 44 | |
| 45 | # ------------------------------ IOZONE SUPPORT ------------------------------ |
| 46 | |
| 47 | |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 48 | class IOZoneParser(object): |
| 49 | "class to parse iozone results" |
| 50 | |
| 51 | start_tests = re.compile(r"^\s+KB\s+reclen\s+") |
| 52 | resuts = re.compile(r"[\s0-9]+") |
| 53 | mt_iozone_re = re.compile(r"\s+Children see throughput " + |
| 54 | r"for\s+\d+\s+(?P<cmd>.*?)\s+=\s+" + |
| 55 | r"(?P<perf>[\d.]+)\s+KB/sec") |
| 56 | |
| 57 | cmap = {'initial writers': 'write', |
| 58 | 'rewriters': 'rewrite', |
| 59 | 'initial readers': 'read', |
| 60 | 're-readers': 'reread', |
| 61 | 'random readers': 'random read', |
koder aka kdanilov | 7dec9df | 2015-02-15 21:35:19 -0800 | [diff] [blame] | 62 | 'random writers': 'random write', |
| 63 | 'readers': 'read'} |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 64 | |
| 65 | string1 = " " + \ |
| 66 | " random random " + \ |
| 67 | "bkwd record stride " |
| 68 | |
| 69 | string2 = "KB reclen write rewrite " + \ |
| 70 | "read reread read write " + \ |
| 71 | "read rewrite read fwrite frewrite fread freread" |
| 72 | |
| 73 | @classmethod |
| 74 | def apply_parts(cls, parts, string, sep=' \t\n'): |
| 75 | add_offset = 0 |
| 76 | for part in parts: |
| 77 | _, start, stop = part |
| 78 | start += add_offset |
| 79 | add_offset = 0 |
| 80 | |
| 81 | # condition splited to make pylint happy |
| 82 | while stop + add_offset < len(string): |
| 83 | |
| 84 | # condition splited to make pylint happy |
| 85 | if not (string[stop + add_offset] not in sep): |
| 86 | break |
| 87 | |
| 88 | add_offset += 1 |
| 89 | |
| 90 | yield part, string[start:stop + add_offset] |
| 91 | |
| 92 | @classmethod |
| 93 | def make_positions(cls): |
| 94 | items = [i for i in cls.string2.split() if i] |
| 95 | |
| 96 | pos = 0 |
| 97 | cls.positions = [] |
| 98 | |
| 99 | for item in items: |
| 100 | npos = cls.string2.index(item, 0 if pos == 0 else pos + 1) |
| 101 | cls.positions.append([item, pos, npos + len(item)]) |
| 102 | pos = npos + len(item) |
| 103 | |
| 104 | for itm, val in cls.apply_parts(cls.positions, cls.string1): |
| 105 | if val.strip(): |
| 106 | itm[0] = val.strip() + " " + itm[0] |
| 107 | |
| 108 | @classmethod |
| 109 | def parse_iozone_res(cls, res, mthreads=False): |
| 110 | parsed_res = None |
| 111 | |
| 112 | sres = res.split('\n') |
| 113 | |
| 114 | if not mthreads: |
| 115 | for pos, line in enumerate(sres[1:]): |
| 116 | if line.strip() == cls.string2 and \ |
| 117 | sres[pos].strip() == cls.string1.strip(): |
| 118 | add_pos = line.index(cls.string2) |
| 119 | parsed_res = {} |
| 120 | |
| 121 | npos = [(name, start + add_pos, stop + add_pos) |
| 122 | for name, start, stop in cls.positions] |
| 123 | |
| 124 | for itm, res in cls.apply_parts(npos, sres[pos + 2]): |
| 125 | if res.strip() != '': |
| 126 | parsed_res[itm[0]] = int(res.strip()) |
| 127 | |
| 128 | del parsed_res['KB'] |
| 129 | del parsed_res['reclen'] |
| 130 | else: |
| 131 | parsed_res = {} |
| 132 | for line in sres: |
| 133 | rr = cls.mt_iozone_re.match(line) |
| 134 | if rr is not None: |
| 135 | cmd = rr.group('cmd') |
| 136 | key = cls.cmap.get(cmd, cmd) |
| 137 | perf = int(float(rr.group('perf'))) |
| 138 | parsed_res[key] = perf |
| 139 | return parsed_res |
| 140 | |
| 141 | |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 142 | IOZoneParser.make_positions() |
| 143 | |
| 144 | |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 145 | def iozone_do_prepare(params, filename, pattern_base): |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 146 | all_files = [] |
| 147 | threads = int(params.concurence) |
| 148 | if 1 != threads: |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 149 | filename = filename + "_{0}" |
| 150 | all_files.extend(filename .format(i) for i in range(threads)) |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 151 | else: |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 152 | all_files.append(filename) |
| 153 | |
| 154 | bsz = 1024 if params.size > 1024 else params.size |
| 155 | if params.size % bsz != 0: |
| 156 | fsz = (params.size // bsz + 1) * bsz |
| 157 | else: |
| 158 | fsz = params.size |
| 159 | |
| 160 | for fname in all_files: |
koder aka kdanilov | 4a72f12 | 2015-02-09 12:25:54 +0200 | [diff] [blame] | 161 | with open(fname, "wb") as fd: |
| 162 | if fsz > 1024: |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 163 | pattern = pattern_base * 1024 * 1024 |
koder aka kdanilov | 4a72f12 | 2015-02-09 12:25:54 +0200 | [diff] [blame] | 164 | for _ in range(int(fsz / 1024) + 1): |
| 165 | fd.write(pattern) |
| 166 | else: |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 167 | fd.write(pattern_base * 1024 * fsz) |
koder aka kdanilov | 4a72f12 | 2015-02-09 12:25:54 +0200 | [diff] [blame] | 168 | fd.flush() |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 169 | return all_files |
| 170 | |
| 171 | |
| 172 | VERIFY_PATTERN = "\x6d" |
| 173 | |
| 174 | |
| 175 | def do_run_iozone(params, filename, timeout, iozone_path='iozone', |
| 176 | microsecond_mode=False, |
| 177 | prepare_only=False): |
| 178 | |
| 179 | cmd = [iozone_path, "-V", str(ord(VERIFY_PATTERN))] |
| 180 | |
| 181 | if params.sync: |
| 182 | cmd.append('-o') |
| 183 | |
| 184 | if params.direct_io: |
| 185 | cmd.append('-I') |
| 186 | |
| 187 | if microsecond_mode: |
| 188 | cmd.append('-N') |
| 189 | |
| 190 | all_files = iozone_do_prepare(params, filename, VERIFY_PATTERN) |
| 191 | |
| 192 | threads = int(params.concurence) |
| 193 | if 1 != threads: |
| 194 | cmd.extend(('-t', str(threads), '-F')) |
| 195 | cmd.extend(all_files) |
| 196 | else: |
| 197 | cmd.extend(('-f', all_files[0])) |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 198 | |
| 199 | cmd.append('-i') |
| 200 | |
| 201 | if params.action == 'write': |
| 202 | cmd.append("0") |
koder aka kdanilov | 4a72f12 | 2015-02-09 12:25:54 +0200 | [diff] [blame] | 203 | elif params.action == 'read': |
| 204 | cmd.append("1") |
| 205 | elif params.action == 'randwrite' or params.action == 'randread': |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 206 | cmd.append("2") |
| 207 | else: |
| 208 | raise ValueError("Unknown action {0!r}".format(params.action)) |
| 209 | |
| 210 | cmd.extend(('-s', str(params.size))) |
| 211 | cmd.extend(('-r', str(params.blocksize))) |
| 212 | |
koder aka kdanilov | 78ba895 | 2015-02-03 01:11:23 +0200 | [diff] [blame] | 213 | # no retest |
| 214 | cmd.append('-+n') |
| 215 | |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 216 | raw_res = subprocess.check_output(cmd) |
| 217 | |
| 218 | try: |
| 219 | parsed_res = IOZoneParser.parse_iozone_res(raw_res, threads > 1) |
| 220 | |
| 221 | res = {} |
| 222 | |
| 223 | if params.action == 'write': |
| 224 | res['bw_mean'] = parsed_res['write'] |
| 225 | elif params.action == 'randwrite': |
| 226 | res['bw_mean'] = parsed_res['random write'] |
| 227 | elif params.action == 'read': |
| 228 | res['bw_mean'] = parsed_res['read'] |
| 229 | elif params.action == 'randread': |
| 230 | res['bw_mean'] = parsed_res['random read'] |
| 231 | except: |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 232 | raise |
| 233 | |
koder aka kdanilov | 4a72f12 | 2015-02-09 12:25:54 +0200 | [diff] [blame] | 234 | return res, " ".join(cmd) |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 235 | |
| 236 | |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 237 | def run_iozone(benchmark, iozone_path, tmpname, |
| 238 | prepare_only=False, |
| 239 | timeout=None): |
| 240 | |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 241 | if timeout is not None: |
| 242 | benchmark.size = benchmark.blocksize * 50 |
| 243 | res_time = do_run_iozone(benchmark, tmpname, timeout, |
| 244 | iozone_path=iozone_path, |
koder aka kdanilov | 4a72f12 | 2015-02-09 12:25:54 +0200 | [diff] [blame] | 245 | microsecond_mode=True)[0] |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 246 | |
| 247 | size = (benchmark.blocksize * timeout * 1000000) |
| 248 | size /= res_time["bw_mean"] |
| 249 | size = (size // benchmark.blocksize + 1) * benchmark.blocksize |
| 250 | benchmark.size = size |
| 251 | |
| 252 | return do_run_iozone(benchmark, tmpname, timeout, |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 253 | iozone_path=iozone_path, prepare_only=prepare_only) |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 254 | |
| 255 | |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 256 | def install_iozone_package(): |
| 257 | if which('iozone'): |
| 258 | return |
| 259 | |
| 260 | is_redhat = os.path.exists('/etc/centos-release') |
| 261 | is_redhat = is_redhat or os.path.exists('/etc/fedora-release') |
| 262 | is_redhat = is_redhat or os.path.exists('/etc/redhat-release') |
| 263 | |
| 264 | if is_redhat: |
| 265 | subprocess.check_output(["yum", "install", 'iozone3']) |
| 266 | return |
| 267 | |
| 268 | try: |
| 269 | os_release_cont = open('/etc/os-release').read() |
| 270 | |
| 271 | is_ubuntu = "Ubuntu" in os_release_cont |
| 272 | |
| 273 | if is_ubuntu or "Debian GNU/Linux" in os_release_cont: |
| 274 | subprocess.check_output(["apt-get", "install", "iozone3"]) |
| 275 | return |
koder aka kdanilov | 4a72f12 | 2015-02-09 12:25:54 +0200 | [diff] [blame] | 276 | except (IOError, OSError): |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 277 | pass |
| 278 | |
| 279 | raise RuntimeError("Unknown host OS.") |
| 280 | |
| 281 | |
| 282 | def install_iozone_static(iozone_url, dst): |
| 283 | if not os.path.isfile(dst): |
| 284 | import urllib |
| 285 | urllib.urlretrieve(iozone_url, dst) |
| 286 | |
| 287 | st = os.stat(dst) |
| 288 | os.chmod(dst, st.st_mode | stat.S_IEXEC) |
| 289 | |
| 290 | |
koder aka kdanilov | 98615bf | 2015-02-02 00:59:07 +0200 | [diff] [blame] | 291 | def locate_iozone(): |
| 292 | binary_path = which('iozone') |
| 293 | |
| 294 | if binary_path is None: |
| 295 | binary_path = which('iozone3') |
| 296 | |
| 297 | if binary_path is None: |
| 298 | sys.stderr.write("Can't found neither iozone not iozone3 binary" |
Kostiantyn Danylov aka koder | 02adc1d | 2015-02-02 01:10:04 +0200 | [diff] [blame] | 299 | "Provide --bonary-path or --binary-url option") |
koder aka kdanilov | 4ec0f71 | 2015-02-19 19:19:27 -0800 | [diff] [blame^] | 300 | return None |
koder aka kdanilov | 98615bf | 2015-02-02 00:59:07 +0200 | [diff] [blame] | 301 | |
koder aka kdanilov | 4ec0f71 | 2015-02-19 19:19:27 -0800 | [diff] [blame^] | 302 | return binary_path |
koder aka kdanilov | 98615bf | 2015-02-02 00:59:07 +0200 | [diff] [blame] | 303 | |
| 304 | # ------------------------------ FIO SUPPORT --------------------------------- |
| 305 | |
| 306 | |
| 307 | def run_fio_once(benchmark, fio_path, tmpname, timeout=None): |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 308 | if benchmark.size is None: |
| 309 | raise ValueError("You need to specify file size for fio") |
koder aka kdanilov | 98615bf | 2015-02-02 00:59:07 +0200 | [diff] [blame] | 310 | |
| 311 | cmd_line = [fio_path, |
| 312 | "--name=%s" % benchmark.action, |
| 313 | "--rw=%s" % benchmark.action, |
| 314 | "--blocksize=%sk" % benchmark.blocksize, |
| 315 | "--iodepth=%d" % benchmark.iodepth, |
| 316 | "--filename=%s" % tmpname, |
| 317 | "--size={0}k".format(benchmark.size), |
| 318 | "--numjobs={0}".format(benchmark.concurence), |
| 319 | "--output-format=json", |
| 320 | "--sync=" + ('1' if benchmark.sync else '0')] |
| 321 | |
| 322 | if timeout is not None: |
| 323 | cmd_line.append("--timeout=%d" % timeout) |
| 324 | cmd_line.append("--runtime=%d" % timeout) |
| 325 | |
| 326 | if benchmark.direct_io: |
| 327 | cmd_line.append("--direct=1") |
| 328 | |
| 329 | if benchmark.use_hight_io_priority: |
| 330 | cmd_line.append("--prio=0") |
| 331 | |
| 332 | raw_out = subprocess.check_output(cmd_line) |
koder aka kdanilov | 4a72f12 | 2015-02-09 12:25:54 +0200 | [diff] [blame] | 333 | return json.loads(raw_out)["jobs"][0], " ".join(cmd_line) |
koder aka kdanilov | 98615bf | 2015-02-02 00:59:07 +0200 | [diff] [blame] | 334 | |
| 335 | |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 336 | def run_fio(benchmark, fio_path, tmpname, prepare_only=False, timeout=None): |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 337 | if prepare_only: |
| 338 | return {}, "" |
| 339 | |
koder aka kdanilov | 4a72f12 | 2015-02-09 12:25:54 +0200 | [diff] [blame] | 340 | job_output, cmd_line = run_fio_once(benchmark, fio_path, tmpname, timeout) |
koder aka kdanilov | 98615bf | 2015-02-02 00:59:07 +0200 | [diff] [blame] | 341 | |
| 342 | if benchmark.action in ('write', 'randwrite'): |
| 343 | raw_result = job_output['write'] |
| 344 | else: |
| 345 | raw_result = job_output['read'] |
| 346 | |
| 347 | res = {} |
koder aka kdanilov | 78ba895 | 2015-02-03 01:11:23 +0200 | [diff] [blame] | 348 | |
| 349 | # 'bw_dev bw_mean bw_max bw_min'.split() |
| 350 | for field in ["bw_mean"]: |
koder aka kdanilov | 98615bf | 2015-02-02 00:59:07 +0200 | [diff] [blame] | 351 | res[field] = raw_result[field] |
| 352 | |
koder aka kdanilov | 4a72f12 | 2015-02-09 12:25:54 +0200 | [diff] [blame] | 353 | return res, cmd_line |
koder aka kdanilov | 98615bf | 2015-02-02 00:59:07 +0200 | [diff] [blame] | 354 | |
| 355 | |
| 356 | def locate_fio(): |
koder aka kdanilov | 4ec0f71 | 2015-02-19 19:19:27 -0800 | [diff] [blame^] | 357 | return which('fio') |
koder aka kdanilov | 98615bf | 2015-02-02 00:59:07 +0200 | [diff] [blame] | 358 | |
| 359 | |
| 360 | # ---------------------------------------------------------------------------- |
| 361 | |
| 362 | |
koder aka kdanilov | 4ec0f71 | 2015-02-19 19:19:27 -0800 | [diff] [blame^] | 363 | def locate_binary(binary_tp, binary_path): |
| 364 | if binary_path is not None: |
koder aka kdanilov | 98615bf | 2015-02-02 00:59:07 +0200 | [diff] [blame] | 365 | if os.path.isfile(binary_path): |
| 366 | if not os.access(binary_path, os.X_OK): |
| 367 | st = os.stat(binary_path) |
| 368 | os.chmod(binary_path, st.st_mode | stat.S_IEXEC) |
| 369 | else: |
| 370 | binary_path = None |
| 371 | |
| 372 | if binary_path is not None: |
koder aka kdanilov | 4ec0f71 | 2015-02-19 19:19:27 -0800 | [diff] [blame^] | 373 | return binary_path |
koder aka kdanilov | 98615bf | 2015-02-02 00:59:07 +0200 | [diff] [blame] | 374 | |
| 375 | if 'iozone' == binary_tp: |
| 376 | return locate_iozone() |
| 377 | else: |
| 378 | return locate_fio() |
| 379 | |
| 380 | |
| 381 | def run_benchmark(binary_tp, *argv, **kwargs): |
| 382 | if 'iozone' == binary_tp: |
| 383 | return run_iozone(*argv, **kwargs) |
| 384 | else: |
| 385 | return run_fio(*argv, **kwargs) |
| 386 | |
| 387 | |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 388 | def type_size(string): |
| 389 | try: |
| 390 | return re.match("\d+[KGBM]?", string, re.I).group(0) |
| 391 | except: |
| 392 | msg = "{0!r} don't looks like size-description string".format(string) |
| 393 | raise ValueError(msg) |
| 394 | |
| 395 | |
koder aka kdanilov | 4a72f12 | 2015-02-09 12:25:54 +0200 | [diff] [blame] | 396 | def type_size_ext(string): |
| 397 | if string.startswith("x"): |
| 398 | int(string[1:]) |
| 399 | return string |
| 400 | |
| 401 | if string.startswith("r"): |
| 402 | int(string[1:]) |
| 403 | return string |
| 404 | |
| 405 | try: |
| 406 | return re.match("\d+[KGBM]?", string, re.I).group(0) |
| 407 | except: |
| 408 | msg = "{0!r} don't looks like size-description string".format(string) |
| 409 | raise ValueError(msg) |
| 410 | |
| 411 | |
koder aka kdanilov | 98615bf | 2015-02-02 00:59:07 +0200 | [diff] [blame] | 412 | def ssize_to_kb(ssize): |
| 413 | try: |
| 414 | smap = dict(k=1, K=1, M=1024, m=1024, G=1024**2, g=1024**2) |
| 415 | for ext, coef in smap.items(): |
| 416 | if ssize.endswith(ext): |
| 417 | return int(ssize[:-1]) * coef |
| 418 | |
| 419 | if int(ssize) % 1024 != 0: |
| 420 | raise ValueError() |
| 421 | |
| 422 | return int(ssize) / 1024 |
| 423 | |
| 424 | except (ValueError, TypeError, AttributeError): |
| 425 | tmpl = "Unknow size format {0!r} (or size not multiples 1024)" |
| 426 | raise ValueError(tmpl.format(ssize)) |
| 427 | |
| 428 | |
koder aka kdanilov | 4a72f12 | 2015-02-09 12:25:54 +0200 | [diff] [blame] | 429 | def get_ram_size(): |
| 430 | try: |
| 431 | with open("/proc/meminfo") as fd: |
| 432 | for ln in fd: |
| 433 | if "MemTotal:" in ln: |
| 434 | sz, kb = ln.split(':')[1].strip().split(" ") |
| 435 | assert kb == 'kB' |
| 436 | return int(sz) |
| 437 | except (ValueError, TypeError, AssertionError): |
| 438 | raise |
| 439 | # return None |
| 440 | |
| 441 | |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 442 | def parse_args(argv): |
| 443 | parser = argparse.ArgumentParser( |
koder aka kdanilov | 98615bf | 2015-02-02 00:59:07 +0200 | [diff] [blame] | 444 | description="Run 'iozone' or 'fio' and return result") |
| 445 | parser.add_argument( |
| 446 | "--type", metavar="BINARY_TYPE", |
| 447 | choices=['iozone', 'fio'], required=True) |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 448 | parser.add_argument( |
| 449 | "--iodepth", metavar="IODEPTH", type=int, |
koder aka kdanilov | 4ec0f71 | 2015-02-19 19:19:27 -0800 | [diff] [blame^] | 450 | help="I/O requests queue depths", required=True) |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 451 | parser.add_argument( |
| 452 | '-a', "--action", metavar="ACTION", type=str, |
| 453 | help="actions to run", required=True, |
| 454 | choices=["read", "write", "randread", "randwrite"]) |
| 455 | parser.add_argument( |
| 456 | "--blocksize", metavar="BLOCKSIZE", type=type_size, |
| 457 | help="single operation block size", required=True) |
| 458 | parser.add_argument( |
| 459 | "--timeout", metavar="TIMEOUT", type=int, |
| 460 | help="runtime of a single run", default=None) |
| 461 | parser.add_argument( |
koder aka kdanilov | 4a72f12 | 2015-02-09 12:25:54 +0200 | [diff] [blame] | 462 | "--iosize", metavar="SIZE", type=type_size_ext, |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 463 | help="file size", default=None) |
| 464 | parser.add_argument( |
| 465 | "-s", "--sync", default=False, action="store_true", |
| 466 | help="exec sync after each write") |
| 467 | parser.add_argument( |
| 468 | "-d", "--direct-io", default=False, action="store_true", |
| 469 | help="use O_DIRECT", dest='directio') |
| 470 | parser.add_argument( |
koder aka kdanilov | 4ec0f71 | 2015-02-19 19:19:27 -0800 | [diff] [blame^] | 471 | "-t", "--sync-time", default=None, type=int, metavar="UTC_TIME", |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 472 | help="sleep till sime utc time", dest='sync_time') |
| 473 | parser.add_argument( |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 474 | "--test-file", help="file path to run test on", |
| 475 | default=None, dest='test_file') |
| 476 | parser.add_argument( |
koder aka kdanilov | 4ec0f71 | 2015-02-19 19:19:27 -0800 | [diff] [blame^] | 477 | "--binary-path", help="path to binary, used for testing", |
koder aka kdanilov | 98615bf | 2015-02-02 00:59:07 +0200 | [diff] [blame] | 478 | default=None, dest='binary_path') |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 479 | parser.add_argument( |
| 480 | "--prepare-only", default=False, dest='prepare_only', |
| 481 | action="store_true") |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 482 | parser.add_argument("--concurrency", default=1, type=int) |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 483 | |
| 484 | parser.add_argument("--with-sensors", default="", |
| 485 | dest="with_sensors") |
| 486 | |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 487 | return parser.parse_args(argv) |
| 488 | |
| 489 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 490 | def sensor_thread(sensor_list, cmd_q, data_q): |
| 491 | while True: |
| 492 | try: |
| 493 | cmd_q.get(timeout=0.5) |
| 494 | data_q.put([]) |
| 495 | return |
| 496 | except Queue.Empty: |
| 497 | pass |
| 498 | |
| 499 | |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 500 | def main(argv): |
| 501 | argv_obj = parse_args(argv) |
| 502 | argv_obj.blocksize = ssize_to_kb(argv_obj.blocksize) |
| 503 | |
| 504 | if argv_obj.iosize is not None: |
koder aka kdanilov | 4a72f12 | 2015-02-09 12:25:54 +0200 | [diff] [blame] | 505 | if argv_obj.iosize.startswith('x'): |
| 506 | argv_obj.iosize = argv_obj.blocksize * int(argv_obj.iosize[1:]) |
| 507 | elif argv_obj.iosize.startswith('r'): |
| 508 | rs = get_ram_size() |
| 509 | if rs is None: |
| 510 | sys.stderr.write("Can't determine ram size\n") |
| 511 | exit(1) |
| 512 | argv_obj.iosize = rs * int(argv_obj.iosize[1:]) |
| 513 | else: |
| 514 | argv_obj.iosize = ssize_to_kb(argv_obj.iosize) |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 515 | |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 516 | benchmark = BenchmarkOption(argv_obj.concurrency, |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 517 | argv_obj.iodepth, |
| 518 | argv_obj.action, |
| 519 | argv_obj.blocksize, |
| 520 | argv_obj.iosize) |
| 521 | |
| 522 | benchmark.direct_io = argv_obj.directio |
| 523 | |
koder aka kdanilov | 98615bf | 2015-02-02 00:59:07 +0200 | [diff] [blame] | 524 | if argv_obj.sync: |
| 525 | benchmark.sync = True |
| 526 | |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 527 | test_file_name = argv_obj.test_file |
| 528 | if test_file_name is None: |
| 529 | with warnings.catch_warnings(): |
| 530 | warnings.simplefilter("ignore") |
| 531 | test_file_name = os.tmpnam() |
| 532 | |
koder aka kdanilov | 4ec0f71 | 2015-02-19 19:19:27 -0800 | [diff] [blame^] | 533 | binary_path = locate_binary(argv_obj.type, |
| 534 | argv_obj.binary_path) |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 535 | |
koder aka kdanilov | 98615bf | 2015-02-02 00:59:07 +0200 | [diff] [blame] | 536 | if binary_path is None: |
koder aka kdanilov | 4a72f12 | 2015-02-09 12:25:54 +0200 | [diff] [blame] | 537 | sys.stderr.write("Can't locate binary {0}\n".format(argv_obj.type)) |
koder aka kdanilov | 98615bf | 2015-02-02 00:59:07 +0200 | [diff] [blame] | 538 | return 1 |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 539 | |
| 540 | try: |
| 541 | if argv_obj.sync_time is not None: |
| 542 | dt = argv_obj.sync_time - time.time() |
| 543 | if dt > 0: |
| 544 | time.sleep(dt) |
| 545 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 546 | if argv_obj.with_sensors != "": |
| 547 | oq = Queue.Queue() |
| 548 | iq = Queue.Queue() |
| 549 | argv = (argv_obj.with_sensors, oq, iq) |
| 550 | th = threading.Thread(None, sensor_thread, None, argv) |
| 551 | th.daemon = True |
| 552 | th.start() |
| 553 | |
koder aka kdanilov | 4a72f12 | 2015-02-09 12:25:54 +0200 | [diff] [blame] | 554 | res, cmd = run_benchmark(argv_obj.type, |
| 555 | benchmark, |
| 556 | binary_path, |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 557 | test_file_name, |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 558 | argv_obj.prepare_only, |
| 559 | argv_obj.timeout) |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 560 | if argv_obj.with_sensors != "": |
| 561 | iq.put(None) |
| 562 | stats = oq.get() |
| 563 | else: |
| 564 | stats = None |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 565 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 566 | if not argv_obj.prepare_only: |
| 567 | res['__meta__'] = benchmark.__dict__.copy() |
| 568 | res['__meta__']['cmdline'] = cmd |
| 569 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 570 | if stats is not None: |
| 571 | res['__meta__']['sensor_data'] = stats |
| 572 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 573 | sys.stdout.write(json.dumps(res)) |
| 574 | |
| 575 | if not argv_obj.prepare_only: |
| 576 | sys.stdout.write("\n") |
| 577 | |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 578 | finally: |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 579 | if os.path.isfile(test_file_name) and not argv_obj.prepare_only: |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 580 | os.unlink(test_file_name) |
| 581 | |
| 582 | |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 583 | if __name__ == '__main__': |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 584 | exit(main(sys.argv[1:])) |