3 ##############################################################################
4 # Copyright (c) 2015 Ericsson AB and others.
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
12 """ yardstick - command line tool for managing benchmarks
15 $ yardstick samples/ping-task.yaml
23 from yardstick.benchmark.context.model import Context
24 from yardstick.benchmark.runners import base as base_runner
25 from yardstick.cmdparser import CmdParser
28 class TaskParser(object):
29 '''Parser for task config files in yaml format'''
30 def __init__(self, path):
34 '''parses the task file and return an context and scenario instances'''
35 print "Parsing task config:", self.path
37 with open(self.path) as stream:
38 cfg = yaml.load(stream)
39 except IOError as ioerror:
42 if cfg["schema"] != "yardstick:task:0.1":
43 sys.exit("error: file %s has unknown schema %s" % (self.path,
46 # TODO: support one or many contexts? Many would simpler and precise
48 context_cfgs = [cfg["context"]]
50 context_cfgs = cfg["contexts"]
52 for cfg_attrs in context_cfgs:
54 context.init(cfg_attrs)
56 run_in_parallel = cfg.get("run_in_parallel", False)
58 # TODO we need something better here, a class that represent the file
59 return cfg["scenarios"], run_in_parallel
63 '''handler for process termination'''
64 base_runner.Runner.terminate_all()
66 if len(Context.list) > 0:
67 print "Undeploying all contexts"
68 for context in Context.list:
72 def run_one_scenario(scenario_cfg, output_file):
73 '''run one scenario using context'''
74 key_filename = pkg_resources.resource_filename(
75 'yardstick.resources', 'files/yardstick_key')
77 host = Context.get_server(scenario_cfg["host"])
79 runner_cfg = scenario_cfg["runner"]
80 runner_cfg['host'] = host.floating_ip["ipaddr"]
81 runner_cfg['user'] = host.context.user
82 runner_cfg['key_filename'] = key_filename
83 runner_cfg['output_filename'] = output_file
85 # TODO target should be optional to support single VM scenarios
86 target = Context.get_server(scenario_cfg["target"])
87 if target.floating_ip:
88 runner_cfg['target'] = target.floating_ip["ipaddr"]
90 # TODO scenario_cfg["ipaddr"] is bad, "dest_ip" is better
91 if host.context != target.context:
92 # target is in another context, get its public IP
93 scenario_cfg["ipaddr"] = target.floating_ip["ipaddr"]
95 # TODO hardcoded name below, a server can be attached to several nets
96 scenario_cfg["ipaddr"] = target.ports["test"]["ipaddr"]
98 runner = base_runner.Runner.get(runner_cfg)
100 print "Starting runner of type '%s'" % runner_cfg["type"]
101 runner.run(scenario_cfg["type"], scenario_cfg)
106 def runner_join(runner):
107 '''join (wait for) a runner, exit process at runner failure'''
108 status = runner.join()
109 base_runner.Runner.release(runner)
111 sys.exit("Runner failed")
117 atexit.register(atexit_handler)
119 prog_args = CmdParser().parse_args()
121 parser = TaskParser(prog_args.taskfile[0])
122 scenarios, run_in_parallel = parser.parse()
124 if prog_args.parse_only:
127 for context in Context.list:
132 for scenario in scenarios:
133 runner = run_one_scenario(scenario, prog_args.output_file)
134 runners.append(runner)
136 # Wait for runners to finish
137 for runner in runners:
139 print "Runner ended, output in", prog_args.output_file
142 for scenario in scenarios:
143 runner = run_one_scenario(scenario, prog_args.output_file)
145 print "Runner ended, output in", prog_args.output_file
147 if prog_args.keep_deploy:
148 # keep deployment, forget about stack (hide it for exit handler)
151 for context in Context.list:
154 print "Done, exiting"
156 if __name__ == '__main__':