koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 1 | import re |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 2 | import abc |
| 3 | import json |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 4 | import os.path |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 5 | import logging |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 6 | from StringIO import StringIO |
| 7 | from ConfigParser import RawConfigParser |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 8 | |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 9 | from tests import disk_test_agent |
koder aka kdanilov | 3a6633e | 2015-03-26 18:20:00 +0200 | [diff] [blame] | 10 | from ssh_utils import copy_paths |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 11 | from utils import run_over_ssh, ssize_to_b |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 12 | |
| 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 | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 56 | code, out_err = run_over_ssh(conn, cmd) |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 57 | if code != 0: |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 58 | raise Exception("Pre run failed. %s" % out_err) |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 59 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 60 | def run(self, conn, barrier): |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 61 | remote_script = self.copy_script(conn, self.run_script) |
Yulia Portnova | 886a256 | 2015-04-07 11:16:13 +0300 | [diff] [blame^] | 62 | cmd_opts = ' '.join(["%s %s" % (key, val) for key, val |
| 63 | in self.opts.items()]) |
| 64 | cmd = remote_script + ' ' + cmd_opts |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 65 | code, out_err = run_over_ssh(conn, cmd) |
| 66 | self.on_result(code, out_err, cmd) |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 67 | |
| 68 | def parse_results(self, out): |
| 69 | for line in out.split("\n"): |
| 70 | key, separator, value = line.partition(":") |
| 71 | if key and value: |
| 72 | self.on_result_cb((key, float(value))) |
| 73 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 74 | def on_result(self, code, out_err, cmd): |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 75 | if 0 == code: |
| 76 | try: |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 77 | self.parse_results(out_err) |
| 78 | except Exception as exc: |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 79 | msg_templ = "Error during postprocessing results: {0!r}" |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 80 | raise RuntimeError(msg_templ.format(exc.message)) |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 81 | else: |
| 82 | templ = "Command {0!r} failed with code {1}. Error output is:\n{2}" |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 83 | logger.error(templ.format(cmd, code, 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 | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 95 | class IOPerfTest(IPerfTest): |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 96 | io_py_remote = "/tmp/disk_test_agent.py" |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 97 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 98 | def __init__(self, |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 99 | test_options, |
| 100 | on_result_cb): |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 101 | IPerfTest.__init__(self, on_result_cb) |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 102 | self.options = test_options |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 103 | self.config_fname = test_options['cfg'] |
| 104 | self.config_params = test_options.get('params', {}) |
| 105 | self.tool = test_options.get('tool', 'fio') |
| 106 | self.raw_cfg = open(self.config_fname).read() |
koder aka kdanilov | 50f1864 | 2015-02-11 08:54:44 -0800 | [diff] [blame] | 107 | |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 108 | parse_func = disk_test_agent.parse_fio_config_full |
| 109 | self.configs = parse_func(self.raw_cfg, self.config_params) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 110 | |
| 111 | def pre_run(self, conn): |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 112 | |
| 113 | # TODO: install fio, if not installed |
| 114 | run_over_ssh(conn, "apt-get -y install fio") |
| 115 | |
| 116 | local_fname = disk_test_agent.__file__.rsplit('.')[0] + ".py" |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 117 | self.files_to_copy = {local_fname: self.io_py_remote} |
koder aka kdanilov | 50f1864 | 2015-02-11 08:54:44 -0800 | [diff] [blame] | 118 | copy_paths(conn, self.files_to_copy) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 119 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 120 | cmd_templ = "dd if=/dev/zero of={0} bs={1} count={2}" |
| 121 | for secname, params in self.configs: |
| 122 | sz = ssize_to_b(params['size']) |
| 123 | msz = msz = sz / (1024 ** 2) |
| 124 | if sz % (1024 ** 2) != 0: |
| 125 | msz += 1 |
koder aka kdanilov | 6e2ae79 | 2015-03-04 18:02:24 -0800 | [diff] [blame] | 126 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 127 | cmd = cmd_templ.format(params['filename'], 1024 ** 2, msz) |
| 128 | code, out_err = run_over_ssh(conn, cmd) |
| 129 | |
koder aka kdanilov | 6e2ae79 | 2015-03-04 18:02:24 -0800 | [diff] [blame] | 130 | if code != 0: |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 131 | raise RuntimeError("Preparation failed " + out_err) |
koder aka kdanilov | 6e2ae79 | 2015-03-04 18:02:24 -0800 | [diff] [blame] | 132 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 133 | def run(self, conn, barrier): |
| 134 | cmd_templ = "env python2 {0} --type {1} --json -" |
| 135 | cmd = cmd_templ.format(self.io_py_remote, self.tool) |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 136 | logger.debug("Run {0}".format(cmd)) |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 137 | try: |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 138 | barrier.wait() |
| 139 | code, out_err = run_over_ssh(conn, cmd, stdin_data=self.raw_cfg) |
| 140 | self.on_result(code, out_err, cmd) |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 141 | finally: |
| 142 | barrier.exit() |
| 143 | |
| 144 | def on_result(self, code, out_err, cmd): |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 145 | if 0 == code: |
| 146 | try: |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 147 | start_patt = r"(?ims)=+\s+RESULTS\(format=json\)\s+=+" |
| 148 | end_patt = r"(?ims)=+\s+END OF RESULTS\s+=+" |
| 149 | for block in re.split(start_patt, out_err)[1:]: |
| 150 | data, garbage = re.split(end_patt, block) |
| 151 | self.on_result_cb(json.loads(data.strip())) |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 152 | except Exception as exc: |
koder aka kdanilov | 0f0546c | 2015-02-17 20:42:05 -0800 | [diff] [blame] | 153 | msg_templ = "Error during postprocessing results: {0!r}" |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 154 | raise RuntimeError(msg_templ.format(exc.message)) |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 155 | else: |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 156 | templ = "Command {0!r} failed with code {1}. Output is:\n{2}" |
| 157 | logger.error(templ.format(cmd, code, out_err)) |