koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 1 | import sys |
| 2 | import time |
| 3 | import json |
| 4 | import select |
| 5 | import pprint |
| 6 | import argparse |
| 7 | import subprocess |
| 8 | from StringIO import StringIO |
| 9 | from ConfigParser import RawConfigParser |
| 10 | |
| 11 | |
| 12 | def run_fio(benchmark_config): |
| 13 | cmd = ["fio", "--output-format=json", "-"] |
| 14 | p = subprocess.Popen(cmd, stdin=subprocess.PIPE, |
| 15 | stdout=subprocess.PIPE, |
| 16 | stderr=subprocess.STDOUT) |
| 17 | raw_out, _ = p.communicate(benchmark_config) |
| 18 | job_output = json.loads(raw_out)["jobs"][0] |
| 19 | |
| 20 | if job_output['write']['iops'] != 0: |
| 21 | raw_result = job_output['write'] |
| 22 | else: |
| 23 | raw_result = job_output['read'] |
| 24 | |
| 25 | res = {} |
| 26 | |
| 27 | # 'bw_dev bw_mean bw_max bw_min'.split() |
| 28 | for field in ["bw_mean", "iops"]: |
| 29 | res[field] = raw_result[field] |
| 30 | |
| 31 | res["lat"] = raw_result["lat"]["mean"] |
| 32 | res["clat"] = raw_result["clat"]["mean"] |
| 33 | res["slat"] = raw_result["slat"]["mean"] |
| 34 | res["util"] = json.loads(raw_out)["disk_util"][0] |
| 35 | |
| 36 | res["util"] = dict((str(k), v) for k, v in res["util"].items()) |
| 37 | |
| 38 | return res |
| 39 | |
| 40 | |
| 41 | def run_benchmark(binary_tp, *argv, **kwargs): |
| 42 | if 'fio' == binary_tp: |
| 43 | return run_fio(*argv, **kwargs) |
| 44 | raise ValueError("Unknown behcnmark {0}".format(binary_tp)) |
| 45 | |
| 46 | |
| 47 | def parse_args(argv): |
| 48 | parser = argparse.ArgumentParser( |
| 49 | description="Run fio' and return result") |
| 50 | parser.add_argument( |
| 51 | "--type", metavar="BINARY_TYPE", |
| 52 | choices=['fio'], required=True) |
| 53 | parser.add_argument("--start-at", metavar="START_TIME", type=int) |
| 54 | parser.add_argument("--json", action="store_true", default=False) |
| 55 | parser.add_argument("jobfile") |
| 56 | return parser.parse_args(argv) |
| 57 | |
| 58 | |
| 59 | def main(argv): |
| 60 | argv_obj = parse_args(argv) |
| 61 | if argv_obj.jobfile == '-': |
| 62 | job_cfg = "" |
| 63 | dtime = 10 |
| 64 | while True: |
| 65 | r, w, x = select.select([sys.stdin], [], [], dtime) |
| 66 | if len(r) == 0: |
| 67 | raise IOError("No config provided") |
| 68 | char = sys.stdin.read(1) |
| 69 | if '' == char: |
| 70 | break |
| 71 | job_cfg += char |
| 72 | dtime = 1 |
| 73 | else: |
| 74 | job_cfg = open(argv_obj.jobfile).read() |
| 75 | |
| 76 | rcp = RawConfigParser() |
| 77 | rcp.readfp(StringIO(job_cfg)) |
| 78 | assert len(rcp.sections()) == 1 |
| 79 | |
| 80 | if argv_obj.start_at is not None: |
| 81 | ctime = time.time() |
| 82 | if argv_obj.start_at >= ctime: |
| 83 | time.sleep(ctime - argv_obj.start_at) |
| 84 | |
| 85 | res = run_benchmark(argv_obj.type, job_cfg) |
| 86 | res['__meta__'] = dict(rcp.items(rcp.sections()[0])) |
| 87 | res['__meta__']['raw'] = job_cfg |
| 88 | |
| 89 | if argv_obj.json: |
| 90 | sys.stdout.write(json.dumps(res)) |
| 91 | else: |
| 92 | sys.stdout.write(pprint.pformat(res)) |
| 93 | sys.stdout.write("\n") |
| 94 | return 0 |
| 95 | |
| 96 | if __name__ == '__main__': |
| 97 | exit(main(sys.argv[1:])) |