blob: 975499b31cb5b46d0251087053d4e6a46597470e [file] [log] [blame]
koder aka kdanilov4643fd62015-02-10 16:20:13 -08001import abc
koder aka kdanilov66839a92015-04-11 13:22:31 +03002import time
koder aka kdanilov4643fd62015-02-10 16:20:13 -08003import os.path
koder aka kdanilove21d7472015-02-14 19:02:04 -08004import logging
5
koder aka kdanilov2e928022015-04-08 13:47:15 +03006from disk_perf_test_tool.tests import disk_test_agent
koder aka kdanilov66839a92015-04-11 13:22:31 +03007from disk_perf_test_tool.tests.disk_test_agent import parse_fio_config_full
koder aka kdanilov652cd802015-04-13 12:21:07 +03008from disk_perf_test_tool.tests.disk_test_agent import estimate_cfg, sec_to_str
koder aka kdanilov1c2b5112015-04-10 16:53:51 +03009from disk_perf_test_tool.tests.io_results_loader import parse_output
koder aka kdanilov652cd802015-04-13 12:21:07 +030010from disk_perf_test_tool.ssh_utils import copy_paths, run_over_ssh
11from disk_perf_test_tool.utils import ssize_to_b
12
koder aka kdanilov4643fd62015-02-10 16:20:13 -080013
koder aka kdanilove21d7472015-02-14 19:02:04 -080014logger = logging.getLogger("io-perf-tool")
15
16
koder aka kdanilov4643fd62015-02-10 16:20:13 -080017class IPerfTest(object):
18 def __init__(self, on_result_cb):
19 self.on_result_cb = on_result_cb
20
koder aka kdanilov4643fd62015-02-10 16:20:13 -080021 def pre_run(self, conn):
22 pass
23
24 @abc.abstractmethod
koder aka kdanilov2c473092015-03-29 17:12:13 +030025 def run(self, conn, barrier):
koder aka kdanilov4643fd62015-02-10 16:20:13 -080026 pass
27
28
Yulia Portnova7ddfa732015-02-24 17:32:58 +020029class TwoScriptTest(IPerfTest):
Yulia Portnova886a2562015-04-07 11:16:13 +030030 def __init__(self, opts, on_result_cb):
Yulia Portnova7ddfa732015-02-24 17:32:58 +020031 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 kdanilov66839a92015-04-11 13:22:31 +030056 run_over_ssh(conn, cmd)
Yulia Portnova7ddfa732015-02-24 17:32:58 +020057
koder aka kdanilov2c473092015-03-29 17:12:13 +030058 def run(self, conn, barrier):
Yulia Portnova7ddfa732015-02-24 17:32:58 +020059 remote_script = self.copy_script(conn, self.run_script)
Yulia Portnova886a2562015-04-07 11:16:13 +030060 cmd_opts = ' '.join(["%s %s" % (key, val) for key, val
61 in self.opts.items()])
62 cmd = remote_script + ' ' + cmd_opts
koder aka kdanilov66839a92015-04-11 13:22:31 +030063 out_err = run_over_ssh(conn, cmd)
64 self.on_result(out_err, cmd)
Yulia Portnova7ddfa732015-02-24 17:32:58 +020065
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 kdanilov66839a92015-04-11 13:22:31 +030072 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 Portnova7ddfa732015-02-24 17:32:58 +020078
79
80class PgBenchTest(TwoScriptTest):
81
82 def set_run_script(self):
Yulia Portnova886a2562015-04-07 11:16:13 +030083 self.pre_run_script = "tests/postgres/prepare.sh"
Yulia Portnova7ddfa732015-02-24 17:32:58 +020084
85 def set_pre_run_script(self):
Yulia Portnova886a2562015-04-07 11:16:13 +030086 self.run_script = "tests/postgres/run.sh"
Yulia Portnova7ddfa732015-02-24 17:32:58 +020087
88
koder aka kdanilov4643fd62015-02-10 16:20:13 -080089class IOPerfTest(IPerfTest):
koder aka kdanilovda45e882015-04-06 02:24:42 +030090 io_py_remote = "/tmp/disk_test_agent.py"
koder aka kdanilov2c473092015-03-29 17:12:13 +030091
koder aka kdanilov4643fd62015-02-10 16:20:13 -080092 def __init__(self,
koder aka kdanilov2c473092015-03-29 17:12:13 +030093 test_options,
94 on_result_cb):
koder aka kdanilov4643fd62015-02-10 16:20:13 -080095 IPerfTest.__init__(self, on_result_cb)
koder aka kdanilov2c473092015-03-29 17:12:13 +030096 self.options = test_options
koder aka kdanilovda45e882015-04-06 02:24:42 +030097 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 kdanilov652cd802015-04-13 12:21:07 +0300101 self.configs = list(parse_fio_config_full(self.raw_cfg,
102 self.config_params))
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800103
104 def pre_run(self, conn):
koder aka kdanilovda45e882015-04-06 02:24:42 +0300105
koder aka kdanilov652cd802015-04-13 12:21:07 +0300106 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 kdanilov66839a92015-04-11 13:22:31 +0300111
koder aka kdanilov652cd802015-04-13 12:21:07 +0300112 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 kdanilovda45e882015-04-06 02:24:42 +0300120
121 local_fname = disk_test_agent.__file__.rsplit('.')[0] + ".py"
koder aka kdanilov2c473092015-03-29 17:12:13 +0300122 self.files_to_copy = {local_fname: self.io_py_remote}
koder aka kdanilov50f18642015-02-11 08:54:44 -0800123 copy_paths(conn, self.files_to_copy)
koder aka kdanilov4643fd62015-02-10 16:20:13 -0800124
koder aka kdanilovcee43342015-04-14 22:52:53 +0300125 cmd_templ = "sudo dd if=/dev/zero of={0} bs={1} count={2}"
koder aka kdanilov652cd802015-04-13 12:21:07 +0300126 files = {}
127
koder aka kdanilov2c473092015-03-29 17:12:13 +0300128 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 kdanilov6e2ae792015-03-04 18:02:24 -0800133
koder aka kdanilov652cd802015-04-13 12:21:07 +0300134 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 kdanilov6e2ae792015-03-04 18:02:24 -0800140
koder aka kdanilov2c473092015-03-29 17:12:13 +0300141 def run(self, conn, barrier):
koder aka kdanilovcee43342015-04-14 22:52:53 +0300142 cmd_templ = "sudo env python2 {0} --type {1} {2} --json -"
koder aka kdanilov66839a92015-04-11 13:22:31 +0300143
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 kdanilov652cd802015-04-13 12:21:07 +0300152
153 exec_time = estimate_cfg(self.raw_cfg, self.config_params)
154 exec_time_str = sec_to_str(exec_time)
155
koder aka kdanilov2c473092015-03-29 17:12:13 +0300156 try:
koder aka kdanilov652cd802015-04-13 12:21:07 +0300157 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,
koder aka kdanilovcee43342015-04-14 22:52:53 +0300162 timeout=int(exec_time * 1.1 + 300))
koder aka kdanilov12ae0632015-04-15 01:13:43 +0300163 logger.info("Done")
koder aka kdanilov2c473092015-03-29 17:12:13 +0300164 finally:
165 barrier.exit()
166
koder aka kdanilov652cd802015-04-13 12:21:07 +0300167 self.on_result(out_err, cmd)
168
koder aka kdanilov66839a92015-04-11 13:22:31 +0300169 def on_result(self, out_err, cmd):
170 try:
171 for data in parse_output(out_err):
172 self.on_result_cb(data)
173 except Exception as exc:
174 msg_templ = "Error during postprocessing results: {0!r}"
175 raise RuntimeError(msg_templ.format(exc.message))
176
177 def merge_results(self, results):
178 merged_result = results[0]
179 merged_data = merged_result['res']
180 expected_keys = set(merged_data.keys())
koder aka kdanilov4e9f3ed2015-04-14 11:26:12 +0300181 mergable_fields = ['bw', 'clat', 'iops', 'lat', 'slat']
koder aka kdanilov66839a92015-04-11 13:22:31 +0300182
183 for res in results[1:]:
184 assert res['__meta__'] == merged_result['__meta__']
185
186 data = res['res']
187 diff = set(data.keys()).symmetric_difference(expected_keys)
188
189 msg = "Difference: {0}".format(",".join(diff))
190 assert len(diff) == 0, msg
191
192 for testname, test_data in data.items():
193 res_test_data = merged_data[testname]
194
195 diff = set(test_data.keys()).symmetric_difference(
196 res_test_data.keys())
197
198 msg = "Difference: {0}".format(",".join(diff))
199 assert len(diff) == 0, msg
200
201 for k, v in test_data.items():
202 if k in mergable_fields:
203 res_test_data[k].extend(v)
204 else:
205 msg = "{0!r} != {1!r}".format(res_test_data[k], v)
206 assert res_test_data[k] == v, msg
207
208 return merged_result