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 | |
| 57 | def parse_args(argv): |
| 58 | parser = argparse.ArgumentParser( |
| 59 | description="Run disk io performance test") |
| 60 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 61 | parser.add_argument("-l", dest='extra_logs', |
| 62 | action='store_true', default=False, |
| 63 | help="print some extra log info") |
| 64 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame^] | 65 | parser.add_argument('stages', nargs="+", |
| 66 | choices=["discover", "connect", "start_new_nodes", |
| 67 | "deploy_sensors"]) |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 68 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame^] | 69 | # THIS ALL MOVE TO CONFIG FILE |
| 70 | # parser.add_argument("-o", "--test-opts", dest='opts', |
| 71 | # help="cmd line options for test") |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 72 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame^] | 73 | # parser.add_argument("-f", "--test-opts-file", dest='opts_file', |
| 74 | # type=argparse.FileType('r'), default=None, |
| 75 | # help="file with cmd line options for test") |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 76 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame^] | 77 | # parser.add_argument("--max-preparation-time", default=300, |
| 78 | # type=int, dest="max_preparation_time") |
gstepanov | 8053b01 | 2015-02-16 17:25:27 +0200 | [diff] [blame] | 79 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame^] | 80 | # parser.add_argument("-b", "--build-info", default=None, |
| 81 | # dest="build_name") |
gstepanov | 8053b01 | 2015-02-16 17:25:27 +0200 | [diff] [blame] | 82 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame^] | 83 | # parser.add_argument("-d", "--data-server-url", default=None, |
| 84 | # dest="data_server_url") |
gstepanov | 8053b01 | 2015-02-16 17:25:27 +0200 | [diff] [blame] | 85 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame^] | 86 | # parser.add_argument("-n", "--lab-name", default=None, |
| 87 | # dest="lab_name") |
koder aka kdanilov | 4ec0f71 | 2015-02-19 19:19:27 -0800 | [diff] [blame] | 88 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame^] | 89 | # parser.add_argument("--create-vms-opts", default=None, |
| 90 | # help="Creating vm's before run ssh runner", |
| 91 | # dest="create_vms_opts") |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 92 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame^] | 93 | # parser.add_argument("-k", "--keep", default=False, |
| 94 | # help="keep temporary files", |
| 95 | # dest="keep_temp_files", action='store_true') |
koder aka kdanilov | 4ec0f71 | 2015-02-19 19:19:27 -0800 | [diff] [blame] | 96 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame^] | 97 | # parser.add_argument("--runner", required=True, |
| 98 | # choices=["local", "ssh"], help="runner type") |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 99 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame^] | 100 | # parser.add_argument("--runner-extra-opts", default=None, |
| 101 | # dest="runner_opts", help="runner extra options") |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 102 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame^] | 103 | return parser.parse_args(argv[1:]) |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 104 | |
| 105 | |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 106 | def format_result(res, formatter): |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 107 | data = "\n{0}\n".format("=" * 80) |
| 108 | data += pprint.pformat(res) + "\n" |
| 109 | data += "{0}\n".format("=" * 80) |
koder aka kdanilov | fe05662 | 2015-02-19 08:46:15 -0800 | [diff] [blame] | 110 | templ = "{0}\n\n====> {1}\n\n{2}\n\n" |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 111 | return templ.format(data, formatter(res), "=" * 80) |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 112 | |
| 113 | |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 114 | def main(argv): |
Yulia Portnova | 0e64ea2 | 2015-03-20 17:27:22 +0200 | [diff] [blame] | 115 | logging_conf = cfg_dict.get('logging') |
| 116 | if logging_conf: |
| 117 | if logging_conf.get('extra_logs'): |
| 118 | logger.setLevel(logging.DEBUG) |
| 119 | ch.setLevel(logging.DEBUG) |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 120 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame^] | 121 | opts = parse_args(argv) |
| 122 | if 'discover' in opts.stages: |
| 123 | current_data = discover.discover(cfg_dict.get('cluster')) |
| 124 | |
| 125 | if 'connect' in opts.stages: |
| 126 | for node in current_data: |
| 127 | node.connection = ssh_utils.connect(node.connection_url) |
| 128 | |
| 129 | print "\n".join(map(str, current_data)) |
| 130 | return 0 |
Yulia Portnova | 0e64ea2 | 2015-03-20 17:27:22 +0200 | [diff] [blame] | 131 | # Discover nodes |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 132 | |
Yulia Portnova | 0e64ea2 | 2015-03-20 17:27:22 +0200 | [diff] [blame] | 133 | tests = cfg_dict.get("tests", []) |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 134 | |
Yulia Portnova | 0e64ea2 | 2015-03-20 17:27:22 +0200 | [diff] [blame] | 135 | # Deploy and start sensors |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame^] | 136 | # deploy_and_start_sensors(cfg_dict.get('sensors'), nodes_to_run) |
koder aka kdanilov | 4ec0f71 | 2015-02-19 19:19:27 -0800 | [diff] [blame] | 137 | |
Yulia Portnova | 0e64ea2 | 2015-03-20 17:27:22 +0200 | [diff] [blame] | 138 | for test_name, opts in tests.items(): |
| 139 | cmd_line = " ".join(opts['opts']) |
| 140 | logger.debug("Run test with {0!r} params".format(cmd_line)) |
| 141 | latest_start_time = 300 + time.time() |
| 142 | uris = [node.connection_url for node in nodes_to_run] |
| 143 | runner = ssh_runner.get_ssh_runner(uris, |
| 144 | latest_start_time, |
| 145 | opts.get('keep_temp_files')) |
| 146 | res = run_io_test(test_name, |
| 147 | opts['opts'], |
| 148 | runner, |
| 149 | opts.get('keep_temp_files')) |
| 150 | logger.debug(format_result(res, get_formatter(test_name))) |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 151 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame^] | 152 | # if cfg_dict.get('data_server_url'): |
| 153 | # result = json.loads(get_formatter(opts.tool_type)(res)) |
| 154 | # result['name'] = opts.build_name |
| 155 | # add_test(opts.build_name, result, opts.data_server_url) |
koder aka kdanilov | 4ec0f71 | 2015-02-19 19:19:27 -0800 | [diff] [blame] | 156 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 157 | return 0 |
| 158 | |
| 159 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 160 | if __name__ == '__main__': |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame^] | 161 | exit(main(sys.argv)) |