gstepanov | 023c1e4 | 2015-04-08 15:50:19 +0300 | [diff] [blame] | 1 | import os |
| 2 | import pickle |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 3 | import sys |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 4 | import json |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 5 | import Queue |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 6 | import pprint |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 7 | import logging |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 8 | import argparse |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 9 | import threading |
| 10 | import collections |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 11 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 12 | from concurrent.futures import ThreadPoolExecutor |
| 13 | |
| 14 | import utils |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 15 | import ssh_utils |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 16 | import start_vms |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 17 | from nodes import discover |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 18 | from nodes.node import Node |
gstepanov | cd256d6 | 2015-04-07 17:47:32 +0300 | [diff] [blame] | 19 | from config import cfg_dict, parse_config |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 20 | from tests.itest import IOPerfTest, PgBenchTest |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 21 | from sensors.api import start_monitoring |
| 22 | |
| 23 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 24 | logger = logging.getLogger("io-perf-tool") |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 25 | |
| 26 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 27 | def setup_logger(logger, level=logging.DEBUG): |
| 28 | logger.setLevel(level) |
| 29 | ch = logging.StreamHandler() |
| 30 | ch.setLevel(level) |
| 31 | logger.addHandler(ch) |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 32 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 33 | log_format = '%(asctime)s - %(levelname)-6s - %(name)s - %(message)s' |
| 34 | formatter = logging.Formatter(log_format, |
| 35 | "%H:%M:%S") |
| 36 | ch.setFormatter(formatter) |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 37 | |
| 38 | |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 39 | def format_result(res, formatter): |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 40 | data = "\n{0}\n".format("=" * 80) |
| 41 | data += pprint.pformat(res) + "\n" |
| 42 | data += "{0}\n".format("=" * 80) |
koder aka kdanilov | fe05662 | 2015-02-19 08:46:15 -0800 | [diff] [blame] | 43 | templ = "{0}\n\n====> {1}\n\n{2}\n\n" |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 44 | return templ.format(data, formatter(res), "=" * 80) |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 45 | |
| 46 | |
koder aka kdanilov | 5d589b4 | 2015-03-26 12:25:51 +0200 | [diff] [blame] | 47 | def connect_one(node): |
| 48 | try: |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 49 | ssh_pref = "ssh://" |
| 50 | if node.conn_url.startswith(ssh_pref): |
| 51 | url = node.conn_url[len(ssh_pref):] |
| 52 | node.connection = ssh_utils.connect(url) |
| 53 | else: |
| 54 | raise ValueError("Unknown url type {0}".format(node.conn_url)) |
koder aka kdanilov | 3a6633e | 2015-03-26 18:20:00 +0200 | [diff] [blame] | 55 | except Exception: |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 56 | logger.exception("During connect to {0}".format(node)) |
koder aka kdanilov | 5d589b4 | 2015-03-26 12:25:51 +0200 | [diff] [blame] | 57 | |
| 58 | |
| 59 | def connect_all(nodes): |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 60 | logger.info("Connecting to nodes") |
| 61 | with ThreadPoolExecutor(32) as pool: |
| 62 | list(pool.map(connect_one, nodes)) |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 63 | logger.info("All nodes connected successfully") |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 64 | |
| 65 | |
| 66 | def save_sensors_data(q): |
| 67 | logger.info("Start receiving sensors data") |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 68 | sensor_data = [] |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 69 | while True: |
| 70 | val = q.get() |
| 71 | if val is None: |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 72 | print sensor_data |
| 73 | q.put(sensor_data) |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 74 | break |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 75 | sensor_data.append(val) |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 76 | logger.info("Sensors thread exits") |
| 77 | |
| 78 | |
| 79 | def test_thread(test, node, barrier): |
| 80 | try: |
| 81 | logger.debug("Run preparation for {0}".format(node.conn_url)) |
| 82 | test.pre_run(node.connection) |
| 83 | logger.debug("Run test for {0}".format(node.conn_url)) |
| 84 | test.run(node.connection, barrier) |
| 85 | except: |
| 86 | logger.exception("In test {0} for node {1}".format(test, node)) |
| 87 | |
| 88 | |
| 89 | def run_tests(config, nodes): |
| 90 | tool_type_mapper = { |
| 91 | "io": IOPerfTest, |
| 92 | "pgbench": PgBenchTest, |
| 93 | } |
| 94 | |
| 95 | test_nodes = [node for node in nodes |
| 96 | if 'testnode' in node.roles] |
| 97 | |
| 98 | res_q = Queue.Queue() |
| 99 | |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 100 | for test in config['tests']: |
gstepanov | 023c1e4 | 2015-04-08 15:50:19 +0300 | [diff] [blame] | 101 | for test in config['tests'][test]['internal_tests']: |
| 102 | for name, params in test.items(): |
| 103 | logger.info("Starting {0} tests".format(name)) |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 104 | |
gstepanov | 023c1e4 | 2015-04-08 15:50:19 +0300 | [diff] [blame] | 105 | threads = [] |
| 106 | barrier = utils.Barrier(len(test_nodes)) |
| 107 | for node in test_nodes: |
| 108 | msg = "Starting {0} test on {1} node" |
| 109 | logger.debug(msg.format(name, node.conn_url)) |
| 110 | test = tool_type_mapper[name](params, res_q.put) |
| 111 | th = threading.Thread(None, test_thread, None, |
| 112 | (test, node, barrier)) |
| 113 | threads.append(th) |
| 114 | th.daemon = True |
| 115 | th.start() |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 116 | |
gstepanov | 023c1e4 | 2015-04-08 15:50:19 +0300 | [diff] [blame] | 117 | for th in threads: |
| 118 | th.join() |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 119 | |
gstepanov | 023c1e4 | 2015-04-08 15:50:19 +0300 | [diff] [blame] | 120 | results = [] |
| 121 | while not res_q.empty(): |
| 122 | results.append(res_q.get()) |
| 123 | # logger.info("Get test result {0!r}".format(results[-1])) |
| 124 | yield name, results |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 125 | |
| 126 | |
| 127 | def parse_args(argv): |
| 128 | parser = argparse.ArgumentParser( |
| 129 | description="Run disk io performance test") |
| 130 | |
| 131 | parser.add_argument("-l", dest='extra_logs', |
| 132 | action='store_true', default=False, |
| 133 | help="print some extra log info") |
| 134 | |
gstepanov | 4861d71 | 2015-04-09 13:28:02 +0300 | [diff] [blame^] | 135 | parser.add_argument("-b", '--build_description', |
| 136 | type=str, default="Build info") |
gstepanov | affcdb1 | 2015-04-07 17:18:29 +0300 | [diff] [blame] | 137 | parser.add_argument("-i", '--build_id', type=str, default="id") |
| 138 | parser.add_argument("-t", '--build_type', type=str, default="GA") |
| 139 | parser.add_argument("-u", '--username', type=str, default="admin") |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 140 | parser.add_argument("-o", '--output-dest', nargs="*") |
| 141 | parser.add_argument("config_file", nargs="?", default="config.yaml") |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 142 | |
| 143 | return parser.parse_args(argv[1:]) |
| 144 | |
| 145 | |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 146 | def log_nodes_statistic(_, ctx): |
| 147 | nodes = ctx.nodes |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 148 | logger.info("Found {0} nodes total".format(len(nodes))) |
| 149 | per_role = collections.defaultdict(lambda: 0) |
| 150 | for node in nodes: |
| 151 | for role in node.roles: |
| 152 | per_role[role] += 1 |
| 153 | |
| 154 | for role, count in sorted(per_role.items()): |
| 155 | logger.debug("Found {0} nodes with role {1}".format(count, role)) |
| 156 | |
| 157 | |
| 158 | def log_sensors_config(cfg): |
koder aka kdanilov | 5d589b4 | 2015-03-26 12:25:51 +0200 | [diff] [blame] | 159 | pass |
| 160 | |
| 161 | |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 162 | def connect_stage(cfg, ctx): |
| 163 | ctx.clear_calls_stack.append(disconnect_stage) |
| 164 | connect_all(ctx.nodes) |
| 165 | |
| 166 | |
| 167 | def discover_stage(cfg, ctx): |
| 168 | if 'discover' in cfg: |
| 169 | discover_objs = [i.strip() for i in cfg['discover'].strip().split(",")] |
| 170 | ctx.nodes.extend(discover.discover(discover_objs, cfg['clouds'])) |
| 171 | |
| 172 | for url, roles in cfg.get('explicit_nodes', {}).items(): |
| 173 | ctx.nodes.append(Node(url, roles.split(","))) |
| 174 | |
| 175 | |
| 176 | def deploy_sensors_stage(cfg_dict, ctx): |
| 177 | ctx.clear_calls_stack.append(remove_sensors_stage) |
| 178 | if 'sensors' not in cfg_dict: |
| 179 | return |
| 180 | |
| 181 | cfg = cfg_dict.get('sensors') |
| 182 | sens_cfg = [] |
| 183 | |
| 184 | for role, sensors_str in cfg["roles_mapping"].items(): |
| 185 | sensors = [sens.strip() for sens in sensors_str.split(",")] |
| 186 | |
| 187 | collect_cfg = dict((sensor, {}) for sensor in sensors) |
| 188 | |
| 189 | for node in ctx.nodes: |
| 190 | if role in node.roles: |
| 191 | sens_cfg.append((node.connection, collect_cfg)) |
| 192 | |
| 193 | log_sensors_config(sens_cfg) |
| 194 | |
| 195 | ctx.sensor_cm = start_monitoring(cfg["receiver_uri"], None, |
| 196 | connected_config=sens_cfg) |
| 197 | |
| 198 | ctx.sensors_control_queue = ctx.sensor_cm.__enter__() |
| 199 | |
| 200 | th = threading.Thread(None, save_sensors_data, None, |
| 201 | (ctx.sensors_control_queue,)) |
| 202 | th.daemon = True |
| 203 | th.start() |
| 204 | ctx.sensor_listen_thread = th |
| 205 | |
| 206 | |
| 207 | def remove_sensors_stage(cfg, ctx): |
| 208 | ctx.sensors_control_queue.put(None) |
| 209 | ctx.sensor_listen_thread.join() |
| 210 | ctx.sensor_data = ctx.sensors_control_queue.get() |
| 211 | |
| 212 | |
gstepanov | 023c1e4 | 2015-04-08 15:50:19 +0300 | [diff] [blame] | 213 | def run_all_test(cfg, ctx, store_nodes): |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 214 | ctx.results = [] |
| 215 | |
gstepanov | 023c1e4 | 2015-04-08 15:50:19 +0300 | [diff] [blame] | 216 | if 'start_test_nodes' in cfg['tests']: |
| 217 | params = cfg['tests']['start_test_nodes']['openstack'] |
| 218 | for new_node in start_vms.launch_vms(params): |
| 219 | new_node.roles.append('testnode') |
| 220 | ctx.nodes.append(new_node) |
| 221 | |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 222 | if 'tests' in cfg: |
gstepanov | 023c1e4 | 2015-04-08 15:50:19 +0300 | [diff] [blame] | 223 | store_nodes(ctx.nodes) |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 224 | ctx.results.extend(run_tests(cfg_dict, ctx.nodes)) |
| 225 | |
gstepanov | 023c1e4 | 2015-04-08 15:50:19 +0300 | [diff] [blame] | 226 | |
| 227 | def shut_down_vms(cfg, ctx): |
| 228 | with open('vm_journal.log') as f: |
| 229 | data = str(f.read()) |
| 230 | nodes = pickle.loads(data) |
| 231 | |
| 232 | for node in nodes: |
| 233 | logger.info("Node " + str(node) + " has been loaded") |
| 234 | |
| 235 | logger.info("Removing nodes") |
| 236 | start_vms.clear_nodes() |
| 237 | logger.info("Nodes has been removed") |
| 238 | |
| 239 | |
| 240 | def store_nodes(nodes): |
| 241 | with open('vm_journal.log', 'w+') as f: |
| 242 | f.write(pickle.dumps([nodes])) |
| 243 | for node in nodes: |
| 244 | logger.info("Node " + str(node) + " has been stored") |
| 245 | |
| 246 | |
| 247 | def clear_enviroment(cfg, ctx): |
| 248 | if os.path.exists('vm_journal.log'): |
| 249 | shut_down_vms(cfg, ctx) |
| 250 | os.remove('vm_journal.log') |
| 251 | |
| 252 | |
| 253 | def run_tests_stage(cfg, ctx): |
| 254 | # clear nodes that possible were created on previous test running |
| 255 | clear_enviroment(cfg, ctx) |
| 256 | ctx.clear_calls_stack.append(shut_down_vms) |
| 257 | run_all_test(cfg, ctx, store_nodes) |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 258 | |
| 259 | |
| 260 | def disconnect_stage(cfg, ctx): |
| 261 | for node in ctx.nodes: |
| 262 | if node.connection is not None: |
| 263 | node.connection.close() |
| 264 | |
| 265 | |
| 266 | def report_stage(cfg, ctx): |
| 267 | output_dest = cfg.get('output_dest') |
| 268 | if output_dest is not None: |
| 269 | with open(output_dest, "w") as fd: |
| 270 | data = {"sensor_data": ctx.sensor_data, |
| 271 | "results": ctx.results} |
| 272 | fd.write(json.dumps(data)) |
| 273 | else: |
| 274 | print "=" * 20 + " RESULTS " + "=" * 20 |
| 275 | pprint.pprint(ctx.results) |
| 276 | print "=" * 60 |
| 277 | |
| 278 | |
| 279 | def complete_log_nodes_statistic(cfg, ctx): |
| 280 | nodes = ctx.nodes |
| 281 | for node in nodes: |
| 282 | logger.debug(str(node)) |
| 283 | |
| 284 | |
| 285 | class Context(object): |
| 286 | def __init__(self): |
gstepanov | affcdb1 | 2015-04-07 17:18:29 +0300 | [diff] [blame] | 287 | self.build_meta = {} |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 288 | self.nodes = [] |
| 289 | self.clear_calls_stack = [] |
| 290 | |
| 291 | |
gstepanov | cd256d6 | 2015-04-07 17:47:32 +0300 | [diff] [blame] | 292 | def load_config(path): |
| 293 | global cfg_dict |
| 294 | cfg_dict = parse_config(path) |
| 295 | |
| 296 | |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 297 | def main(argv): |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 298 | opts = parse_args(argv) |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 299 | |
| 300 | level = logging.DEBUG if opts.extra_logs else logging.WARNING |
| 301 | setup_logger(logger, level) |
| 302 | |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 303 | stages = [ |
| 304 | discover_stage, |
| 305 | connect_stage, |
| 306 | complete_log_nodes_statistic, |
| 307 | # deploy_sensors_stage, |
| 308 | run_tests_stage, |
| 309 | report_stage |
| 310 | ] |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 311 | |
gstepanov | cd256d6 | 2015-04-07 17:47:32 +0300 | [diff] [blame] | 312 | load_config(opts.config_file) |
| 313 | |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 314 | ctx = Context() |
gstepanov | affcdb1 | 2015-04-07 17:18:29 +0300 | [diff] [blame] | 315 | ctx.build_meta['build_id'] = opts.build_id |
| 316 | ctx.build_meta['build_descrption'] = opts.build_description |
| 317 | ctx.build_meta['build_type'] = opts.build_type |
| 318 | ctx.build_meta['username'] = opts.username |
gstepanov | 023c1e4 | 2015-04-08 15:50:19 +0300 | [diff] [blame] | 319 | logger.setLevel(logging.INFO) |
| 320 | logger.addHandler(logging.FileHandler('log.txt')) |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 321 | try: |
| 322 | for stage in stages: |
| 323 | logger.info("Start {0.__name__} stage".format(stage)) |
gstepanov | 023c1e4 | 2015-04-08 15:50:19 +0300 | [diff] [blame] | 324 | print "Start {0.__name__} stage".format(stage) |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 325 | stage(cfg_dict, ctx) |
| 326 | finally: |
| 327 | exc, cls, tb = sys.exc_info() |
| 328 | for stage in ctx.clear_calls_stack[::-1]: |
| 329 | try: |
| 330 | logger.info("Start {0.__name__} stage".format(stage)) |
| 331 | stage(cfg_dict, ctx) |
| 332 | except: |
| 333 | pass |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 334 | |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 335 | if exc is not None: |
| 336 | raise exc, cls, tb |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 337 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 338 | return 0 |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 339 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 340 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 341 | if __name__ == '__main__': |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 342 | exit(main(sys.argv)) |