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