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 | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 9 | import traceback |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 10 | |
| 11 | import io_scenario |
| 12 | from itest import IOPerfTest |
| 13 | from log import setlogger |
| 14 | |
| 15 | import ssh_runner |
| 16 | import rally_runner |
| 17 | |
| 18 | from starts_vms import nova_connect, create_vms_mt, clear_all |
| 19 | |
| 20 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame^] | 21 | logger = logging.getLogger("io-perf-tool") |
| 22 | logger.setLevel(logging.DEBUG) |
| 23 | ch = logging.StreamHandler() |
| 24 | ch.setLevel(logging.DEBUG) |
| 25 | logger.addHandler(ch) |
| 26 | |
| 27 | log_format = '%(asctime)s - %(levelname)s - %(name)s - %(message)s' |
| 28 | formatter = logging.Formatter(log_format, |
| 29 | "%H:%M:%S") |
| 30 | ch.setFormatter(formatter) |
| 31 | |
| 32 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 33 | def run_io_test(tool, |
| 34 | script_args, |
| 35 | test_runner, |
| 36 | keep_temp_files=False): |
| 37 | |
| 38 | files_dir = os.path.dirname(io_scenario.__file__) |
| 39 | |
| 40 | path = 'iozone' if 'iozone' == tool else 'fio' |
| 41 | src_testtool_path = os.path.join(files_dir, path) |
| 42 | |
| 43 | obj = IOPerfTest(script_args, |
| 44 | src_testtool_path, |
| 45 | None, |
| 46 | keep_temp_files) |
| 47 | |
| 48 | return test_runner(obj) |
| 49 | |
| 50 | |
| 51 | def parse_args(argv): |
| 52 | parser = argparse.ArgumentParser( |
| 53 | description="Run disk io performance test") |
| 54 | |
| 55 | parser.add_argument("tool_type", help="test tool type", |
| 56 | choices=['iozone', 'fio']) |
| 57 | |
| 58 | parser.add_argument("-l", dest='extra_logs', |
| 59 | action='store_true', default=False, |
| 60 | help="print some extra log info") |
| 61 | |
| 62 | parser.add_argument("-o", "--io-opts", dest='io_opts', |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 63 | help="cmd line options for io.py") |
| 64 | |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 65 | parser.add_argument("-f", "--io-opts-file", dest='io_opts_file', |
| 66 | type=argparse.FileType('r'), default=None, |
| 67 | help="file with cmd line options for io.py") |
| 68 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 69 | parser.add_argument("-t", "--test-directory", help="directory with test", |
| 70 | dest="test_directory", required=True) |
| 71 | |
| 72 | parser.add_argument("--max-preparation-time", default=300, |
| 73 | type=int, dest="max_preparation_time") |
| 74 | |
| 75 | parser.add_argument("-k", "--keep", default=False, |
| 76 | help="keep temporary files", |
| 77 | dest="keep_temp_files", action='store_true') |
| 78 | |
| 79 | parser.add_argument("--runner", required=True, |
| 80 | choices=["ssh", "rally"], |
| 81 | help="runner type") |
| 82 | |
| 83 | parser.add_argument("--runner-extra-opts", default="", |
| 84 | dest="runner_opts", help="runner extra options") |
| 85 | |
| 86 | return parser.parse_args(argv) |
| 87 | |
| 88 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame^] | 89 | def format_measurements_stat(res): |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 90 | if len(res) != 0: |
| 91 | bw_mean = 0.0 |
| 92 | for measurement in res: |
| 93 | bw_mean += measurement["bw_mean"] |
| 94 | |
| 95 | bw_mean /= len(res) |
| 96 | |
| 97 | it = ((bw_mean - measurement["bw_mean"]) ** 2 for measurement in res) |
| 98 | bw_dev = sum(it) ** 0.5 |
| 99 | |
| 100 | meta = res[0]['__meta__'] |
| 101 | key = "{0} {1} {2}k".format(meta['action'], |
| 102 | 's' if meta['sync'] else 'a', |
| 103 | meta['blocksize']) |
| 104 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame^] | 105 | data = json.dumps({key: (int(bw_mean), int(bw_dev))}) |
| 106 | |
| 107 | return "\n====> {0}\n\n{1}\n".format(data, "=" * 80) |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 108 | |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 109 | |
| 110 | def get_io_opts(io_opts_file, io_opts): |
| 111 | if io_opts_file is not None and io_opts is not None: |
| 112 | print "Options --io-opts-file and --io-opts can't be " + \ |
| 113 | "provided same time" |
| 114 | exit(1) |
| 115 | |
| 116 | if io_opts_file is None and io_opts is None: |
| 117 | print "Either --io-opts-file or --io-opts should " + \ |
| 118 | "be provided" |
| 119 | exit(1) |
| 120 | |
| 121 | if io_opts_file is not None: |
| 122 | io_opts = [] |
| 123 | |
| 124 | opt_lines = io_opts_file.readlines() |
| 125 | opt_lines = [i for i in opt_lines if i != "" and not i.startswith("#")] |
| 126 | |
| 127 | for opt_line in opt_lines: |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame^] | 128 | if opt_line.strip() != "": |
| 129 | io_opts.append([opt.strip() |
| 130 | for opt in opt_line.strip().split(" ") |
| 131 | if opt.strip() != ""]) |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 132 | else: |
| 133 | io_opts = [[opt.strip() |
| 134 | for opt in io_opts.split(" ") |
| 135 | if opt.strip() != ""]] |
| 136 | |
| 137 | if len(io_opts) == 0: |
| 138 | print "Can't found parameters for io. Check" + \ |
| 139 | "--io-opts-file or --io-opts options" |
| 140 | exit(1) |
| 141 | |
| 142 | return io_opts |
| 143 | |
| 144 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame^] | 145 | def format_result(res): |
| 146 | data = "\n{0}\n".format("=" * 80) |
| 147 | data += pprint.pformat(res) + "\n" |
| 148 | data += "{0}\n".format("=" * 80) |
| 149 | return data + "\n" + format_measurements_stat(res) + "\n" |
| 150 | |
| 151 | |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 152 | def main(argv): |
| 153 | opts = parse_args(argv) |
| 154 | |
| 155 | if not opts.extra_logs: |
| 156 | def nolog(x): |
| 157 | pass |
| 158 | |
| 159 | setlogger(nolog) |
| 160 | |
| 161 | io_opts = get_io_opts(opts.io_opts_file, opts.io_opts) |
| 162 | |
| 163 | if opts.runner == "rally": |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame^] | 164 | logger.debug("Use rally runner") |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 165 | for script_args in io_opts: |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame^] | 166 | |
| 167 | cmd_line = " ".join(script_args) |
| 168 | logger.debug("Run test with {0!r} params".format(cmd_line)) |
| 169 | |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 170 | runner = rally_runner.get_rally_runner( |
| 171 | files_dir=os.path.dirname(io_scenario.__file__), |
| 172 | rally_extra_opts=opts.runner_opts.split(" "), |
| 173 | max_preparation_time=opts.max_preparation_time, |
| 174 | keep_temp_files=opts.keep_temp_files) |
| 175 | |
| 176 | res = run_io_test(opts.tool_type, |
| 177 | script_args, |
| 178 | runner, |
| 179 | opts.keep_temp_files) |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame^] | 180 | logger.debug(format_result(res)) |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 181 | |
| 182 | elif opts.runner == "ssh": |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame^] | 183 | logger.debug("Use ssh runner") |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 184 | create_vms_opts = {} |
| 185 | for opt in opts.runner_opts.split(","): |
| 186 | name, val = opt.split("=", 1) |
| 187 | create_vms_opts[name] = val |
| 188 | |
| 189 | user = create_vms_opts.pop("user") |
| 190 | key_file = create_vms_opts.pop("key_file") |
| 191 | aff_group = create_vms_opts.pop("aff_group", None) |
| 192 | raw_count = create_vms_opts.pop("count", "x1") |
| 193 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame^] | 194 | logger.debug("Connection to nova") |
koder aka kdanilov | ec53033 | 2015-02-13 11:12:53 -0800 | [diff] [blame] | 195 | nova = nova_connect() |
| 196 | |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 197 | if raw_count.startswith("x"): |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame^] | 198 | logger.debug("Getting amount of compute services") |
koder aka kdanilov | ec53033 | 2015-02-13 11:12:53 -0800 | [diff] [blame] | 199 | count = len(nova.services.list(binary="nova-compute")) |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame^] | 200 | count *= int(raw_count[1:]) |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 201 | else: |
| 202 | count = int(raw_count) |
| 203 | |
| 204 | if aff_group is not None: |
| 205 | scheduler_hints = {'group': aff_group} |
| 206 | else: |
| 207 | scheduler_hints = None |
| 208 | |
| 209 | create_vms_opts['scheduler_hints'] = scheduler_hints |
| 210 | |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 211 | # nova, amount, keypair_name, img_name, |
| 212 | # flavor_name, vol_sz=None, network_zone_name=None, |
| 213 | # flt_ip_pool=None, name_templ='ceph-test-{}', |
| 214 | # scheduler_hints=None |
| 215 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame^] | 216 | logger.debug("Will start {} vms".format(count)) |
| 217 | |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 218 | try: |
| 219 | ips = [i[0] for i in create_vms_mt(nova, count, **create_vms_opts)] |
| 220 | |
| 221 | uris = ["{0}@{1}::{2}".format(user, ip, key_file) for ip in ips] |
| 222 | |
| 223 | for script_args in io_opts: |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame^] | 224 | cmd_line = " ".join(script_args) |
| 225 | logger.debug("Run test with {0!r} params".format(cmd_line)) |
| 226 | latest_start_time = opts.max_preparation_time + time.time() |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 227 | runner = ssh_runner.get_ssh_runner(uris, |
| 228 | latest_start_time, |
| 229 | opts.keep_temp_files) |
| 230 | res = run_io_test(opts.tool_type, |
| 231 | script_args, |
| 232 | runner, |
| 233 | opts.keep_temp_files) |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame^] | 234 | logger.debug(format_result(res)) |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 235 | |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 236 | except: |
| 237 | traceback.print_exc() |
| 238 | finally: |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame^] | 239 | logger.debug("Clearing") |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 240 | clear_all(nova) |
| 241 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 242 | return 0 |
| 243 | |
| 244 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 245 | if __name__ == '__main__': |
| 246 | exit(main(sys.argv[1:])) |