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 |
koder aka kdanilov | ea22c3d | 2015-04-21 03:42:22 +0300 | [diff] [blame] | 5 | import datetime |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 6 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 7 | from wally.ssh_utils import copy_paths, run_over_ssh, delete_file |
| 8 | from wally.utils import ssize_to_b, open_for_append_or_create, sec_to_str |
| 9 | |
| 10 | from . import postgres |
| 11 | from .io import agent as io_agent |
| 12 | from .io import formatter as io_formatter |
| 13 | from .io.results_loader import parse_output |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 14 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 15 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 16 | logger = logging.getLogger("wally") |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 17 | |
| 18 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 19 | class IPerfTest(object): |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 20 | def __init__(self, on_result_cb, log_directory=None, node=None): |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 21 | self.on_result_cb = on_result_cb |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 22 | self.log_directory = log_directory |
| 23 | self.node = node |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 24 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 25 | def pre_run(self, conn): |
| 26 | pass |
| 27 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 28 | def cleanup(self, conn): |
| 29 | pass |
| 30 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 31 | @abc.abstractmethod |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 32 | def run(self, conn, barrier): |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 33 | pass |
| 34 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 35 | @classmethod |
| 36 | def format_for_console(cls, data): |
| 37 | msg = "{0}.format_for_console".format(cls.__name__) |
| 38 | raise NotImplementedError(msg) |
| 39 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 40 | |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 41 | class TwoScriptTest(IPerfTest): |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 42 | remote_tmp_dir = '/tmp' |
| 43 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 44 | def __init__(self, opts, on_result_cb, log_directory=None, node=None): |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 45 | IPerfTest.__init__(self, on_result_cb, log_directory, node=node) |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 46 | self.opts = opts |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 47 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 48 | if 'run_script' in self.opts: |
| 49 | self.run_script = self.opts['run_script'] |
| 50 | self.prepare_script = self.opts['prepare_script'] |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 51 | |
| 52 | def get_remote_for_script(self, script): |
| 53 | return os.path.join(self.tmp_dir, script.rpartition('/')[2]) |
| 54 | |
| 55 | def copy_script(self, conn, src): |
| 56 | remote_path = self.get_remote_for_script(src) |
| 57 | copy_paths(conn, {src: remote_path}) |
| 58 | return remote_path |
| 59 | |
| 60 | def pre_run(self, conn): |
| 61 | remote_script = self.copy_script(conn, self.pre_run_script) |
| 62 | cmd = remote_script |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 63 | run_over_ssh(conn, cmd, node=self.node) |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 64 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 65 | def run(self, conn, barrier): |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 66 | remote_script = self.copy_script(conn, self.run_script) |
Yulia Portnova | 886a256 | 2015-04-07 11:16:13 +0300 | [diff] [blame] | 67 | cmd_opts = ' '.join(["%s %s" % (key, val) for key, val |
| 68 | in self.opts.items()]) |
| 69 | cmd = remote_script + ' ' + cmd_opts |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 70 | out_err = run_over_ssh(conn, cmd, node=self.node) |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 71 | self.on_result(out_err, cmd) |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 72 | |
| 73 | def parse_results(self, out): |
| 74 | for line in out.split("\n"): |
| 75 | key, separator, value = line.partition(":") |
| 76 | if key and value: |
| 77 | self.on_result_cb((key, float(value))) |
| 78 | |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 79 | def on_result(self, out_err, cmd): |
| 80 | try: |
| 81 | self.parse_results(out_err) |
| 82 | except Exception as exc: |
| 83 | msg_templ = "Error during postprocessing results: {0!r}. {1}" |
| 84 | raise RuntimeError(msg_templ.format(exc.message, out_err)) |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 85 | |
| 86 | |
| 87 | class PgBenchTest(TwoScriptTest): |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 88 | root = os.path.dirname(postgres.__file__) |
| 89 | prepare_script = os.path.join(root, "prepare.sh") |
| 90 | run_script = os.path.join(root, "run.sh") |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 91 | |
| 92 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 93 | class IOPerfTest(IPerfTest): |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 94 | io_py_remote = "/tmp/disk_test_agent.py" |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 95 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 96 | def __init__(self, test_options, on_result_cb, |
| 97 | log_directory=None, node=None): |
| 98 | IPerfTest.__init__(self, on_result_cb, log_directory, node=node) |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 99 | self.options = test_options |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 100 | self.config_fname = test_options['cfg'] |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 101 | self.alive_check_interval = test_options.get('alive_check_interval') |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 102 | self.config_params = test_options.get('params', {}) |
| 103 | self.tool = test_options.get('tool', 'fio') |
| 104 | self.raw_cfg = open(self.config_fname).read() |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 105 | self.configs = list(io_agent.parse_all_in_1(self.raw_cfg, |
| 106 | self.config_params)) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 107 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 108 | cmd_log = os.path.join(self.log_directory, "task_compiled.cfg") |
| 109 | raw_res = os.path.join(self.log_directory, "raw_results.txt") |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 110 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 111 | fio_command_file = open_for_append_or_create(cmd_log) |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 112 | |
| 113 | cfg_s_it = io_agent.compile_all_in_1(self.raw_cfg, self.config_params) |
| 114 | splitter = "\n\n" + "-" * 60 + "\n\n" |
| 115 | fio_command_file.write(splitter.join(cfg_s_it)) |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 116 | self.fio_raw_results_file = open_for_append_or_create(raw_res) |
| 117 | |
| 118 | def cleanup(self, conn): |
| 119 | delete_file(conn, self.io_py_remote) |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 120 | # Need to remove tempo files, used for testing |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 121 | |
| 122 | def pre_run(self, conn): |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 123 | try: |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 124 | run_over_ssh(conn, 'which fio', node=self.node) |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 125 | except OSError: |
| 126 | # TODO: install fio, if not installed |
| 127 | cmd = "sudo apt-get -y install fio" |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 128 | |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 129 | for i in range(3): |
| 130 | try: |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 131 | run_over_ssh(conn, cmd, node=self.node) |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 132 | break |
| 133 | except OSError as err: |
| 134 | time.sleep(3) |
| 135 | else: |
| 136 | raise OSError("Can't install fio - " + err.message) |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 137 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 138 | local_fname = io_agent.__file__.rsplit('.')[0] + ".py" |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 139 | self.files_to_copy = {local_fname: self.io_py_remote} |
koder aka kdanilov | 50f1864 | 2015-02-11 08:54:44 -0800 | [diff] [blame] | 140 | copy_paths(conn, self.files_to_copy) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 141 | |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 142 | if self.options.get('prefill_files', True): |
| 143 | files = {} |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 144 | |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 145 | for section in self.configs: |
| 146 | sz = ssize_to_b(section.vals['size']) |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 147 | msz = sz / (1024 ** 2) |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 148 | |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 149 | if sz % (1024 ** 2) != 0: |
| 150 | msz += 1 |
koder aka kdanilov | 6e2ae79 | 2015-03-04 18:02:24 -0800 | [diff] [blame] | 151 | |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 152 | fname = section.vals['filename'] |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 153 | |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 154 | # if already has other test with the same file name |
| 155 | # take largest size |
| 156 | files[fname] = max(files.get(fname, 0), msz) |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 157 | |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 158 | # logger.warning("dd run DISABLED") |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 159 | cmd_templ = "dd if=/dev/zero of={0} bs={1} count={2}" |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 160 | |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 161 | # cmd_templ = "sudo dd if=/dev/zero of={0} bs={1} count={2}" |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 162 | ssize = 0 |
| 163 | stime = time.time() |
| 164 | |
| 165 | for fname, curr_sz in files.items(): |
| 166 | cmd = cmd_templ.format(fname, 1024 ** 2, curr_sz) |
| 167 | ssize += curr_sz |
| 168 | run_over_ssh(conn, cmd, timeout=curr_sz, node=self.node) |
| 169 | |
| 170 | ddtime = time.time() - stime |
| 171 | if ddtime > 1E-3: |
| 172 | fill_bw = int(ssize / ddtime) |
| 173 | mess = "Initiall dd fill bw is {0} MiBps for this vm" |
| 174 | logger.info(mess.format(fill_bw)) |
| 175 | else: |
| 176 | logger.warning("Test files prefill disabled") |
koder aka kdanilov | 6e2ae79 | 2015-03-04 18:02:24 -0800 | [diff] [blame] | 177 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 178 | def run(self, conn, barrier): |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 179 | # logger.warning("No tests runned") |
| 180 | # return |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 181 | # cmd_templ = "sudo env python2 {0} --type {1} {2} --json -" |
| 182 | cmd_templ = "env python2 {0} --type {1} {2} --json -" |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 183 | |
| 184 | params = " ".join("{0}={1}".format(k, v) |
| 185 | for k, v in self.config_params.items()) |
| 186 | |
| 187 | if "" != params: |
| 188 | params = "--params " + params |
| 189 | |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 190 | cmd = cmd_templ.format(self.io_py_remote, self.tool, params) |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 191 | logger.debug("Waiting on barrier") |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 192 | |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 193 | exec_time = io_agent.calculate_execution_time(self.configs) |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 194 | exec_time_str = sec_to_str(exec_time) |
| 195 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 196 | try: |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 197 | timeout = int(exec_time * 1.2 + 300) |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 198 | if barrier.wait(): |
koder aka kdanilov | ea22c3d | 2015-04-21 03:42:22 +0300 | [diff] [blame] | 199 | templ = "Test should takes about {0}." + \ |
| 200 | " Should finish at {1}," + \ |
| 201 | " will wait at most till {2}" |
| 202 | now_dt = datetime.datetime.now() |
| 203 | end_dt = now_dt + datetime.timedelta(0, exec_time) |
| 204 | wait_till = now_dt + datetime.timedelta(0, timeout) |
| 205 | |
| 206 | logger.info(templ.format(exec_time_str, |
| 207 | end_dt.strftime("%H:%M:%S"), |
| 208 | wait_till.strftime("%H:%M:%S"))) |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 209 | |
| 210 | out_err = run_over_ssh(conn, cmd, |
| 211 | stdin_data=self.raw_cfg, |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 212 | timeout=timeout, |
| 213 | node=self.node) |
koder aka kdanilov | 12ae063 | 2015-04-15 01:13:43 +0300 | [diff] [blame] | 214 | logger.info("Done") |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 215 | finally: |
| 216 | barrier.exit() |
| 217 | |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 218 | self.on_result(out_err, cmd) |
| 219 | |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 220 | def on_result(self, out_err, cmd): |
| 221 | try: |
| 222 | for data in parse_output(out_err): |
| 223 | self.on_result_cb(data) |
| 224 | except Exception as exc: |
| 225 | msg_templ = "Error during postprocessing results: {0!r}" |
| 226 | raise RuntimeError(msg_templ.format(exc.message)) |
| 227 | |
| 228 | def merge_results(self, results): |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 229 | if len(results) == 0: |
| 230 | return None |
| 231 | |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 232 | merged_result = results[0] |
| 233 | merged_data = merged_result['res'] |
| 234 | expected_keys = set(merged_data.keys()) |
koder aka kdanilov | 4e9f3ed | 2015-04-14 11:26:12 +0300 | [diff] [blame] | 235 | mergable_fields = ['bw', 'clat', 'iops', 'lat', 'slat'] |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 236 | |
| 237 | for res in results[1:]: |
| 238 | assert res['__meta__'] == merged_result['__meta__'] |
| 239 | |
| 240 | data = res['res'] |
| 241 | diff = set(data.keys()).symmetric_difference(expected_keys) |
| 242 | |
| 243 | msg = "Difference: {0}".format(",".join(diff)) |
| 244 | assert len(diff) == 0, msg |
| 245 | |
| 246 | for testname, test_data in data.items(): |
| 247 | res_test_data = merged_data[testname] |
| 248 | |
| 249 | diff = set(test_data.keys()).symmetric_difference( |
| 250 | res_test_data.keys()) |
| 251 | |
| 252 | msg = "Difference: {0}".format(",".join(diff)) |
| 253 | assert len(diff) == 0, msg |
| 254 | |
| 255 | for k, v in test_data.items(): |
| 256 | if k in mergable_fields: |
| 257 | res_test_data[k].extend(v) |
| 258 | else: |
| 259 | msg = "{0!r} != {1!r}".format(res_test_data[k], v) |
| 260 | assert res_test_data[k] == v, msg |
| 261 | |
| 262 | return merged_result |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 263 | |
| 264 | @classmethod |
| 265 | def format_for_console(cls, data): |
| 266 | return io_formatter.format_results_for_console(data) |