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