blob: d4f4f525713d1e157320026edc6f0be85e7dd9d8 [file] [log] [blame]
koder aka kdanilov4643fd62015-02-10 16:20:13 -08001import abc
2import json
3import types
4import os.path
5
6from io_scenario import io
7from ssh_copy_directory import copy_paths
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -08008from utils import run_over_ssh
koder aka kdanilov4643fd62015-02-10 16:20:13 -08009
10
11class IPerfTest(object):
12 def __init__(self, on_result_cb):
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080013 self.set_result_cb(on_result_cb)
14
15 def set_result_cb(self, on_result_cb):
koder aka kdanilov4643fd62015-02-10 16:20:13 -080016 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
29def 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
39class 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 kdanilov50f18642015-02-11 08:54:44 -080047
koder aka kdanilov4643fd62015-02-10 16:20:13 -080048 dst_testtool_path = '/tmp/io_tool'
koder aka kdanilov4643fd62015-02-10 16:20:13 -080049 self.script_opts = script_opts + ["--binary-path", dst_testtool_path]
koder aka kdanilov50f18642015-02-11 08:54:44 -080050 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 kdanilov4643fd62015-02-10 16:20:13 -080055
56 def pre_run(self, conn):
koder aka kdanilov50f18642015-02-11 08:54:44 -080057 copy_paths(conn, self.files_to_copy)
koder aka kdanilov4643fd62015-02-10 16:20:13 -080058
59 def run(self, conn):
koder aka kdanilov50f18642015-02-11 08:54:44 -080060 args = ['env', 'python2', self.io_py_remote] + self.script_opts
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080061 code, out, err = run_over_ssh(conn, " ".join(args))
koder aka kdanilov4643fd62015-02-10 16:20:13 -080062 self.on_result(code, out, err)
koder aka kdanilov4643fd62015-02-10 16:20:13 -080063
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)