koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 1 | import os |
| 2 | import sys |
| 3 | import json |
| 4 | import time |
| 5 | import pprint |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 6 | import logging |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 7 | import os.path |
| 8 | import argparse |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 9 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 10 | import ssh_utils |
koder aka kdanilov | 4ec0f71 | 2015-02-19 19:19:27 -0800 | [diff] [blame] | 11 | import io_scenario |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 12 | from nodes import discover |
Yulia Portnova | 0e64ea2 | 2015-03-20 17:27:22 +0200 | [diff] [blame] | 13 | from config import cfg_dict |
koder aka kdanilov | 4ec0f71 | 2015-02-19 19:19:27 -0800 | [diff] [blame] | 14 | from utils import log_error |
| 15 | from rest_api import add_test |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 16 | from formatters import get_formatter |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 17 | from itest import IOPerfTest, PgBenchTest |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 18 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 19 | logger = logging.getLogger("io-perf-tool") |
yportnova | a47b33e | 2015-03-05 20:20:31 +0200 | [diff] [blame] | 20 | logger.setLevel(logging.DEBUG) |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 21 | ch = logging.StreamHandler() |
yportnova | a47b33e | 2015-03-05 20:20:31 +0200 | [diff] [blame] | 22 | ch.setLevel(logging.DEBUG) |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 23 | logger.addHandler(ch) |
| 24 | |
| 25 | log_format = '%(asctime)s - %(levelname)s - %(name)s - %(message)s' |
| 26 | formatter = logging.Formatter(log_format, |
| 27 | "%H:%M:%S") |
| 28 | ch.setFormatter(formatter) |
| 29 | |
| 30 | |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 31 | tool_type_mapper = { |
| 32 | "iozone": IOPerfTest, |
| 33 | "fio": IOPerfTest, |
| 34 | "pgbench": PgBenchTest, |
| 35 | } |
| 36 | |
| 37 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 38 | def run_io_test(tool, |
| 39 | script_args, |
| 40 | test_runner, |
| 41 | keep_temp_files=False): |
| 42 | |
| 43 | files_dir = os.path.dirname(io_scenario.__file__) |
| 44 | |
| 45 | path = 'iozone' if 'iozone' == tool else 'fio' |
| 46 | src_testtool_path = os.path.join(files_dir, path) |
| 47 | |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 48 | obj_cls = tool_type_mapper[tool] |
| 49 | obj = obj_cls(script_args, |
| 50 | src_testtool_path, |
| 51 | None, |
| 52 | keep_temp_files) |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 53 | |
| 54 | return test_runner(obj) |
| 55 | |
| 56 | |
koder aka kdanilov | 3a6633e | 2015-03-26 18:20:00 +0200 | [diff] [blame] | 57 | def conn_func(obj, barrier, latest_start_time, conn): |
| 58 | try: |
| 59 | test_iter = itest.run_test_iter(obj, conn) |
| 60 | next(test_iter) |
| 61 | |
| 62 | wait_on_barrier(barrier, latest_start_time) |
| 63 | |
| 64 | with log_error("!Run test"): |
| 65 | return next(test_iter) |
| 66 | except: |
| 67 | print traceback.format_exc() |
| 68 | raise |
| 69 | |
| 70 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 71 | def parse_args(argv): |
| 72 | parser = argparse.ArgumentParser( |
| 73 | description="Run disk io performance test") |
| 74 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 75 | parser.add_argument("-l", dest='extra_logs', |
| 76 | action='store_true', default=False, |
| 77 | help="print some extra log info") |
| 78 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 79 | parser.add_argument('stages', nargs="+", |
| 80 | choices=["discover", "connect", "start_new_nodes", |
| 81 | "deploy_sensors"]) |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 82 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 83 | # THIS ALL MOVE TO CONFIG FILE |
| 84 | # parser.add_argument("-o", "--test-opts", dest='opts', |
| 85 | # help="cmd line options for test") |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 86 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 87 | # parser.add_argument("-f", "--test-opts-file", dest='opts_file', |
| 88 | # type=argparse.FileType('r'), default=None, |
| 89 | # help="file with cmd line options for test") |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 90 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 91 | # parser.add_argument("--max-preparation-time", default=300, |
| 92 | # type=int, dest="max_preparation_time") |
gstepanov | 8053b01 | 2015-02-16 17:25:27 +0200 | [diff] [blame] | 93 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 94 | # parser.add_argument("-b", "--build-info", default=None, |
| 95 | # dest="build_name") |
gstepanov | 8053b01 | 2015-02-16 17:25:27 +0200 | [diff] [blame] | 96 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 97 | # parser.add_argument("-d", "--data-server-url", default=None, |
| 98 | # dest="data_server_url") |
gstepanov | 8053b01 | 2015-02-16 17:25:27 +0200 | [diff] [blame] | 99 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 100 | # parser.add_argument("-n", "--lab-name", default=None, |
| 101 | # dest="lab_name") |
koder aka kdanilov | 4ec0f71 | 2015-02-19 19:19:27 -0800 | [diff] [blame] | 102 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 103 | # parser.add_argument("--create-vms-opts", default=None, |
| 104 | # help="Creating vm's before run ssh runner", |
| 105 | # dest="create_vms_opts") |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 106 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 107 | # parser.add_argument("-k", "--keep", default=False, |
| 108 | # help="keep temporary files", |
| 109 | # dest="keep_temp_files", action='store_true') |
koder aka kdanilov | 4ec0f71 | 2015-02-19 19:19:27 -0800 | [diff] [blame] | 110 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 111 | # parser.add_argument("--runner", required=True, |
| 112 | # choices=["local", "ssh"], help="runner type") |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 113 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 114 | # parser.add_argument("--runner-extra-opts", default=None, |
| 115 | # dest="runner_opts", help="runner extra options") |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 116 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 117 | return parser.parse_args(argv[1:]) |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 118 | |
| 119 | |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 120 | def format_result(res, formatter): |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 121 | data = "\n{0}\n".format("=" * 80) |
| 122 | data += pprint.pformat(res) + "\n" |
| 123 | data += "{0}\n".format("=" * 80) |
koder aka kdanilov | fe05662 | 2015-02-19 08:46:15 -0800 | [diff] [blame] | 124 | templ = "{0}\n\n====> {1}\n\n{2}\n\n" |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 125 | return templ.format(data, formatter(res), "=" * 80) |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 126 | |
| 127 | |
koder aka kdanilov | 5d589b4 | 2015-03-26 12:25:51 +0200 | [diff] [blame] | 128 | def connect_one(node): |
| 129 | try: |
| 130 | node.connection = ssh_utils.connect(node.connection_url) |
koder aka kdanilov | 3a6633e | 2015-03-26 18:20:00 +0200 | [diff] [blame] | 131 | except Exception: |
| 132 | logger.exception() |
koder aka kdanilov | 5d589b4 | 2015-03-26 12:25:51 +0200 | [diff] [blame] | 133 | |
| 134 | |
| 135 | def connect_all(nodes): |
| 136 | pass |
| 137 | |
| 138 | |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 139 | def main(argv): |
Yulia Portnova | 0e64ea2 | 2015-03-20 17:27:22 +0200 | [diff] [blame] | 140 | logging_conf = cfg_dict.get('logging') |
| 141 | if logging_conf: |
| 142 | if logging_conf.get('extra_logs'): |
| 143 | logger.setLevel(logging.DEBUG) |
| 144 | ch.setLevel(logging.DEBUG) |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 145 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 146 | opts = parse_args(argv) |
| 147 | if 'discover' in opts.stages: |
koder aka kdanilov | 3a6633e | 2015-03-26 18:20:00 +0200 | [diff] [blame] | 148 | current_data = discover.discover(cfg_dict.get('discover')) |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 149 | |
| 150 | if 'connect' in opts.stages: |
| 151 | for node in current_data: |
koder aka kdanilov | 3a6633e | 2015-03-26 18:20:00 +0200 | [diff] [blame] | 152 | pass |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 153 | |
| 154 | print "\n".join(map(str, current_data)) |
| 155 | return 0 |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 156 | |
koder aka kdanilov | 5d589b4 | 2015-03-26 12:25:51 +0200 | [diff] [blame] | 157 | # tests = cfg_dict.get("tests", []) |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 158 | |
Yulia Portnova | 0e64ea2 | 2015-03-20 17:27:22 +0200 | [diff] [blame] | 159 | # Deploy and start sensors |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 160 | # deploy_and_start_sensors(cfg_dict.get('sensors'), nodes_to_run) |
koder aka kdanilov | 4ec0f71 | 2015-02-19 19:19:27 -0800 | [diff] [blame] | 161 | |
koder aka kdanilov | 5d589b4 | 2015-03-26 12:25:51 +0200 | [diff] [blame] | 162 | # for test_name, opts in tests.items(): |
| 163 | # cmd_line = " ".join(opts['opts']) |
| 164 | # logger.debug("Run test with {0!r} params".format(cmd_line)) |
| 165 | # latest_start_time = 300 + time.time() |
| 166 | # uris = [node.connection_url for node in nodes_to_run] |
koder aka kdanilov | 3a6633e | 2015-03-26 18:20:00 +0200 | [diff] [blame] | 167 | # runner = ssh_runner.get_ssh_runner(uris, conn_func, |
koder aka kdanilov | 5d589b4 | 2015-03-26 12:25:51 +0200 | [diff] [blame] | 168 | # latest_start_time, |
| 169 | # opts.get('keep_temp_files')) |
| 170 | # res = run_io_test(test_name, |
| 171 | # opts['opts'], |
| 172 | # runner, |
| 173 | # opts.get('keep_temp_files')) |
| 174 | # logger.debug(format_result(res, get_formatter(test_name))) |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 175 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 176 | # if cfg_dict.get('data_server_url'): |
| 177 | # result = json.loads(get_formatter(opts.tool_type)(res)) |
| 178 | # result['name'] = opts.build_name |
| 179 | # add_test(opts.build_name, result, opts.data_server_url) |
koder aka kdanilov | 4ec0f71 | 2015-02-19 19:19:27 -0800 | [diff] [blame] | 180 | |
koder aka kdanilov | 5d589b4 | 2015-03-26 12:25:51 +0200 | [diff] [blame] | 181 | # return 0 |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 182 | |
| 183 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 184 | if __name__ == '__main__': |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 185 | exit(main(sys.argv)) |