blob: 2d0c2240727d199922b41575c9de6c3c79f2280d [file] [log] [blame]
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -08001import os
2import sys
3import json
4import time
5import pprint
koder aka kdanilove21d7472015-02-14 19:02:04 -08006import logging
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -08007import os.path
8import argparse
koder aka kdanilov3f356262015-02-13 08:06:14 -08009import traceback
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080010
11import io_scenario
12from itest import IOPerfTest
13from log import setlogger
14
15import ssh_runner
16import rally_runner
17
18from starts_vms import nova_connect, create_vms_mt, clear_all
19
20
koder aka kdanilove21d7472015-02-14 19:02:04 -080021logger = logging.getLogger("io-perf-tool")
22logger.setLevel(logging.DEBUG)
23ch = logging.StreamHandler()
24ch.setLevel(logging.DEBUG)
25logger.addHandler(ch)
26
27log_format = '%(asctime)s - %(levelname)s - %(name)s - %(message)s'
28formatter = logging.Formatter(log_format,
29 "%H:%M:%S")
30ch.setFormatter(formatter)
31
32
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080033def 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
51def 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 kdanilov7acd6bd2015-02-12 14:28:30 -080063 help="cmd line options for io.py")
64
koder aka kdanilov3f356262015-02-13 08:06:14 -080065 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 kdanilov7acd6bd2015-02-12 14:28:30 -080069 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 kdanilove21d7472015-02-14 19:02:04 -080089def format_measurements_stat(res):
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080090 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 kdanilove21d7472015-02-14 19:02:04 -0800105 data = json.dumps({key: (int(bw_mean), int(bw_dev))})
106
107 return "\n====> {0}\n\n{1}\n".format(data, "=" * 80)
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800108
koder aka kdanilov3f356262015-02-13 08:06:14 -0800109
110def 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 kdanilove21d7472015-02-14 19:02:04 -0800128 if opt_line.strip() != "":
129 io_opts.append([opt.strip()
130 for opt in opt_line.strip().split(" ")
131 if opt.strip() != ""])
koder aka kdanilov3f356262015-02-13 08:06:14 -0800132 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 kdanilove21d7472015-02-14 19:02:04 -0800145def 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 kdanilov3f356262015-02-13 08:06:14 -0800152def 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 kdanilove21d7472015-02-14 19:02:04 -0800164 logger.debug("Use rally runner")
koder aka kdanilov3f356262015-02-13 08:06:14 -0800165 for script_args in io_opts:
koder aka kdanilove21d7472015-02-14 19:02:04 -0800166
167 cmd_line = " ".join(script_args)
168 logger.debug("Run test with {0!r} params".format(cmd_line))
169
koder aka kdanilov3f356262015-02-13 08:06:14 -0800170 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 kdanilove21d7472015-02-14 19:02:04 -0800180 logger.debug(format_result(res))
koder aka kdanilov3f356262015-02-13 08:06:14 -0800181
182 elif opts.runner == "ssh":
koder aka kdanilove21d7472015-02-14 19:02:04 -0800183 logger.debug("Use ssh runner")
koder aka kdanilov3f356262015-02-13 08:06:14 -0800184 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 kdanilove21d7472015-02-14 19:02:04 -0800194 logger.debug("Connection to nova")
koder aka kdanilovec530332015-02-13 11:12:53 -0800195 nova = nova_connect()
196
koder aka kdanilov3f356262015-02-13 08:06:14 -0800197 if raw_count.startswith("x"):
koder aka kdanilove21d7472015-02-14 19:02:04 -0800198 logger.debug("Getting amount of compute services")
koder aka kdanilovec530332015-02-13 11:12:53 -0800199 count = len(nova.services.list(binary="nova-compute"))
koder aka kdanilove21d7472015-02-14 19:02:04 -0800200 count *= int(raw_count[1:])
koder aka kdanilov3f356262015-02-13 08:06:14 -0800201 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 kdanilov3f356262015-02-13 08:06:14 -0800211 # 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 kdanilove21d7472015-02-14 19:02:04 -0800216 logger.debug("Will start {} vms".format(count))
217
koder aka kdanilov3f356262015-02-13 08:06:14 -0800218 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 kdanilove21d7472015-02-14 19:02:04 -0800224 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 kdanilov3f356262015-02-13 08:06:14 -0800227 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 kdanilove21d7472015-02-14 19:02:04 -0800234 logger.debug(format_result(res))
koder aka kdanilov3f356262015-02-13 08:06:14 -0800235
koder aka kdanilov3f356262015-02-13 08:06:14 -0800236 except:
237 traceback.print_exc()
238 finally:
koder aka kdanilove21d7472015-02-14 19:02:04 -0800239 logger.debug("Clearing")
koder aka kdanilov3f356262015-02-13 08:06:14 -0800240 clear_all(nova)
241
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800242 return 0
243
244
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -0800245if __name__ == '__main__':
246 exit(main(sys.argv[1:]))