koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 1 | import abc |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 2 | import time |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 3 | import os.path |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 4 | import logging |
| 5 | |
koder aka kdanilov | 2e92802 | 2015-04-08 13:47:15 +0300 | [diff] [blame] | 6 | from disk_perf_test_tool.tests import disk_test_agent |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 7 | from disk_perf_test_tool.tests.disk_test_agent import parse_fio_config_full |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 8 | from disk_perf_test_tool.tests.disk_test_agent import estimate_cfg, sec_to_str |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 9 | from disk_perf_test_tool.tests.io_results_loader import parse_output |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame^] | 10 | from disk_perf_test_tool.ssh_utils import copy_paths, run_over_ssh, delete_file |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 11 | from disk_perf_test_tool.utils import ssize_to_b |
| 12 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 13 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 14 | logger = logging.getLogger("io-perf-tool") |
| 15 | |
| 16 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 17 | class IPerfTest(object): |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame^] | 18 | def __init__(self, on_result_cb, log_directory=None, node=None): |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 19 | self.on_result_cb = on_result_cb |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame^] | 20 | self.log_directory = log_directory |
| 21 | self.node = node |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 22 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 23 | def pre_run(self, conn): |
| 24 | pass |
| 25 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame^] | 26 | def cleanup(self, conn): |
| 27 | pass |
| 28 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 29 | @abc.abstractmethod |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 30 | def run(self, conn, barrier): |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 31 | pass |
| 32 | |
| 33 | |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 34 | class TwoScriptTest(IPerfTest): |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame^] | 35 | def __init__(self, opts, on_result_cb, log_directory=None, node=None): |
| 36 | super(TwoScriptTest, self).__init__(on_result_cb, log_directory, |
| 37 | node=node) |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 38 | self.opts = opts |
| 39 | self.pre_run_script = None |
| 40 | self.run_script = None |
| 41 | self.tmp_dir = "/tmp/" |
| 42 | self.set_run_script() |
| 43 | self.set_pre_run_script() |
| 44 | |
| 45 | def set_run_script(self): |
| 46 | self.pre_run_script = self.opts.pre_run_script |
| 47 | |
| 48 | def set_pre_run_script(self): |
| 49 | self.run_script = self.opts.run_script |
| 50 | |
| 51 | def get_remote_for_script(self, script): |
| 52 | return os.path.join(self.tmp_dir, script.rpartition('/')[2]) |
| 53 | |
| 54 | def copy_script(self, conn, src): |
| 55 | remote_path = self.get_remote_for_script(src) |
| 56 | copy_paths(conn, {src: remote_path}) |
| 57 | return remote_path |
| 58 | |
| 59 | def pre_run(self, conn): |
| 60 | remote_script = self.copy_script(conn, self.pre_run_script) |
| 61 | cmd = remote_script |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame^] | 62 | run_over_ssh(conn, cmd, node=self.node) |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 63 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 64 | def run(self, conn, barrier): |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 65 | remote_script = self.copy_script(conn, self.run_script) |
Yulia Portnova | 886a256 | 2015-04-07 11:16:13 +0300 | [diff] [blame] | 66 | cmd_opts = ' '.join(["%s %s" % (key, val) for key, val |
| 67 | in self.opts.items()]) |
| 68 | cmd = remote_script + ' ' + cmd_opts |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame^] | 69 | out_err = run_over_ssh(conn, cmd, node=self.node) |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 70 | self.on_result(out_err, cmd) |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 71 | |
| 72 | def parse_results(self, out): |
| 73 | for line in out.split("\n"): |
| 74 | key, separator, value = line.partition(":") |
| 75 | if key and value: |
| 76 | self.on_result_cb((key, float(value))) |
| 77 | |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 78 | def on_result(self, out_err, cmd): |
| 79 | try: |
| 80 | self.parse_results(out_err) |
| 81 | except Exception as exc: |
| 82 | msg_templ = "Error during postprocessing results: {0!r}. {1}" |
| 83 | raise RuntimeError(msg_templ.format(exc.message, out_err)) |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 84 | |
| 85 | |
| 86 | class PgBenchTest(TwoScriptTest): |
| 87 | |
| 88 | def set_run_script(self): |
Yulia Portnova | 886a256 | 2015-04-07 11:16:13 +0300 | [diff] [blame] | 89 | self.pre_run_script = "tests/postgres/prepare.sh" |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 90 | |
| 91 | def set_pre_run_script(self): |
Yulia Portnova | 886a256 | 2015-04-07 11:16:13 +0300 | [diff] [blame] | 92 | self.run_script = "tests/postgres/run.sh" |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 93 | |
| 94 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame^] | 95 | def open_for_append_or_create(fname): |
| 96 | if not os.path.exists(fname): |
| 97 | return open(fname, "w") |
| 98 | |
| 99 | fd = open(fname, 'r+') |
| 100 | fd.seek(0, os.SEEK_END) |
| 101 | return fd |
| 102 | |
| 103 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 104 | class IOPerfTest(IPerfTest): |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 105 | io_py_remote = "/tmp/disk_test_agent.py" |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 106 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame^] | 107 | def __init__(self, test_options, on_result_cb, |
| 108 | log_directory=None, node=None): |
| 109 | IPerfTest.__init__(self, on_result_cb, log_directory, node=node) |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 110 | self.options = test_options |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 111 | self.config_fname = test_options['cfg'] |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame^] | 112 | self.alive_check_interval = test_options.get('alive_check_interval') |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 113 | self.config_params = test_options.get('params', {}) |
| 114 | self.tool = test_options.get('tool', 'fio') |
| 115 | self.raw_cfg = open(self.config_fname).read() |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 116 | self.configs = list(parse_fio_config_full(self.raw_cfg, |
| 117 | self.config_params)) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 118 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame^] | 119 | cmd_log = os.path.join(self.log_directory, "task_compiled.cfg") |
| 120 | raw_res = os.path.join(self.log_directory, "raw_results.txt") |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 121 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame^] | 122 | fio_command_file = open_for_append_or_create(cmd_log) |
| 123 | fio_command_file.write(disk_test_agent.compile(self.raw_cfg, |
| 124 | self.config_params, |
| 125 | None)) |
| 126 | self.fio_raw_results_file = open_for_append_or_create(raw_res) |
| 127 | |
| 128 | def cleanup(self, conn): |
| 129 | delete_file(conn, self.io_py_remote) |
| 130 | |
| 131 | def pre_run(self, conn): |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 132 | try: |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame^] | 133 | run_over_ssh(conn, 'which fio', node=self.node) |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 134 | except OSError: |
| 135 | # TODO: install fio, if not installed |
| 136 | cmd = "sudo apt-get -y install fio" |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 137 | |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 138 | for i in range(3): |
| 139 | try: |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame^] | 140 | run_over_ssh(conn, cmd, node=self.node) |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 141 | break |
| 142 | except OSError as err: |
| 143 | time.sleep(3) |
| 144 | else: |
| 145 | raise OSError("Can't install fio - " + err.message) |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 146 | |
| 147 | local_fname = disk_test_agent.__file__.rsplit('.')[0] + ".py" |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 148 | self.files_to_copy = {local_fname: self.io_py_remote} |
koder aka kdanilov | 50f1864 | 2015-02-11 08:54:44 -0800 | [diff] [blame] | 149 | copy_paths(conn, self.files_to_copy) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 150 | |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 151 | cmd_templ = "sudo dd if=/dev/zero of={0} bs={1} count={2}" |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 152 | files = {} |
| 153 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 154 | for secname, params in self.configs: |
| 155 | sz = ssize_to_b(params['size']) |
| 156 | msz = msz = sz / (1024 ** 2) |
| 157 | if sz % (1024 ** 2) != 0: |
| 158 | msz += 1 |
koder aka kdanilov | 6e2ae79 | 2015-03-04 18:02:24 -0800 | [diff] [blame] | 159 | |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 160 | fname = params['filename'] |
| 161 | files[fname] = max(files.get(fname, 0), msz) |
| 162 | |
| 163 | for fname, sz in files.items(): |
| 164 | cmd = cmd_templ.format(fname, 1024 ** 2, msz) |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame^] | 165 | run_over_ssh(conn, cmd, timeout=msz, node=self.node) |
koder aka kdanilov | 6e2ae79 | 2015-03-04 18:02:24 -0800 | [diff] [blame] | 166 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 167 | def run(self, conn, barrier): |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 168 | cmd_templ = "sudo env python2 {0} --type {1} {2} --json -" |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 169 | |
| 170 | params = " ".join("{0}={1}".format(k, v) |
| 171 | for k, v in self.config_params.items()) |
| 172 | |
| 173 | if "" != params: |
| 174 | params = "--params " + params |
| 175 | |
| 176 | cmd = cmd_templ.format(self.io_py_remote, self.tool, params) |
| 177 | logger.debug("Waiting on barrier") |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 178 | |
| 179 | exec_time = estimate_cfg(self.raw_cfg, self.config_params) |
| 180 | exec_time_str = sec_to_str(exec_time) |
| 181 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 182 | try: |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 183 | if barrier.wait(): |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame^] | 184 | templ = "Test should takes about {0}. Will wait at most {1}" |
| 185 | timeout = int(exec_time * 1.1 + 300) |
| 186 | logger.info(templ.format(exec_time_str, sec_to_str(timeout))) |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 187 | |
| 188 | out_err = run_over_ssh(conn, cmd, |
| 189 | stdin_data=self.raw_cfg, |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame^] | 190 | timeout=timeout, |
| 191 | node=self.node) |
koder aka kdanilov | 12ae063 | 2015-04-15 01:13:43 +0300 | [diff] [blame] | 192 | logger.info("Done") |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 193 | finally: |
| 194 | barrier.exit() |
| 195 | |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 196 | self.on_result(out_err, cmd) |
| 197 | |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 198 | def on_result(self, out_err, cmd): |
| 199 | try: |
| 200 | for data in parse_output(out_err): |
| 201 | self.on_result_cb(data) |
| 202 | except Exception as exc: |
| 203 | msg_templ = "Error during postprocessing results: {0!r}" |
| 204 | raise RuntimeError(msg_templ.format(exc.message)) |
| 205 | |
| 206 | def merge_results(self, results): |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame^] | 207 | if len(results) == 0: |
| 208 | return None |
| 209 | |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 210 | merged_result = results[0] |
| 211 | merged_data = merged_result['res'] |
| 212 | expected_keys = set(merged_data.keys()) |
koder aka kdanilov | 4e9f3ed | 2015-04-14 11:26:12 +0300 | [diff] [blame] | 213 | mergable_fields = ['bw', 'clat', 'iops', 'lat', 'slat'] |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 214 | |
| 215 | for res in results[1:]: |
| 216 | assert res['__meta__'] == merged_result['__meta__'] |
| 217 | |
| 218 | data = res['res'] |
| 219 | diff = set(data.keys()).symmetric_difference(expected_keys) |
| 220 | |
| 221 | msg = "Difference: {0}".format(",".join(diff)) |
| 222 | assert len(diff) == 0, msg |
| 223 | |
| 224 | for testname, test_data in data.items(): |
| 225 | res_test_data = merged_data[testname] |
| 226 | |
| 227 | diff = set(test_data.keys()).symmetric_difference( |
| 228 | res_test_data.keys()) |
| 229 | |
| 230 | msg = "Difference: {0}".format(",".join(diff)) |
| 231 | assert len(diff) == 0, msg |
| 232 | |
| 233 | for k, v in test_data.items(): |
| 234 | if k in mergable_fields: |
| 235 | res_test_data[k].extend(v) |
| 236 | else: |
| 237 | msg = "{0!r} != {1!r}".format(res_test_data[k], v) |
| 238 | assert res_test_data[k] == v, msg |
| 239 | |
| 240 | return merged_result |