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 | from io_scenario import io |
| 8 | from ssh_copy_directory import copy_paths |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 9 | from utils import run_over_ssh |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 10 | |
| 11 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 12 | logger = logging.getLogger("io-perf-tool") |
| 13 | |
| 14 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 15 | class IPerfTest(object): |
| 16 | def __init__(self, on_result_cb): |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 17 | self.set_result_cb(on_result_cb) |
| 18 | |
| 19 | def set_result_cb(self, on_result_cb): |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 20 | self.on_result_cb = on_result_cb |
| 21 | |
| 22 | def build(self, conn): |
| 23 | self.pre_run(conn) |
| 24 | |
| 25 | def pre_run(self, conn): |
| 26 | pass |
| 27 | |
| 28 | @abc.abstractmethod |
| 29 | def run(self, conn): |
| 30 | pass |
| 31 | |
| 32 | |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 33 | class TwoScriptTest(IPerfTest): |
| 34 | def __init__(self, opts, testtool, on_result_cb, keep_tmp_files): |
| 35 | super(TwoScriptTest, self).__init__(on_result_cb) |
| 36 | self.opts = opts |
| 37 | self.pre_run_script = None |
| 38 | self.run_script = None |
| 39 | self.tmp_dir = "/tmp/" |
| 40 | self.set_run_script() |
| 41 | self.set_pre_run_script() |
| 42 | |
| 43 | def set_run_script(self): |
| 44 | self.pre_run_script = self.opts.pre_run_script |
| 45 | |
| 46 | def set_pre_run_script(self): |
| 47 | self.run_script = self.opts.run_script |
| 48 | |
| 49 | def get_remote_for_script(self, script): |
| 50 | return os.path.join(self.tmp_dir, script.rpartition('/')[2]) |
| 51 | |
| 52 | def copy_script(self, conn, src): |
| 53 | remote_path = self.get_remote_for_script(src) |
| 54 | copy_paths(conn, {src: remote_path}) |
| 55 | return remote_path |
| 56 | |
| 57 | def pre_run(self, conn): |
| 58 | remote_script = self.copy_script(conn, self.pre_run_script) |
| 59 | cmd = remote_script |
| 60 | code, out, err = run_over_ssh(conn, cmd) |
| 61 | if code != 0: |
| 62 | raise Exception("Pre run failed. %s" % err) |
| 63 | |
| 64 | def run(self, conn): |
| 65 | remote_script = self.copy_script(conn, self.run_script) |
Yulia Portnova | 3f65e7c | 2015-02-25 17:19:35 +0200 | [diff] [blame] | 66 | cmd = remote_script + ' ' + ' '.join(self.opts) |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 67 | code, out, err = run_over_ssh(conn, cmd) |
| 68 | self.on_result(code, out, err, cmd) |
| 69 | |
| 70 | def parse_results(self, out): |
| 71 | for line in out.split("\n"): |
| 72 | key, separator, value = line.partition(":") |
| 73 | if key and value: |
| 74 | self.on_result_cb((key, float(value))) |
| 75 | |
| 76 | def on_result(self, code, out, err, cmd): |
| 77 | if 0 == code: |
| 78 | try: |
| 79 | self.parse_results(out) |
| 80 | except Exception as err: |
| 81 | msg_templ = "Error during postprocessing results: {0!r}" |
| 82 | raise RuntimeError(msg_templ.format(err.message)) |
| 83 | else: |
| 84 | templ = "Command {0!r} failed with code {1}. Error output is:\n{2}" |
| 85 | logger.error(templ.format(cmd, code, err)) |
| 86 | |
| 87 | |
| 88 | class PgBenchTest(TwoScriptTest): |
| 89 | |
| 90 | def set_run_script(self): |
| 91 | self.pre_run_script = "hl_tests/postgres/prepare.sh" |
| 92 | |
| 93 | def set_pre_run_script(self): |
| 94 | self.run_script = "hl_tests/postgres/run.sh" |
| 95 | |
| 96 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 97 | def run_test_iter(obj, conn): |
koder aka kdanilov | 6e2ae79 | 2015-03-04 18:02:24 -0800 | [diff] [blame] | 98 | logger.debug("Run preparation") |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 99 | yield obj.pre_run(conn) |
koder aka kdanilov | 6e2ae79 | 2015-03-04 18:02:24 -0800 | [diff] [blame] | 100 | logger.debug("Run test") |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 101 | res = obj.run(conn) |
| 102 | if isinstance(res, types.GeneratorType): |
| 103 | for vl in res: |
| 104 | yield vl |
| 105 | else: |
| 106 | yield res |
| 107 | |
| 108 | |
| 109 | class IOPerfTest(IPerfTest): |
| 110 | def __init__(self, |
| 111 | script_opts, |
| 112 | testtool_local, |
| 113 | on_result_cb, |
| 114 | keep_tmp_files): |
| 115 | |
| 116 | IPerfTest.__init__(self, on_result_cb) |
koder aka kdanilov | 50f1864 | 2015-02-11 08:54:44 -0800 | [diff] [blame] | 117 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 118 | dst_testtool_path = '/tmp/io_tool' |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 119 | self.script_opts = script_opts + ["--binary-path", dst_testtool_path] |
koder aka kdanilov | 50f1864 | 2015-02-11 08:54:44 -0800 | [diff] [blame] | 120 | io_py_local = os.path.join(os.path.dirname(io.__file__), "io.py") |
| 121 | self.io_py_remote = "/tmp/io.py" |
| 122 | |
| 123 | self.files_to_copy = {testtool_local: dst_testtool_path, |
| 124 | io_py_local: self.io_py_remote} |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 125 | |
| 126 | def pre_run(self, conn): |
koder aka kdanilov | 50f1864 | 2015-02-11 08:54:44 -0800 | [diff] [blame] | 127 | copy_paths(conn, self.files_to_copy) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 128 | |
koder aka kdanilov | 6e2ae79 | 2015-03-04 18:02:24 -0800 | [diff] [blame] | 129 | args = ['env', 'python2', self.io_py_remote] + \ |
| 130 | self.script_opts + ['--prepare-only'] |
| 131 | |
| 132 | code, self.prep_results, err = run_over_ssh(conn, " ".join(args)) |
| 133 | if code != 0: |
| 134 | raise RuntimeError("Preparation failed " + err) |
| 135 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 136 | def run(self, conn): |
koder aka kdanilov | 50f1864 | 2015-02-11 08:54:44 -0800 | [diff] [blame] | 137 | args = ['env', 'python2', self.io_py_remote] + self.script_opts |
koder aka kdanilov | 6e2ae79 | 2015-03-04 18:02:24 -0800 | [diff] [blame] | 138 | args.append('--preparation-results') |
| 139 | args.append("'{0}'".format(self.prep_results)) |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 140 | cmd = " ".join(args) |
| 141 | code, out, err = run_over_ssh(conn, cmd) |
| 142 | self.on_result(code, out, err, cmd) |
koder aka kdanilov | 6e2ae79 | 2015-03-04 18:02:24 -0800 | [diff] [blame] | 143 | args = ['env', 'python2', self.io_py_remote, '--clean', |
| 144 | "'{0}'".format(self.prep_results)] |
| 145 | logger.debug(" ".join(args)) |
| 146 | code, _, err = run_over_ssh(conn, " ".join(args)) |
| 147 | if 0 != code: |
| 148 | logger.error("Cleaning failed: " + err) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 149 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 150 | def on_result(self, code, out, err, cmd): |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 151 | if 0 == code: |
| 152 | try: |
| 153 | for line in out.split("\n"): |
| 154 | if line.strip() != "": |
| 155 | self.on_result_cb(json.loads(line)) |
| 156 | except Exception as err: |
koder aka kdanilov | 0f0546c | 2015-02-17 20:42:05 -0800 | [diff] [blame] | 157 | msg_templ = "Error during postprocessing results: {0!r}" |
| 158 | raise RuntimeError(msg_templ.format(err.message)) |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 159 | else: |
| 160 | templ = "Command {0!r} failed with code {1}. Error output is:\n{2}" |
| 161 | logger.error(templ.format(cmd, code, err)) |