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 | 98615bf | 2015-02-02 00:59:07 +0200 | [diff] [blame] | 300 | return False, None |
| 301 | |
| 302 | return False, binary_path |
| 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 | 4a72f12 | 2015-02-09 12:25:54 +0200 | [diff] [blame] | 357 | return False, which('fio') |
koder aka kdanilov | 98615bf | 2015-02-02 00:59:07 +0200 | [diff] [blame] | 358 | |
| 359 | |
| 360 | # ---------------------------------------------------------------------------- |
| 361 | |
| 362 | |
| 363 | def locate_binary(binary_tp, binary_url, binary_path): |
| 364 | remove_binary = False |
| 365 | |
| 366 | if binary_url is not None: |
| 367 | if binary_path is not None: |
| 368 | sys.stderr.write("At most one option from --binary-path and " |
| 369 | "--binary-url should be provided") |
| 370 | return False, None |
| 371 | |
| 372 | binary_path = os.tmpnam() |
| 373 | install_iozone_static(binary_url, binary_path) |
| 374 | remove_binary = True |
| 375 | |
| 376 | elif binary_path is not None: |
| 377 | if os.path.isfile(binary_path): |
| 378 | if not os.access(binary_path, os.X_OK): |
| 379 | st = os.stat(binary_path) |
| 380 | os.chmod(binary_path, st.st_mode | stat.S_IEXEC) |
| 381 | else: |
| 382 | binary_path = None |
| 383 | |
| 384 | if binary_path is not None: |
| 385 | return remove_binary, binary_path |
| 386 | |
| 387 | if 'iozone' == binary_tp: |
| 388 | return locate_iozone() |
| 389 | else: |
| 390 | return locate_fio() |
| 391 | |
| 392 | |
| 393 | def run_benchmark(binary_tp, *argv, **kwargs): |
| 394 | if 'iozone' == binary_tp: |
| 395 | return run_iozone(*argv, **kwargs) |
| 396 | else: |
| 397 | return run_fio(*argv, **kwargs) |
| 398 | |
| 399 | |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 400 | def type_size(string): |
| 401 | try: |
| 402 | return re.match("\d+[KGBM]?", string, re.I).group(0) |
| 403 | except: |
| 404 | msg = "{0!r} don't looks like size-description string".format(string) |
| 405 | raise ValueError(msg) |
| 406 | |
| 407 | |
koder aka kdanilov | 4a72f12 | 2015-02-09 12:25:54 +0200 | [diff] [blame] | 408 | def type_size_ext(string): |
| 409 | if string.startswith("x"): |
| 410 | int(string[1:]) |
| 411 | return string |
| 412 | |
| 413 | if string.startswith("r"): |
| 414 | int(string[1:]) |
| 415 | return string |
| 416 | |
| 417 | try: |
| 418 | return re.match("\d+[KGBM]?", string, re.I).group(0) |
| 419 | except: |
| 420 | msg = "{0!r} don't looks like size-description string".format(string) |
| 421 | raise ValueError(msg) |
| 422 | |
| 423 | |
koder aka kdanilov | 98615bf | 2015-02-02 00:59:07 +0200 | [diff] [blame] | 424 | def ssize_to_kb(ssize): |
| 425 | try: |
| 426 | smap = dict(k=1, K=1, M=1024, m=1024, G=1024**2, g=1024**2) |
| 427 | for ext, coef in smap.items(): |
| 428 | if ssize.endswith(ext): |
| 429 | return int(ssize[:-1]) * coef |
| 430 | |
| 431 | if int(ssize) % 1024 != 0: |
| 432 | raise ValueError() |
| 433 | |
| 434 | return int(ssize) / 1024 |
| 435 | |
| 436 | except (ValueError, TypeError, AttributeError): |
| 437 | tmpl = "Unknow size format {0!r} (or size not multiples 1024)" |
| 438 | raise ValueError(tmpl.format(ssize)) |
| 439 | |
| 440 | |
koder aka kdanilov | 4a72f12 | 2015-02-09 12:25:54 +0200 | [diff] [blame] | 441 | def get_ram_size(): |
| 442 | try: |
| 443 | with open("/proc/meminfo") as fd: |
| 444 | for ln in fd: |
| 445 | if "MemTotal:" in ln: |
| 446 | sz, kb = ln.split(':')[1].strip().split(" ") |
| 447 | assert kb == 'kB' |
| 448 | return int(sz) |
| 449 | except (ValueError, TypeError, AssertionError): |
| 450 | raise |
| 451 | # return None |
| 452 | |
| 453 | |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 454 | def parse_args(argv): |
| 455 | parser = argparse.ArgumentParser( |
koder aka kdanilov | 98615bf | 2015-02-02 00:59:07 +0200 | [diff] [blame] | 456 | description="Run 'iozone' or 'fio' and return result") |
| 457 | parser.add_argument( |
| 458 | "--type", metavar="BINARY_TYPE", |
| 459 | choices=['iozone', 'fio'], required=True) |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 460 | parser.add_argument( |
| 461 | "--iodepth", metavar="IODEPTH", type=int, |
| 462 | help="I/O depths to test in kb", required=True) |
| 463 | parser.add_argument( |
| 464 | '-a', "--action", metavar="ACTION", type=str, |
| 465 | help="actions to run", required=True, |
| 466 | choices=["read", "write", "randread", "randwrite"]) |
| 467 | parser.add_argument( |
| 468 | "--blocksize", metavar="BLOCKSIZE", type=type_size, |
| 469 | help="single operation block size", required=True) |
| 470 | parser.add_argument( |
| 471 | "--timeout", metavar="TIMEOUT", type=int, |
| 472 | help="runtime of a single run", default=None) |
| 473 | parser.add_argument( |
koder aka kdanilov | 4a72f12 | 2015-02-09 12:25:54 +0200 | [diff] [blame] | 474 | "--iosize", metavar="SIZE", type=type_size_ext, |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 475 | help="file size", default=None) |
| 476 | parser.add_argument( |
| 477 | "-s", "--sync", default=False, action="store_true", |
| 478 | help="exec sync after each write") |
| 479 | parser.add_argument( |
| 480 | "-d", "--direct-io", default=False, action="store_true", |
| 481 | help="use O_DIRECT", dest='directio') |
| 482 | parser.add_argument( |
| 483 | "-t", "--sync-time", default=None, type=int, |
| 484 | help="sleep till sime utc time", dest='sync_time') |
| 485 | parser.add_argument( |
koder aka kdanilov | 98615bf | 2015-02-02 00:59:07 +0200 | [diff] [blame] | 486 | "--binary-url", help="static binary url", |
| 487 | dest="binary_url", default=None) |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 488 | parser.add_argument( |
| 489 | "--test-file", help="file path to run test on", |
| 490 | default=None, dest='test_file') |
| 491 | parser.add_argument( |
koder aka kdanilov | 98615bf | 2015-02-02 00:59:07 +0200 | [diff] [blame] | 492 | "--binary-path", help="binary path", |
| 493 | default=None, dest='binary_path') |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 494 | parser.add_argument( |
| 495 | "--prepare-only", default=False, dest='prepare_only', |
| 496 | action="store_true") |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 497 | parser.add_argument("--concurrency", default=1, type=int) |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 498 | |
| 499 | parser.add_argument("--with-sensors", default="", |
| 500 | dest="with_sensors") |
| 501 | |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 502 | return parser.parse_args(argv) |
| 503 | |
| 504 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 505 | def sensor_thread(sensor_list, cmd_q, data_q): |
| 506 | while True: |
| 507 | try: |
| 508 | cmd_q.get(timeout=0.5) |
| 509 | data_q.put([]) |
| 510 | return |
| 511 | except Queue.Empty: |
| 512 | pass |
| 513 | |
| 514 | |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 515 | def main(argv): |
| 516 | argv_obj = parse_args(argv) |
| 517 | argv_obj.blocksize = ssize_to_kb(argv_obj.blocksize) |
| 518 | |
| 519 | if argv_obj.iosize is not None: |
koder aka kdanilov | 4a72f12 | 2015-02-09 12:25:54 +0200 | [diff] [blame] | 520 | if argv_obj.iosize.startswith('x'): |
| 521 | argv_obj.iosize = argv_obj.blocksize * int(argv_obj.iosize[1:]) |
| 522 | elif argv_obj.iosize.startswith('r'): |
| 523 | rs = get_ram_size() |
| 524 | if rs is None: |
| 525 | sys.stderr.write("Can't determine ram size\n") |
| 526 | exit(1) |
| 527 | argv_obj.iosize = rs * int(argv_obj.iosize[1:]) |
| 528 | else: |
| 529 | argv_obj.iosize = ssize_to_kb(argv_obj.iosize) |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 530 | |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 531 | benchmark = BenchmarkOption(argv_obj.concurrency, |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 532 | argv_obj.iodepth, |
| 533 | argv_obj.action, |
| 534 | argv_obj.blocksize, |
| 535 | argv_obj.iosize) |
| 536 | |
| 537 | benchmark.direct_io = argv_obj.directio |
| 538 | |
koder aka kdanilov | 98615bf | 2015-02-02 00:59:07 +0200 | [diff] [blame] | 539 | if argv_obj.sync: |
| 540 | benchmark.sync = True |
| 541 | |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 542 | test_file_name = argv_obj.test_file |
| 543 | if test_file_name is None: |
| 544 | with warnings.catch_warnings(): |
| 545 | warnings.simplefilter("ignore") |
| 546 | test_file_name = os.tmpnam() |
| 547 | |
koder aka kdanilov | 98615bf | 2015-02-02 00:59:07 +0200 | [diff] [blame] | 548 | remove_binary, binary_path = locate_binary(argv_obj.type, |
| 549 | argv_obj.binary_url, |
| 550 | argv_obj.binary_path) |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 551 | |
koder aka kdanilov | 98615bf | 2015-02-02 00:59:07 +0200 | [diff] [blame] | 552 | if binary_path is None: |
koder aka kdanilov | 4a72f12 | 2015-02-09 12:25:54 +0200 | [diff] [blame] | 553 | 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] | 554 | return 1 |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 555 | |
| 556 | try: |
| 557 | if argv_obj.sync_time is not None: |
| 558 | dt = argv_obj.sync_time - time.time() |
| 559 | if dt > 0: |
| 560 | time.sleep(dt) |
| 561 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 562 | if argv_obj.with_sensors != "": |
| 563 | oq = Queue.Queue() |
| 564 | iq = Queue.Queue() |
| 565 | argv = (argv_obj.with_sensors, oq, iq) |
| 566 | th = threading.Thread(None, sensor_thread, None, argv) |
| 567 | th.daemon = True |
| 568 | th.start() |
| 569 | |
koder aka kdanilov | 4a72f12 | 2015-02-09 12:25:54 +0200 | [diff] [blame] | 570 | res, cmd = run_benchmark(argv_obj.type, |
| 571 | benchmark, |
| 572 | binary_path, |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 573 | test_file_name, |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 574 | argv_obj.prepare_only, |
| 575 | argv_obj.timeout) |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 576 | if argv_obj.with_sensors != "": |
| 577 | iq.put(None) |
| 578 | stats = oq.get() |
| 579 | else: |
| 580 | stats = None |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 581 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 582 | if not argv_obj.prepare_only: |
| 583 | res['__meta__'] = benchmark.__dict__.copy() |
| 584 | res['__meta__']['cmdline'] = cmd |
| 585 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 586 | if stats is not None: |
| 587 | res['__meta__']['sensor_data'] = stats |
| 588 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 589 | sys.stdout.write(json.dumps(res)) |
| 590 | |
| 591 | if not argv_obj.prepare_only: |
| 592 | sys.stdout.write("\n") |
| 593 | |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 594 | finally: |
koder aka kdanilov | 98615bf | 2015-02-02 00:59:07 +0200 | [diff] [blame] | 595 | if remove_binary: |
| 596 | os.unlink(binary_path) |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 597 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 598 | 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] | 599 | os.unlink(test_file_name) |
| 600 | |
| 601 | |
koder aka kdanilov | 4af8085 | 2015-02-01 23:36:38 +0200 | [diff] [blame] | 602 | if __name__ == '__main__': |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 603 | exit(main(sys.argv[1:])) |