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