Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 1 | import csv |
| 2 | import os |
| 3 | import json |
| 4 | |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 5 | from datetime import datetime, timedelta, timezone |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 6 | |
Alex | dcb792f | 2021-10-04 14:24:21 -0500 | [diff] [blame] | 7 | from cfg_checker.common import logger_cli |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 8 | from cfg_checker.common.decorators import retry |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 9 | from cfg_checker.common.file_utils import write_str_to_file |
Alex | bfa947c | 2021-11-11 18:14:28 -0600 | [diff] [blame] | 10 | from cfg_checker.helpers.console_utils import Progress |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 11 | from cfg_checker.reports import reporter |
Alex | dcb792f | 2021-10-04 14:24:21 -0500 | [diff] [blame] | 12 | # from cfg_checker.common.exception import InvalidReturnException |
| 13 | # from cfg_checker.common.exception import ConfigException |
| 14 | # from cfg_checker.common.exception import KubeException |
| 15 | |
| 16 | from cfg_checker.nodes import KubeNodes |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 17 | from cfg_checker.agent.fio_runner import _get_seconds, _datetime_fmt |
Alex | dcb792f | 2021-10-04 14:24:21 -0500 | [diff] [blame] | 18 | |
| 19 | |
Alex | 90ac153 | 2021-12-09 11:13:14 -0600 | [diff] [blame^] | 20 | _file_datetime_fmt = "%m%d%Y%H%M%S%z" |
| 21 | |
| 22 | |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 23 | def _reformat_timestr(_str, _chars=["/", ",", " ", ":", "+"], _tchar=""): |
| 24 | _new = "" |
| 25 | for _c in _str: |
| 26 | _new += _c if _c not in _chars else _tchar |
| 27 | return _new |
| 28 | |
| 29 | |
| 30 | def _parse_json_output(buffer): |
| 31 | try: |
| 32 | return json.loads(buffer) |
| 33 | except TypeError as e: |
| 34 | logger_cli.error( |
| 35 | "ERROR: Status not decoded: {}\n{}".format(e, buffer) |
| 36 | ) |
| 37 | except json.decoder.JSONDecodeError as e: |
| 38 | logger_cli.error( |
| 39 | "ERROR: Status not decoded: {}\n{}".format(e, buffer) |
| 40 | ) |
| 41 | return {} |
| 42 | |
| 43 | |
Alex | dcb792f | 2021-10-04 14:24:21 -0500 | [diff] [blame] | 44 | class CephBench(object): |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 45 | _agent_template = "cfgagent-template.yaml" |
| 46 | |
| 47 | def __init__(self, config): |
Alex | dcb792f | 2021-10-04 14:24:21 -0500 | [diff] [blame] | 48 | self.env_config = config |
| 49 | return |
| 50 | |
Alex | dcb792f | 2021-10-04 14:24:21 -0500 | [diff] [blame] | 51 | |
| 52 | class SaltCephBench(CephBench): |
| 53 | def __init__( |
| 54 | self, |
| 55 | config |
| 56 | ): |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 57 | logger_cli.error("ERROR: Not impelented for Salt environment!") |
Alex | dcb792f | 2021-10-04 14:24:21 -0500 | [diff] [blame] | 58 | |
| 59 | # self.master = SaltNodes(config) |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 60 | super(SaltCephBench, self).__init__(config) |
Alex | dcb792f | 2021-10-04 14:24:21 -0500 | [diff] [blame] | 61 | return |
| 62 | |
| 63 | |
| 64 | class KubeCephBench(CephBench): |
| 65 | def __init__(self, config): |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 66 | self.agent_count = config.bench_agent_count |
Alex | dcb792f | 2021-10-04 14:24:21 -0500 | [diff] [blame] | 67 | self.master = KubeNodes(config) |
| 68 | super(KubeCephBench, self).__init__(config) |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 69 | |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 70 | self.mode = config.bench_mode |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 71 | self.resource_prefix = config.resource_prefix |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 72 | if config.bench_mode == "tasks": |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 73 | self.taskfile = config.bench_task_file |
| 74 | self.load_tasks(self.taskfile) |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 75 | elif config.bench_mode == "cleanup": |
| 76 | self.cleanup_list = [] |
| 77 | return |
| 78 | |
| 79 | self.storage_class = config.bench_storage_class |
| 80 | self.results_dump_path = config.bench_results_dump_path |
Alex | 90ac153 | 2021-12-09 11:13:14 -0600 | [diff] [blame^] | 81 | self.bench_name = config.bench_name |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 82 | self.agent_pods = [] |
| 83 | self.services = [] |
| 84 | # By default, |
| 85 | # 30 seconds should be enough to send tasks to 3-5 agents |
| 86 | self.scheduled_delay = 30 |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 87 | |
Alex | 2a7657c | 2021-11-10 20:51:34 -0600 | [diff] [blame] | 88 | self.cleanup_list = [] |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 89 | self.results = {} |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 90 | self.agent_results = {} |
| 91 | |
| 92 | def set_ceph_info_class(self, ceph_info): |
| 93 | self.ceph_info = ceph_info |
Alex | 2a7657c | 2021-11-10 20:51:34 -0600 | [diff] [blame] | 94 | |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 95 | def load_tasks(self, taskfile): |
| 96 | # Load csv file |
| 97 | logger_cli.info("-> Loading taskfile '{}'".format(taskfile)) |
| 98 | self.tasks = [] |
| 99 | with open(taskfile) as f: |
| 100 | _reader = csv.reader(f, delimiter=',') |
| 101 | # load packages |
| 102 | for row in _reader: |
| 103 | self.tasks.append({ |
| 104 | "readwrite": row[0], |
| 105 | "rwmixread": row[1], |
| 106 | "bs": row[2], |
| 107 | "iodepth": row[3], |
| 108 | "size": row[4] |
| 109 | }) |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 110 | logger_cli.info("-> Loaded {} tasks".format(len(self.tasks))) |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 111 | |
Alex | 2a7657c | 2021-11-10 20:51:34 -0600 | [diff] [blame] | 112 | def add_for_deletion(self, obj, typ): |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 113 | self.cleanup_list.append( |
| 114 | [ |
| 115 | typ, |
| 116 | obj.metadata.namespace, |
| 117 | obj.metadata.name |
| 118 | ] |
| 119 | ) |
| 120 | return |
| 121 | |
| 122 | def prepare_cleanup(self): |
| 123 | # Assume number of resources not given |
| 124 | # list all svc, pod, pvc, pv and identify 'cfgagent-xx ones |
| 125 | _types = ["pv", "pvc", "pod", "svc"] |
| 126 | _prefix = self.resource_prefix |
| 127 | for _typ in _types: |
| 128 | _list = self.master.list_resource_names_by_type_and_ns(_typ) |
| 129 | for ns, name in _list: |
| 130 | if name.startswith(_prefix): |
| 131 | if ns: |
| 132 | _msg = "{} {}/{}".format(_typ, ns, name) |
| 133 | else: |
| 134 | _msg = "{} {}".format(_typ, name) |
| 135 | logger_cli.info("-> Found {}".format(_msg)) |
| 136 | self.cleanup_list.append([_typ, ns, name]) |
Alex | 2a7657c | 2021-11-10 20:51:34 -0600 | [diff] [blame] | 137 | return |
| 138 | |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 139 | def prepare_agents(self, options): |
| 140 | logger_cli.info("# Preparing {} agents".format(self.agent_count)) |
| 141 | for idx in range(self.agent_count): |
| 142 | # create pvc/pv and pod |
| 143 | logger_cli.info("-> creating agent '{:02}'".format(idx)) |
Alex | 90ac153 | 2021-12-09 11:13:14 -0600 | [diff] [blame^] | 144 | # _agent, _pv, _pvc = self.master.prepare_benchmark_agent( |
| 145 | _agent, _pvc = self.master.prepare_benchmark_agent( |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 146 | idx, |
| 147 | os.path.split(options["filename"])[0], |
| 148 | self.storage_class, |
| 149 | options['size'] + 'i', |
| 150 | self._agent_template |
| 151 | ) |
Alex | 2a7657c | 2021-11-10 20:51:34 -0600 | [diff] [blame] | 152 | # save it to lists |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 153 | self.agent_pods.append(_agent) |
Alex | 90ac153 | 2021-12-09 11:13:14 -0600 | [diff] [blame^] | 154 | # self.add_for_deletion(_pv, "pv") |
Alex | 2a7657c | 2021-11-10 20:51:34 -0600 | [diff] [blame] | 155 | self.add_for_deletion(_pvc, "pvc") |
| 156 | self.add_for_deletion(_agent, "pod") |
| 157 | |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 158 | # expose it |
| 159 | _svc = self.master.expose_benchmark_agent(_agent) |
Alex | 2a7657c | 2021-11-10 20:51:34 -0600 | [diff] [blame] | 160 | self.add_for_deletion(_svc, "svc") |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 161 | # Save service |
| 162 | self.services.append(_svc) |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 163 | # prepopulate results |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 164 | self.agent_results[_agent.metadata.name] = {} |
| 165 | self.agent_results[_agent.metadata.name]["url"] = \ |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 166 | "http://{}:{}/api/".format( |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 167 | _svc.spec.cluster_ip, |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 168 | 8765 |
| 169 | ) |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 170 | self.agent_results[_agent.metadata.name]["storage_class"] = \ |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 171 | self.storage_class |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 172 | self.agent_results[_agent.metadata.name]["volume_size"] = \ |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 173 | options['size'] |
| 174 | |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 175 | logger_cli.info("-> Done creating agents") |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 176 | # TODO: Update after implementing pooled task sending |
Alex | 90ac153 | 2021-12-09 11:13:14 -0600 | [diff] [blame^] | 177 | self.scheduled_delay = self.agent_count * 10 |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 178 | logger_cli.info( |
| 179 | "-> Schedule delay set to {} sec".format(self.scheduled_delay) |
| 180 | ) |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 181 | return |
| 182 | |
| 183 | def _poke_agent(self, url, body, action="GET"): |
| 184 | _datafile = "/tmp/data" |
| 185 | _data = [ |
Alex | bfa947c | 2021-11-11 18:14:28 -0600 | [diff] [blame] | 186 | "-d", |
| 187 | "@" + _datafile |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 188 | ] |
| 189 | _cmd = [ |
| 190 | "curl", |
| 191 | "-s", |
| 192 | "-H", |
| 193 | "'Content-Type: application/json'", |
| 194 | "-X", |
| 195 | action, |
| 196 | url |
| 197 | ] |
| 198 | if body: |
| 199 | _cmd += _data |
| 200 | _ret = self.master.prepare_json_in_pod( |
| 201 | self.agent_pods[0].metadata.name, |
| 202 | self.master._namespace, |
| 203 | body, |
| 204 | _datafile |
| 205 | ) |
| 206 | _ret = self.master.exec_cmd_on_target_pod( |
| 207 | self.agent_pods[0].metadata.name, |
| 208 | self.master._namespace, |
| 209 | " ".join(_cmd) |
| 210 | ) |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 211 | return _parse_json_output(_ret) |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 212 | |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 213 | def _ensure_agents_ready(self): |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 214 | # make sure agents idle |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 215 | _status_set = [] |
| 216 | _ready_set = [] |
| 217 | for _agent, _d in self.get_agents_status().items(): |
| 218 | # obviously, there should be some answer |
| 219 | if _d is None: |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 220 | logger_cli.error("ERROR: Agent status not available") |
| 221 | return False |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 222 | # status should be idle or finished |
| 223 | if _d['status'] not in ["idle", "finished"]: |
| 224 | logger_cli.error( |
| 225 | "Agent status invalid {}:{}".format(_agent, _d['status']) |
| 226 | ) |
| 227 | _status_set += [False] |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 228 | else: |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 229 | # Good agent |
| 230 | _status_set += [True] |
| 231 | # agent's fio shell should be in 'ready' |
| 232 | if not _d["healthcheck"]["ready"]: |
| 233 | logger_cli.error("Agent is not ready {}".format(_agent)) |
| 234 | _ready_set += [False] |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 235 | else: |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 236 | # 'fio' shell for agent is ready |
| 237 | _ready_set += [True] |
| 238 | # all agent's statuses should be True |
| 239 | # and all 'fio' shell modules should be 'ready' |
| 240 | if not any(_status_set) or not any(_ready_set): |
| 241 | # At least one is not ready and it was already logged above |
| 242 | return False |
| 243 | else: |
| 244 | # All is good |
| 245 | return True |
| 246 | |
| 247 | def get_agents_status(self): |
| 248 | _status = {} |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 249 | _results = self.master.exec_on_labeled_pods_and_ns( |
| 250 | "app=cfgagent", |
| 251 | "curl -s http://localhost:8765/api/fio" |
| 252 | ) |
| 253 | for _agent, _result in _results.items(): |
| 254 | _j = _parse_json_output(_result) |
| 255 | _status[_agent] = _j |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 256 | return _status |
| 257 | |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 258 | @retry(Exception, initial_wait=5) |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 259 | def get_agents_resultlist(self): |
| 260 | _t = {"module": "fio", "action": "get_resultlist"} |
| 261 | _status = {} |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 262 | for _agent, _d in self.agent_results.items(): |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 263 | _status[_agent] = self._poke_agent(_d["url"], _t, action="POST") |
| 264 | return _status |
| 265 | |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 266 | @retry(Exception, initial_wait=5) |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 267 | def get_result_from_agent(self, agent, time): |
| 268 | _t = { |
| 269 | "module": "fio", |
| 270 | "action": "get_result", |
| 271 | "options": { |
| 272 | "time": time |
| 273 | } |
| 274 | } |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 275 | return self._poke_agent( |
| 276 | self.agent_results[agent]["url"], |
| 277 | _t, |
| 278 | action="POST" |
| 279 | ) |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 280 | |
| 281 | def _get_next_scheduled_time(self): |
| 282 | _now = datetime.now(timezone.utc) |
| 283 | logger_cli.info("-> time is '{}'".format(_now.strftime(_datetime_fmt))) |
Alex | 90ac153 | 2021-12-09 11:13:14 -0600 | [diff] [blame^] | 284 | self.next_scheduled_time = _now + timedelta( |
| 285 | seconds=self.scheduled_delay |
| 286 | ) |
| 287 | _str_time = self.next_scheduled_time.strftime(_datetime_fmt) |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 288 | logger_cli.info( |
| 289 | "-> next benchmark scheduled to '{}'".format(_str_time) |
| 290 | ) |
| 291 | return _str_time |
| 292 | |
| 293 | def _send_scheduled_task(self, options): |
| 294 | _task = { |
| 295 | "module": "fio", |
| 296 | "action": "do_scheduledrun", |
| 297 | "options": options |
| 298 | } |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 299 | for _agent, _d in self.agent_results.items(): |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 300 | logger_cli.info( |
| 301 | "-> sending task to '{}:{}'".format(_agent, _d["url"]) |
| 302 | ) |
| 303 | _ret = self._poke_agent(_d["url"], _task, action="POST") |
| 304 | if 'error' in _ret: |
| 305 | logger_cli.error( |
| 306 | "ERROR: Agent returned: '{}'".format(_ret['error']) |
| 307 | ) |
| 308 | return False |
| 309 | # No errors detected |
| 310 | return True |
| 311 | |
| 312 | def track_benchmark(self, options): |
| 313 | _runtime = _get_seconds(options["runtime"]) |
| 314 | _ramptime = _get_seconds(options["ramp_time"]) |
| 315 | # Sum up all timings that we must wait and double it |
| 316 | _timeout = (self.scheduled_delay + _runtime + _ramptime) * 2 |
Alex | 90ac153 | 2021-12-09 11:13:14 -0600 | [diff] [blame^] | 317 | _start = self.next_scheduled_time |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 318 | _end = datetime.now(timezone.utc) + timedelta(seconds=_timeout) |
| 319 | while True: |
| 320 | # Print status |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 321 | _sts = self.get_agents_status() |
| 322 | diff = (_end - datetime.now(timezone.utc)).total_seconds() |
| 323 | logger_cli.info("-> {:.2f}s left. Agent status:".format(diff)) |
| 324 | for _agent, _status in _sts.items(): |
| 325 | logger_cli.info( |
| 326 | "\t{}: {} ({}%)".format( |
| 327 | _agent, |
| 328 | _status["status"], |
| 329 | _status["progress"] |
| 330 | ) |
| 331 | ) |
Alex | 90ac153 | 2021-12-09 11:13:14 -0600 | [diff] [blame^] | 332 | # Get Ceph status if _start time passed |
| 333 | _elapsed = (datetime.now(timezone.utc) - _start).total_seconds() |
| 334 | if _elapsed > 0: |
| 335 | logger_cli.info("-> {:.2f}s elapsed".format(_elapsed)) |
| 336 | self.results[options["scheduled_to"]]["ceph"][_elapsed] = \ |
| 337 | self.ceph_info.get_cluster_status() |
| 338 | # Check if agents finished |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 339 | finished = [True for _s in _sts.values() |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 340 | if _s["status"] == 'finished'] |
| 341 | _fcnt = len(finished) |
| 342 | _tcnt = len(_sts) |
| 343 | if _fcnt < _tcnt: |
| 344 | logger_cli.info("-> {}/{} finished".format(_fcnt, _tcnt)) |
| 345 | else: |
| 346 | logger_cli.info("-> All agents finished run") |
| 347 | return True |
| 348 | # recalc how much is left |
| 349 | diff = (_end - datetime.now(timezone.utc)).total_seconds() |
| 350 | # In case end_datetime was in past to begin with |
| 351 | if diff < 0: |
| 352 | logger_cli.info("-> Timed out waiting for agents to finish") |
| 353 | return False |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 354 | |
| 355 | def _do_testrun(self, options): |
| 356 | # send single to agent |
| 357 | if not self._send_scheduled_task(options): |
| 358 | return False |
| 359 | # Track this benchmark progress |
| 360 | if not self.track_benchmark(options): |
| 361 | return False |
| 362 | else: |
Alex | 90ac153 | 2021-12-09 11:13:14 -0600 | [diff] [blame^] | 363 | logger_cli.info("-> Finished testrun. Collecting results...") |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 364 | # Get results for each agent |
Alex | 90ac153 | 2021-12-09 11:13:14 -0600 | [diff] [blame^] | 365 | self.collect_results() |
| 366 | logger_cli.info("-> Calculating totals and averages") |
| 367 | self.calculate_totals() |
| 368 | self.calculate_ceph_stats() |
| 369 | logger_cli.info("-> Dumping results") |
| 370 | for _time, _d in self.results.items(): |
| 371 | self.dump_result( |
| 372 | self._get_dump_filename(_time), |
| 373 | _d |
| 374 | ) |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 375 | return True |
| 376 | |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 377 | def wait_ceph_cooldown(self): |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 378 | # TODO: Query Ceph ince a 20 sec to make sure its load dropped |
| 379 | |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 380 | # get ceph idle status |
| 381 | self.ceph_idle_status = self.ceph_info.get_cluster_status() |
| 382 | self.health_detail = self.ceph_info.get_health_detail() |
| 383 | self.ceph_df = self.ceph_info.get_ceph_df() |
| 384 | self.ceph_pg_dump = self.ceph_info.get_ceph_pg_dump() |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 385 | return |
| 386 | |
| 387 | def run_benchmark(self, options): |
| 388 | logger_cli.info("# Starting '{}' benchmark".format(self.mode)) |
| 389 | # Check agent readyness |
| 390 | logger_cli.info("# Checking agents") |
| 391 | if not self._ensure_agents_ready(): |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 392 | return False |
| 393 | |
| 394 | # Make sure that Ceph is at low load |
| 395 | # TODO: Ceph status check |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 396 | # self._wait_ceph_cooldown() |
| 397 | |
| 398 | _get_df = self.ceph_info.get_ceph_osd_df |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 399 | |
| 400 | # Do benchmark according to mode |
| 401 | if self.mode == "tasks": |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 402 | logger_cli.info( |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 403 | "# Running benchmark with tasks from '{}'".format( |
| 404 | self.taskfile |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 405 | ) |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 406 | ) |
| 407 | # take next task |
| 408 | _total_tasks = len(self.tasks) |
| 409 | for idx in range(_total_tasks): |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 410 | # init time to schedule |
| 411 | _osd_df_before = _get_df() |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 412 | _task = self.tasks[idx] |
| 413 | logger_cli.info( |
| 414 | "-> Starting next task ({}/{})".format(idx+1, _total_tasks) |
| 415 | ) |
| 416 | logger_cli.info("-> Updating options with: {}".format( |
| 417 | ", ".join( |
| 418 | ["{} = {}".format(k, v) for k, v in _task.items()] |
| 419 | ) |
| 420 | ) |
| 421 | ) |
| 422 | # update options |
| 423 | options.update(_task) |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 424 | _sch_time = self._get_next_scheduled_time() |
| 425 | options["scheduled_to"] = _sch_time |
| 426 | # init results table |
| 427 | self.results[_sch_time] = { |
| 428 | "input_options": options, |
| 429 | "agents": {}, |
Alex | 90ac153 | 2021-12-09 11:13:14 -0600 | [diff] [blame^] | 430 | "ceph": {}, |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 431 | "osd_df_before": _osd_df_before |
| 432 | } |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 433 | if not self._do_testrun(options): |
| 434 | return False |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 435 | else: |
| 436 | self.results[_sch_time]["osd_df_after"] = _get_df() |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 437 | |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 438 | self.wait_ceph_cooldown() |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 439 | elif self.mode == "single": |
| 440 | logger_cli.info("# Running single benchmark") |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 441 | _osd_df_before = _get_df() |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 442 | # init time to schedule |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 443 | _sch_time = self._get_next_scheduled_time() |
| 444 | options["scheduled_to"] = _sch_time |
| 445 | # init results table |
| 446 | self.results[_sch_time] = { |
| 447 | "input_options": options, |
| 448 | "agents": {}, |
Alex | 90ac153 | 2021-12-09 11:13:14 -0600 | [diff] [blame^] | 449 | "ceph": {}, |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 450 | "osd_df_before": _osd_df_before |
| 451 | } |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 452 | if not self._do_testrun(options): |
| 453 | return False |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 454 | else: |
| 455 | self.results[_sch_time]["osd_df_after"] = _get_df() |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 456 | else: |
| 457 | logger_cli.error("ERROR: Unknown mode '{}'".format(self.mode)) |
| 458 | return False |
| 459 | |
| 460 | # Normal exit |
| 461 | logger_cli.info("# All benchmark tasks done") |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 462 | return True |
| 463 | |
| 464 | def cleanup(self): |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 465 | logger_cli.info("# Cleaning up") |
Alex | 2a7657c | 2021-11-10 20:51:34 -0600 | [diff] [blame] | 466 | self.cleanup_list.reverse() |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 467 | |
Alex | 2a7657c | 2021-11-10 20:51:34 -0600 | [diff] [blame] | 468 | for _res in self.cleanup_list: |
| 469 | self.master.cleanup_resource_by_name(_res[0], _res[2], ns=_res[1]) |
Alex | bfa947c | 2021-11-11 18:14:28 -0600 | [diff] [blame] | 470 | |
| 471 | # Wait for resource to be cleaned |
| 472 | _timeout = 120 |
| 473 | _total = len(self.cleanup_list) |
| 474 | logger_cli.info("-> Wait until {} resources cleaned".format(_total)) |
| 475 | _p = Progress(_total) |
| 476 | while True: |
| 477 | _g = self.master.get_resource_phase_by_name |
| 478 | _l = [_g(r[0], r[2], ns=r[1]) for r in self.cleanup_list] |
| 479 | _l = [item for item in _l if item] |
| 480 | _idx = _total - len(_l) |
| 481 | if len(_l) > 0: |
| 482 | _p.write_progress(_idx) |
| 483 | else: |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 484 | _p.write_progress(_idx) |
Alex | bfa947c | 2021-11-11 18:14:28 -0600 | [diff] [blame] | 485 | _p.end() |
| 486 | logger_cli.info("# Done cleaning up") |
| 487 | break |
| 488 | if _timeout > 0: |
| 489 | _timeout -= 1 |
| 490 | else: |
| 491 | _p.end() |
| 492 | logger_cli.info("# Timed out waiting after 120s.") |
| 493 | break |
| 494 | |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 495 | return |
| 496 | |
Alex | 90ac153 | 2021-12-09 11:13:14 -0600 | [diff] [blame^] | 497 | def collect_results(self): |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 498 | logger_cli.info("# Collecting results") |
| 499 | # query agents for results |
| 500 | _agents = self.get_agents_resultlist() |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 501 | for _agent, _l in _agents.items(): |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 502 | # Check if we already have this locally |
| 503 | for _time in _l["resultlist"]: |
Alex | 90ac153 | 2021-12-09 11:13:14 -0600 | [diff] [blame^] | 504 | # There is a file already for this task/time |
| 505 | # Check if we need to load it |
| 506 | if _time not in self.results: |
| 507 | # Some older results found |
| 508 | # do not process them |
| 509 | logger_cli.info( |
| 510 | "-> Skipped old results for '{}'".format(_time) |
| 511 | ) |
| 512 | continue |
| 513 | elif _agent not in self.results[_time]["agents"]: |
| 514 | # Load result add it locally |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 515 | logger_cli.info( |
| 516 | "-> Getting results for '{}' from '{}'".format( |
Alex | 90ac153 | 2021-12-09 11:13:14 -0600 | [diff] [blame^] | 517 | _time, |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 518 | _agent |
| 519 | ) |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 520 | ) |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 521 | _r = self.get_result_from_agent(_agent, _time) |
Alex | 90ac153 | 2021-12-09 11:13:14 -0600 | [diff] [blame^] | 522 | self.results[_time]["agents"][_agent] = _r[_time] |
| 523 | else: |
| 524 | # Should never happen, actually |
| 525 | logger_cli.info( |
| 526 | "-> Skipped loaded result for '{}' from '{}'".format( |
| 527 | _time, |
| 528 | _agent |
| 529 | ) |
| 530 | ) |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 531 | |
Alex | 90ac153 | 2021-12-09 11:13:14 -0600 | [diff] [blame^] | 532 | def _get_dump_filename(self, _time): |
| 533 | _r = self.results[_time] |
| 534 | _dirname = _r["input_options"]["name"] |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 535 | _filename = "-".join([ |
Alex | 90ac153 | 2021-12-09 11:13:14 -0600 | [diff] [blame^] | 536 | _reformat_timestr(_time), |
| 537 | "{:02}".format(len(_r["agents"])), |
| 538 | _r["input_options"]["readwrite"], |
| 539 | _r["input_options"]["bs"], |
| 540 | str(_r["input_options"]["iodepth"]), |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 541 | ]) + ".json" |
| 542 | return os.path.join( |
| 543 | self.results_dump_path, |
| 544 | _dirname, |
| 545 | _filename |
| 546 | ) |
| 547 | |
Alex | 90ac153 | 2021-12-09 11:13:14 -0600 | [diff] [blame^] | 548 | def preload_results(self): |
| 549 | logger_cli.info( |
| 550 | "# Preloading results for '{}'".format(self.bench_name) |
| 551 | ) |
| 552 | # get all dirs in folder |
| 553 | _p = self.results_dump_path |
| 554 | if not os.path.isdir(_p): |
| 555 | logger_cli.warn( |
| 556 | "WARNING: Dump path is not a folder '{}'".format(_p) |
| 557 | ) |
| 558 | return |
| 559 | for path, dirs, files in os.walk(_p): |
| 560 | if path == os.path.join(_p, self.bench_name): |
| 561 | logger_cli.info("-> Folder found '{}'".format(path)) |
| 562 | for _fname in files: |
| 563 | logger_cli.debug("... processing '{}'".format(_fname)) |
| 564 | _ext = _fname.split('.')[-1] |
| 565 | if _ext != "json": |
| 566 | logger_cli.info( |
| 567 | "-> Extension invalid '{}', " |
| 568 | "'json' is expected".format(_ext) |
| 569 | ) |
| 570 | continue |
| 571 | # get time from filename |
| 572 | # Ugly, but works |
| 573 | _t = _fname.split('-')[0] |
| 574 | _str_time = _t[:14] + "+" + _t[14:] |
| 575 | _t = datetime.strptime(_str_time, _file_datetime_fmt) |
| 576 | _time = _t.strftime(_datetime_fmt) |
| 577 | self.results[_time] = self.load_dumped_result( |
| 578 | os.path.join(path, _fname) |
| 579 | ) |
| 580 | logger_cli.info( |
| 581 | "-> Loaded '{}' as '{}'".format( |
| 582 | _fname, |
| 583 | _time |
| 584 | ) |
| 585 | ) |
| 586 | |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 587 | def dump_result(self, filename, data): |
| 588 | # Function dumps all available results as jsons to the given path |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 589 | # overwriting if needed |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 590 | _folder, _file = os.path.split(filename) |
| 591 | # Do dump |
| 592 | if not os.path.exists(_folder): |
| 593 | os.mkdir(_folder) |
| 594 | logger_cli.info("-> Created folder '{}'".format(_folder)) |
| 595 | # Dump agent data for this test run |
| 596 | write_str_to_file(filename, json.dumps(data, indent=2)) |
| 597 | logger_cli.info("-> Dumped '{}'".format(filename)) |
Alex | 3034ba5 | 2021-11-13 17:06:45 -0600 | [diff] [blame] | 598 | return |
| 599 | |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 600 | def load_dumped_result(self, filename): |
| 601 | try: |
| 602 | with open(filename, "rt+") as f: |
| 603 | return json.loads(f.read()) |
| 604 | except FileNotFoundError as e: |
| 605 | logger_cli.error( |
| 606 | "ERROR: {}".format(e) |
| 607 | ) |
| 608 | except TypeError as e: |
| 609 | logger_cli.error( |
| 610 | "ERROR: Invalid file ({}): {}".format(filename, e) |
| 611 | ) |
| 612 | except json.decoder.JSONDecodeError as e: |
| 613 | logger_cli.error( |
| 614 | "ERROR: Failed to decode json ({}): {}".format(filename, e) |
| 615 | ) |
| 616 | return None |
| 617 | |
| 618 | def _lookup_storage_class_id_by_name(self, storage_class_name): |
| 619 | # Assume that self had proper data |
| 620 | for _pool in self.ceph_df["pools"]: |
| 621 | if storage_class_name == _pool["name"]: |
| 622 | return _pool["id"] |
| 623 | return None |
| 624 | |
| 625 | def calculate_totals(self): |
| 626 | # Calculate totals for Read and Write |
| 627 | for _time, data in self.results.items(): |
| 628 | if "totals" not in data: |
| 629 | data["totals"] = {} |
| 630 | else: |
| 631 | continue |
| 632 | _totals = data["totals"] |
| 633 | _r_bw = 0 |
| 634 | _r_avglat = [] |
| 635 | _r_iops = 0 |
| 636 | _w_bw = 0 |
| 637 | _w_avglat = [] |
| 638 | _w_iops = 0 |
| 639 | for _a, _d in data["agents"].items(): |
| 640 | # Hardcoded number of jobs param :( |
Alex | 90ac153 | 2021-12-09 11:13:14 -0600 | [diff] [blame^] | 641 | _j = _d["jobs"][0] |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 642 | _r_bw += _j["read"]["bw_bytes"] |
| 643 | _r_avglat += [_j["read"]["lat_ns"]["mean"]] |
| 644 | _r_iops += _j["read"]["iops"] |
| 645 | _w_bw += _j["write"]["bw_bytes"] |
| 646 | _w_avglat += [_j["write"]["lat_ns"]["mean"]] |
| 647 | _w_iops += _j["write"]["iops"] |
| 648 | # Save storage class name |
| 649 | if "storage_class" not in _totals: |
| 650 | _totals["storage_class"] = \ |
| 651 | self.agent_results[_a]["storage_class"] |
| 652 | # Lookup storage class id and num_pg |
| 653 | _totals["storage_class_stats"] = \ |
| 654 | reporter.get_pool_stats_by_id( |
| 655 | self._lookup_storage_class_id_by_name( |
| 656 | self.agent_results[_a]["storage_class"] |
| 657 | ), |
| 658 | self.ceph_pg_dump |
| 659 | ) |
| 660 | |
| 661 | _totals["read_bw_bytes"] = _r_bw |
| 662 | _totals["read_avg_lat_us"] = \ |
| 663 | (sum(_r_avglat) / len(_r_avglat)) / 1000 |
| 664 | _totals["read_iops"] = _r_iops |
| 665 | _totals["write_bw_bytes"] = _w_bw |
| 666 | _totals["write_avg_lat_us"] = \ |
| 667 | (sum(_w_avglat) / len(_w_avglat)) / 1000 |
| 668 | _totals["write_iops"] = _w_iops |
| 669 | |
Alex | 90ac153 | 2021-12-09 11:13:14 -0600 | [diff] [blame^] | 670 | def calculate_ceph_stats(self): |
| 671 | # func to get values as lists |
| 672 | def _as_list(key, stats): |
| 673 | _list = [] |
| 674 | for _v in stats.values(): |
| 675 | if key in _v: |
| 676 | _list += [_v[key]] |
| 677 | else: |
| 678 | _list += [0] |
| 679 | return _list |
| 680 | |
| 681 | def _perc(n, m): |
| 682 | if not n: |
| 683 | return 0 |
| 684 | elif not m: |
| 685 | return 0 |
| 686 | else: |
| 687 | return (n / m) * 100 |
| 688 | |
| 689 | _stats = {} |
| 690 | for _time, data in self.results.items(): |
| 691 | if "ceph" not in data: |
| 692 | logger_cli.warning( |
| 693 | "WARNING: Ceph stats raw data not found in results" |
| 694 | ) |
| 695 | continue |
| 696 | if "ceph_stats" not in data: |
| 697 | data["ceph_stats"] = {} |
| 698 | else: |
| 699 | continue |
| 700 | # Copy pool stats data |
| 701 | for _e, _d in data["ceph"].items(): |
| 702 | _stats[_e] = _d["pgmap"] |
| 703 | # Maximums |
| 704 | m_r_bytes = max(_as_list("read_bytes_sec", _stats)) |
| 705 | m_w_bytes = max(_as_list("write_bytes_sec", _stats)) |
| 706 | m_r_iops = max(_as_list("read_op_per_sec", _stats)) |
| 707 | m_w_iops = max(_as_list("write_op_per_sec", _stats)) |
| 708 | # Replace ceph with shorter data |
| 709 | data["ceph"] = { |
| 710 | "max_read_bytes_sec": m_r_bytes, |
| 711 | "max_write_bytes_sec": m_w_bytes, |
| 712 | "max_read_iops_sec": m_r_iops, |
| 713 | "max_write_iops_sec": m_w_iops, |
| 714 | "stats": _stats |
| 715 | } |
| 716 | # Calculate %% values for barchart |
| 717 | for _e, _d in data["ceph"]["stats"].items(): |
| 718 | _d["read_bytes_sec_perc"] = \ |
| 719 | _perc(_d.get("read_bytes_sec", 0), m_r_bytes) |
| 720 | _d["write_bytes_sec_perc"] = \ |
| 721 | _perc(_d.get("write_bytes_sec", 0), m_w_bytes) |
| 722 | _d["read_op_per_sec_perc"] = \ |
| 723 | _perc(_d.get("read_op_per_sec", 0), m_r_iops) |
| 724 | _d["write_op_per_sec_perc"] = \ |
| 725 | _perc(_d.get("write_op_per_sec", 0), m_w_iops) |
| 726 | return |
| 727 | |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 728 | # Create report |
Alex | 2a7657c | 2021-11-10 20:51:34 -0600 | [diff] [blame] | 729 | def create_report(self, filename): |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 730 | """ |
| 731 | Create static html showing ceph info report |
| 732 | |
| 733 | :return: none |
| 734 | """ |
| 735 | logger_cli.info("### Generating report to '{}'".format(filename)) |
| 736 | _report = reporter.ReportToFile( |
| 737 | reporter.HTMLCephBench(self), |
| 738 | filename |
| 739 | ) |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 740 | _report( |
| 741 | { |
| 742 | "results": self.results, |
| 743 | "idle_status": self.ceph_idle_status, |
| 744 | "health_detail": self.health_detail, |
| 745 | "ceph_df": self.ceph_df, |
| 746 | "ceph_pg_dump": self.ceph_pg_dump, |
| 747 | "info": self.ceph_info.ceph_info, |
| 748 | "cluster": self.ceph_info.cluster_info, |
| 749 | "ceph_version": self.ceph_info.ceph_version, |
| 750 | "nodes": self.agent_pods |
| 751 | } |
| 752 | ) |
| 753 | logger_cli.info("-> Done") |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 754 | |
| 755 | return |