blob: ac9e1c1ae37d2ee2bacb2aeb5f7c481560dfd19b [file] [log] [blame]
koder aka kdanilov4643fd62015-02-10 16:20:13 -08001import abc
koder aka kdanilovbc2c8982015-06-13 02:50:43 +03002import time
3import logging
koder aka kdanilov4643fd62015-02-10 16:20:13 -08004import os.path
koder aka kdanilov108ac362017-01-19 20:17:16 +02005from typing import Any, List, Optional, Callable, Tuple, Iterable, cast
koder aka kdanilov652cd802015-04-13 12:21:07 +03006
koder aka kdanilovf2865172016-12-30 03:35:11 +02007from concurrent.futures import ThreadPoolExecutor, wait, Future
koder aka kdanilov4643fd62015-02-10 16:20:13 -08008
koder aka kdanilov108ac362017-01-19 20:17:16 +02009from ..utils import StopTestError, get_time_interval_printable_info
koder aka kdanilov70227062016-11-26 23:23:21 +020010from ..node_interfaces import IRPCNode
koder aka kdanilov108ac362017-01-19 20:17:16 +020011from ..result_classes import TestSuiteConfig, TestJobConfig, JobMetrics, TimeSeries, IResultStorage
koder aka kdanilov70227062016-11-26 23:23:21 +020012
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030013
koder aka kdanilovbc2c8982015-06-13 02:50:43 +030014logger = logging.getLogger("wally")
koder aka kdanilov88407ff2015-05-26 15:35:57 +030015
16
koder aka kdanilov70227062016-11-26 23:23:21 +020017__doc__ = "Contains base classes for performance tests"
18
19
koder aka kdanilov7f59d562016-12-26 01:34:23 +020020class PerfTest(metaclass=abc.ABCMeta):
koder aka kdanilov70227062016-11-26 23:23:21 +020021 """Base class for all tests"""
22 name = None # type: str
23 max_retry = 3
24 retry_time = 30
koder aka kdanilovf2865172016-12-30 03:35:11 +020025 job_config_cls = None # type: type
koder aka kdanilov70227062016-11-26 23:23:21 +020026
koder aka kdanilov108ac362017-01-19 20:17:16 +020027 def __init__(self, storage: IResultStorage, suite: TestSuiteConfig, on_idle: Callable[[], None] = None) -> None:
28 self.suite = suite
koder aka kdanilove2de58c2015-04-24 22:59:36 +030029 self.stop_requested = False
koder aka kdanilov108ac362017-01-19 20:17:16 +020030 self.sorted_nodes_ids = sorted(node.node_id for node in self.suite.nodes)
koder aka kdanilov7f59d562016-12-26 01:34:23 +020031 self.on_idle = on_idle
koder aka kdanilovf2865172016-12-30 03:35:11 +020032 self.storage = storage
koder aka kdanilove2de58c2015-04-24 22:59:36 +030033
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030034 def request_stop(self) -> None:
koder aka kdanilove2de58c2015-04-24 22:59:36 +030035 self.stop_requested = True
koder aka kdanilov2066daf2015-04-23 21:05:41 +030036
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030037 def join_remote(self, path: str) -> str:
koder aka kdanilov108ac362017-01-19 20:17:16 +020038 return os.path.join(self.suite.remote_dir, path)
koder aka kdanilov4500a5f2015-04-17 16:55:17 +030039
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030040 @abc.abstractmethod
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020041 def run(self) -> None:
koder aka kdanilov4643fd62015-02-10 16:20:13 -080042 pass
43
koder aka kdanilovbc2c8982015-06-13 02:50:43 +030044 @abc.abstractmethod
koder aka kdanilov39e449e2016-12-17 15:15:26 +020045 def format_for_console(self, data: Any) -> str:
koder aka kdanilovec1b9732015-04-23 20:43:29 +030046 pass
47
koder aka kdanilov4643fd62015-02-10 16:20:13 -080048
koder aka kdanilov70227062016-11-26 23:23:21 +020049class ThreadedTest(PerfTest, metaclass=abc.ABCMeta):
50 """Base class for tests, which spawn separated thread for each node"""
51
52 # max allowed time difference between starts and stops of run of the same test on different test nodes
53 # used_max_diff = max((min_run_time * max_rel_time_diff), max_time_diff)
54 max_time_diff = 5
55 max_rel_time_diff = 0.05
koder aka kdanilovffaf48d2016-12-27 02:25:29 +020056 load_profile_name = None # type: str
koder aka kdanilov70227062016-11-26 23:23:21 +020057
koder aka kdanilov7f59d562016-12-26 01:34:23 +020058 def __init__(self, *args, **kwargs) -> None:
59 PerfTest.__init__(self, *args, **kwargs)
koder aka kdanilov108ac362017-01-19 20:17:16 +020060 self.job_configs = None # type: List[TestJobConfig]
koder aka kdanilovbc2c8982015-06-13 02:50:43 +030061
62 @abc.abstractmethod
koder aka kdanilovf2865172016-12-30 03:35:11 +020063 def get_expected_runtime(self, iter_cfg: TestJobConfig) -> Optional[int]:
koder aka kdanilovbc2c8982015-06-13 02:50:43 +030064 pass
65
koder aka kdanilov108ac362017-01-19 20:17:16 +020066 def get_not_done_jobs(self) -> Iterable[TestJobConfig]:
67 jobs_map = {job.storage_id: job for job in self.job_configs}
68 already_in_storage = set()
69 for db_config in cast(List[TestJobConfig], self.storage.iter_job(self.suite)):
70 if db_config.storage_id in jobs_map:
71 job = jobs_map[db_config.storage_id]
72 if job != db_config:
73 logger.error("Test info at '%s.%s' is not equal to expected config for iteration %s.%s." +
koder aka kdanilovf2865172016-12-30 03:35:11 +020074 " Maybe configuration was changed before test was restarted. " +
75 "DB cfg is:\n %s\nExpected cfg is:\n %s\nFix DB or rerun test from beginning",
koder aka kdanilov108ac362017-01-19 20:17:16 +020076 self.suite.storage_id, job.storage_id, self.name, job.summary,
koder aka kdanilovf2865172016-12-30 03:35:11 +020077 str(db_config).replace("\n", "\n "),
koder aka kdanilov108ac362017-01-19 20:17:16 +020078 str(job).replace("\n", "\n "))
koder aka kdanilovf2865172016-12-30 03:35:11 +020079 raise StopTestError()
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020080
koder aka kdanilov108ac362017-01-19 20:17:16 +020081 logger.info("Test iteration %s.%s found in storage and will be skipped", self.name, job.summary)
82 already_in_storage.add(db_config.storage_id)
83
84 return [job for job in self.job_configs if job.storage_id not in already_in_storage]
koder aka kdanilov70227062016-11-26 23:23:21 +020085
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020086 def run(self) -> None:
koder aka kdanilov108ac362017-01-19 20:17:16 +020087 self.storage.put_or_check_suite(self.suite)
koder aka kdanilovf2865172016-12-30 03:35:11 +020088
koder aka kdanilov108ac362017-01-19 20:17:16 +020089 not_in_storage = list(self.get_not_done_jobs())
koder aka kdanilov70227062016-11-26 23:23:21 +020090 if not not_in_storage:
91 logger.info("All test iteration in storage already. Skip test")
92 return
93
koder aka kdanilovf2865172016-12-30 03:35:11 +020094 logger.debug("Run test %s with profile %r on nodes %s.", self.name,
95 self.load_profile_name,
96 ",".join(self.sorted_nodes_ids))
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +020097 logger.debug("Prepare nodes")
koder aka kdanilov70227062016-11-26 23:23:21 +020098
koder aka kdanilov70227062016-11-26 23:23:21 +020099
koder aka kdanilov108ac362017-01-19 20:17:16 +0200100 with ThreadPoolExecutor(len(self.suite.nodes)) as pool:
koder aka kdanilovf2865172016-12-30 03:35:11 +0200101 # config nodes
koder aka kdanilov108ac362017-01-19 20:17:16 +0200102 list(pool.map(self.config_node, self.suite.nodes))
koder aka kdanilovf2865172016-12-30 03:35:11 +0200103
koder aka kdanilov108ac362017-01-19 20:17:16 +0200104 run_times = list(map(self.get_expected_runtime, not_in_storage))
koder aka kdanilovf2865172016-12-30 03:35:11 +0200105
koder aka kdanilov70227062016-11-26 23:23:21 +0200106 if None not in run_times:
koder aka kdanilovf2865172016-12-30 03:35:11 +0200107 # +5% - is a rough estimation for additional operations
koder aka kdanilov70227062016-11-26 23:23:21 +0200108 expected_run_time = int(sum(run_times) * 1.05)
koder aka kdanilov70227062016-11-26 23:23:21 +0200109
koder aka kdanilovf2865172016-12-30 03:35:11 +0200110 exec_time_s, end_dt_s = get_time_interval_printable_info(expected_run_time)
111 logger.info("Entire test should takes around %s and finished at %s", exec_time_s, end_dt_s)
koder aka kdanilov70227062016-11-26 23:23:21 +0200112
koder aka kdanilov108ac362017-01-19 20:17:16 +0200113 for job in not_in_storage:
114 results = [] # type: List[TimeSeries]
115 for retry_idx in range(self.max_retry):
116 logger.debug("Prepare job %s", job.summary)
koder aka kdanilov70227062016-11-26 23:23:21 +0200117
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +0200118 # prepare nodes for new iterations
koder aka kdanilov108ac362017-01-19 20:17:16 +0200119 wait([pool.submit(self.prepare_iteration, node, job) for node in self.suite.nodes])
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +0200120
koder aka kdanilov108ac362017-01-19 20:17:16 +0200121 expected_job_time = self.get_expected_runtime(job)
koder aka kdanilovf2865172016-12-30 03:35:11 +0200122 exec_time_s, end_dt_s = get_time_interval_printable_info(expected_job_time)
123 logger.info("Job should takes around %s and finished at %s", exec_time_s, end_dt_s)
124
koder aka kdanilov108ac362017-01-19 20:17:16 +0200125 jfutures = [pool.submit(self.run_iteration, node, job) for node in self.suite.nodes]
126 failed = False
127 for future in jfutures:
128 try:
129 results.extend(future.result())
130 except EnvironmentError:
131 failed = True
koder aka kdanilov70227062016-11-26 23:23:21 +0200132
koder aka kdanilov108ac362017-01-19 20:17:16 +0200133 if not failed:
134 break
135
136 if self.max_retry - 1 == retry_idx:
137 logger.exception("Fio failed")
138 raise StopTestError()
139
140 logger.exception("During fio run")
141 logger.info("Sleeping %ss and retrying job", self.retry_time)
142 time.sleep(self.retry_time)
143 results = []
144
145 # per node jobs start and stop times
koder aka kdanilov70227062016-11-26 23:23:21 +0200146 start_times = [] # type: List[int]
147 stop_times = [] # type: List[int]
148
koder aka kdanilov108ac362017-01-19 20:17:16 +0200149 for ts in results:
150 self.storage.put_ts(ts)
151 if len(ts.times) >= 2: # type: ignore
152 start_times.append(ts.times[0])
153 stop_times.append(ts.times[-1])
koder aka kdanilov70227062016-11-26 23:23:21 +0200154
koder aka kdanilovf2865172016-12-30 03:35:11 +0200155 if len(start_times) > 0:
156 min_start_time = min(start_times)
157 max_start_time = max(start_times)
158 min_stop_time = min(stop_times)
koder aka kdanilov70227062016-11-26 23:23:21 +0200159
koder aka kdanilovf2865172016-12-30 03:35:11 +0200160 max_allowed_time_diff = int((min_stop_time - max_start_time) * self.max_rel_time_diff)
161 max_allowed_time_diff = max(max_allowed_time_diff, self.max_time_diff)
koder aka kdanilov70227062016-11-26 23:23:21 +0200162
koder aka kdanilovf2865172016-12-30 03:35:11 +0200163 if min_start_time + self.max_time_diff < max_allowed_time_diff:
164 logger.warning("Too large difference in %s:%s start time - %s. " +
165 "Max recommended difference is %s",
koder aka kdanilov108ac362017-01-19 20:17:16 +0200166 self.name, job.summary,
koder aka kdanilovf2865172016-12-30 03:35:11 +0200167 max_start_time - min_start_time, self.max_time_diff)
koder aka kdanilov70227062016-11-26 23:23:21 +0200168
koder aka kdanilovf2865172016-12-30 03:35:11 +0200169 if min_stop_time + self.max_time_diff < max_allowed_time_diff:
170 logger.warning("Too large difference in %s:%s stop time - %s. " +
171 "Max recommended difference is %s",
koder aka kdanilov108ac362017-01-19 20:17:16 +0200172 self.name, job.summary,
koder aka kdanilovf2865172016-12-30 03:35:11 +0200173 max_start_time - min_start_time, self.max_time_diff)
koder aka kdanilov70227062016-11-26 23:23:21 +0200174
koder aka kdanilov108ac362017-01-19 20:17:16 +0200175 job.reliable_info_starts_at = max_start_time
176 job.reliable_info_stops_at = min_stop_time
177
178 self.storage.put_job(self.suite, job)
koder aka kdanilovf2865172016-12-30 03:35:11 +0200179 self.storage.sync()
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200180
181 if self.on_idle is not None:
182 self.on_idle()
183
koder aka kdanilov70227062016-11-26 23:23:21 +0200184 @abc.abstractmethod
185 def config_node(self, node: IRPCNode) -> None:
186 pass
187
188 @abc.abstractmethod
koder aka kdanilov108ac362017-01-19 20:17:16 +0200189 def prepare_iteration(self, node: IRPCNode, job: TestJobConfig) -> None:
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +0200190 pass
191
192 @abc.abstractmethod
koder aka kdanilov108ac362017-01-19 20:17:16 +0200193 def run_iteration(self, node: IRPCNode, job: TestJobConfig) -> List[TimeSeries]:
koder aka kdanilovbc2c8982015-06-13 02:50:43 +0300194 pass
195
196
koder aka kdanilov70227062016-11-26 23:23:21 +0200197class TwoScriptTest(ThreadedTest, metaclass=abc.ABCMeta):
198 def __init__(self, *dt, **mp) -> None:
koder aka kdanilovbc2c8982015-06-13 02:50:43 +0300199 ThreadedTest.__init__(self, *dt, **mp)
koder aka kdanilov108ac362017-01-19 20:17:16 +0200200 self.prerun_script = self.suite.params['prerun_script']
201 self.run_script = self.suite.params['run_script']
202 self.prerun_tout = self.suite.params.get('prerun_tout', 3600)
203 self.run_tout = self.suite.params.get('run_tout', 3600)
204 # TODO: fix job_configs field
205 raise NotImplementedError("Fix job configs")
Yulia Portnova7ddfa732015-02-24 17:32:58 +0200206
koder aka kdanilov108ac362017-01-19 20:17:16 +0200207 def get_expected_runtime(self, job: TestJobConfig) -> Optional[int]:
koder aka kdanilov70227062016-11-26 23:23:21 +0200208 return None
Yulia Portnova7ddfa732015-02-24 17:32:58 +0200209
koder aka kdanilov70227062016-11-26 23:23:21 +0200210 def config_node(self, node: IRPCNode) -> None:
211 node.copy_file(self.run_script, self.join_remote(self.run_script))
212 node.copy_file(self.prerun_script, self.join_remote(self.prerun_script))
koder aka kdanilovbc2c8982015-06-13 02:50:43 +0300213
koder aka kdanilov70227062016-11-26 23:23:21 +0200214 cmd = self.join_remote(self.prerun_script)
koder aka kdanilov108ac362017-01-19 20:17:16 +0200215 cmd += ' ' + self.suite.params.get('prerun_opts', '')
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +0300216 node.run(cmd, timeout=self.prerun_tout)
Yulia Portnova7ddfa732015-02-24 17:32:58 +0200217
koder aka kdanilov108ac362017-01-19 20:17:16 +0200218 def prepare_iteration(self, node: IRPCNode, job: TestJobConfig) -> None:
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +0200219 pass
220
koder aka kdanilov108ac362017-01-19 20:17:16 +0200221 def run_iteration(self, node: IRPCNode, job: TestJobConfig) -> List[TimeSeries]:
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +0200222 # TODO: have to store logs
koder aka kdanilov70227062016-11-26 23:23:21 +0200223 cmd = self.join_remote(self.run_script)
koder aka kdanilov108ac362017-01-19 20:17:16 +0200224 cmd += ' ' + self.suite.params.get('run_opts', '')
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +0200225 return self.parse_results(node.run(cmd, timeout=self.run_tout))
koder aka kdanilov70227062016-11-26 23:23:21 +0200226
227 @abc.abstractmethod
koder aka kdanilov108ac362017-01-19 20:17:16 +0200228 def parse_results(self, data: str) -> List[TimeSeries]:
koder aka kdanilov70227062016-11-26 23:23:21 +0200229 pass
230