blob: 178acce0987c40a68cdc5b7ae3babb2d17d153d6 [file] [log] [blame]
koder aka kdanilov4643fd62015-02-10 16:20:13 -08001import abc
2import json
3import types
4import os.path
koder aka kdanilove21d7472015-02-14 19:02:04 -08005import logging
6
koder aka kdanilov4643fd62015-02-10 16:20:13 -08007
8from io_scenario import io
9from ssh_copy_directory import copy_paths
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080010from utils import run_over_ssh
koder aka kdanilov4643fd62015-02-10 16:20:13 -080011
12
koder aka kdanilove21d7472015-02-14 19:02:04 -080013logger = logging.getLogger("io-perf-tool")
14
15
koder aka kdanilov4643fd62015-02-10 16:20:13 -080016class IPerfTest(object):
17 def __init__(self, on_result_cb):
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080018 self.set_result_cb(on_result_cb)
19
20 def set_result_cb(self, on_result_cb):
koder aka kdanilov4643fd62015-02-10 16:20:13 -080021 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
34def 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
44class 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 kdanilov50f18642015-02-11 08:54:44 -080052
koder aka kdanilov4643fd62015-02-10 16:20:13 -080053 dst_testtool_path = '/tmp/io_tool'
koder aka kdanilov4643fd62015-02-10 16:20:13 -080054 self.script_opts = script_opts + ["--binary-path", dst_testtool_path]
koder aka kdanilov50f18642015-02-11 08:54:44 -080055 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 kdanilov4643fd62015-02-10 16:20:13 -080060
61 def pre_run(self, conn):
koder aka kdanilov50f18642015-02-11 08:54:44 -080062 copy_paths(conn, self.files_to_copy)
koder aka kdanilov4643fd62015-02-10 16:20:13 -080063
64 def run(self, conn):
koder aka kdanilov50f18642015-02-11 08:54:44 -080065 args = ['env', 'python2', self.io_py_remote] + self.script_opts
koder aka kdanilove21d7472015-02-14 19:02:04 -080066 cmd = " ".join(args)
67 code, out, err = run_over_ssh(conn, cmd)
68 self.on_result(code, out, err, cmd)
koder aka kdanilov4643fd62015-02-10 16:20:13 -080069
koder aka kdanilove21d7472015-02-14 19:02:04 -080070 def on_result(self, code, out, err, cmd):
koder aka kdanilov4643fd62015-02-10 16:20:13 -080071 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 kdanilove21d7472015-02-14 19:02:04 -080079 else:
80 templ = "Command {0!r} failed with code {1}. Error output is:\n{2}"
81 logger.error(templ.format(cmd, code, err))