koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 1 | import abc |
| 2 | import json |
| 3 | import types |
| 4 | import os.path |
| 5 | |
| 6 | from io_scenario import io |
| 7 | from ssh_copy_directory import copy_paths |
| 8 | |
| 9 | |
| 10 | class IPerfTest(object): |
| 11 | def __init__(self, on_result_cb): |
| 12 | self.on_result_cb = on_result_cb |
| 13 | |
| 14 | def build(self, conn): |
| 15 | self.pre_run(conn) |
| 16 | |
| 17 | def pre_run(self, conn): |
| 18 | pass |
| 19 | |
| 20 | @abc.abstractmethod |
| 21 | def run(self, conn): |
| 22 | pass |
| 23 | |
| 24 | |
| 25 | def run_test_iter(obj, conn): |
| 26 | yield obj.pre_run(conn) |
| 27 | res = obj.run(conn) |
| 28 | if isinstance(res, types.GeneratorType): |
| 29 | for vl in res: |
| 30 | yield vl |
| 31 | else: |
| 32 | yield res |
| 33 | |
| 34 | |
| 35 | class IOPerfTest(IPerfTest): |
| 36 | def __init__(self, |
| 37 | script_opts, |
| 38 | testtool_local, |
| 39 | on_result_cb, |
| 40 | keep_tmp_files): |
| 41 | |
| 42 | IPerfTest.__init__(self, on_result_cb) |
| 43 | dst_testtool_path = '/tmp/io_tool' |
| 44 | self.files_to_copy = {testtool_local: dst_testtool_path} |
| 45 | self.script_opts = script_opts + ["--binary-path", dst_testtool_path] |
| 46 | |
| 47 | def pre_run(self, conn): |
| 48 | copy_paths(self.files_to_copy) |
| 49 | |
| 50 | def run(self, conn): |
| 51 | io_py = os.path.dirname(io.__file__) |
| 52 | |
| 53 | if io_py.endswith('.pyc'): |
| 54 | io_py = io_py[:-1] |
| 55 | |
| 56 | args = ['env', 'python2', io_py] + self.script_opts |
| 57 | code, out, err = conn.execute(args) |
| 58 | self.on_result(code, out, err) |
| 59 | return code, out, err |
| 60 | |
| 61 | def on_result(self, code, out, err): |
| 62 | if 0 == code: |
| 63 | try: |
| 64 | for line in out.split("\n"): |
| 65 | if line.strip() != "": |
| 66 | self.on_result_cb(json.loads(line)) |
| 67 | except Exception as err: |
| 68 | msg = "Error during postprocessing results: {0!r}".format(err) |
| 69 | raise RuntimeError(msg) |