1 # Copyright 2014: Mirantis Inc.
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
16 # yardstick comment: this is a modified copy of
17 # rally/rally/benchmark/runners/base.py
19 from __future__ import absolute_import
22 import multiprocessing
27 import yardstick.common.utils as utils
28 from yardstick.benchmark.scenarios import base as base_scenario
30 log = logging.getLogger(__name__)
33 def _execute_shell_command(command):
34 """execute shell script with error handling"""
38 output = subprocess.check_output(command, shell=True)
41 output = traceback.format_exc()
42 log.error("exec command '%s' error:\n ", command)
43 log.error(traceback.format_exc())
45 return exitcode, output
48 def _single_action(seconds, command, queue):
49 """entrypoint for the single action process"""
50 log.debug("single action, fires after %d seconds (from now)", seconds)
52 log.debug("single action: executing command: '%s'", command)
53 ret_code, data = _execute_shell_command(command)
55 log.error("single action error! command:%s", command)
56 queue.put({'single-action-data': data})
58 log.debug("single action data: \n%s", data)
59 queue.put({'single-action-data': data})
62 def _periodic_action(interval, command, queue):
63 """entrypoint for the periodic action process"""
64 log.debug("periodic action, fires every: %d seconds", interval)
68 time_spent += interval
69 log.debug("periodic action, executing command: '%s'", command)
70 ret_code, data = _execute_shell_command(command)
72 log.error("periodic action error! command:%s", command)
73 queue.put({'periodic-action-data': data})
75 log.debug("periodic action data: \n%s", data)
76 queue.put({'periodic-action-data': data})
83 def get_cls(runner_type):
84 """return class of specified type"""
85 for runner in utils.itersubclasses(Runner):
86 if runner_type == runner.__execution_type__:
88 raise RuntimeError("No such runner_type %s" % runner_type)
92 """return a list of known runner type (class) names"""
94 for runner in utils.itersubclasses(Runner):
100 """Returns instance of a scenario runner for execution type.
102 return Runner.get_cls(runner_cfg["type"])(runner_cfg)
106 """Release the runner"""
107 if runner in Runner.runners:
108 Runner.runners.remove(runner)
111 def terminate(runner):
112 """Terminate the runner"""
113 if runner.process and runner.process.is_alive():
114 runner.process.terminate()
118 """Terminate all runners (subprocesses)"""
119 log.debug("Terminating all runners")
121 # release dumper process as some errors before any runner is created
122 if not Runner.runners:
125 for runner in Runner.runners:
126 log.debug("Terminating runner: %s", runner)
128 runner.process.terminate()
129 runner.process.join()
130 if runner.periodic_action_process:
131 log.debug("Terminating periodic action process")
132 runner.periodic_action_process.terminate()
133 runner.periodic_action_process = None
134 Runner.release(runner)
136 def __init__(self, config):
138 self.periodic_action_process = None
139 self.output_queue = multiprocessing.Queue()
140 self.result_queue = multiprocessing.Queue()
142 self.aborted = multiprocessing.Event()
143 Runner.runners.append(self)
145 def run_post_stop_action(self):
146 """run a potentially configured post-stop action"""
147 if "post-stop-action" in self.config:
148 command = self.config["post-stop-action"]["command"]
149 log.debug("post stop action: command: '%s'", command)
150 ret_code, data = _execute_shell_command(command)
152 log.error("post action error! command:%s", command)
153 self.result_queue.put({'post-stop-action-data': data})
155 log.debug("post-stop data: \n%s", data)
156 self.result_queue.put({'post-stop-action-data': data})
158 def run(self, scenario_cfg, context_cfg):
159 scenario_type = scenario_cfg["type"]
160 class_name = base_scenario.Scenario.get(scenario_type)
161 path_split = class_name.split(".")
162 module_path = ".".join(path_split[:-1])
163 module = importlib.import_module(module_path)
164 cls = getattr(module, path_split[-1])
166 self.config['object'] = class_name
169 # run a potentially configured pre-start action
170 if "pre-start-action" in self.config:
171 command = self.config["pre-start-action"]["command"]
172 log.debug("pre start action: command: '%s'", command)
173 ret_code, data = _execute_shell_command(command)
175 log.error("pre-start action error! command:%s", command)
176 self.result_queue.put({'pre-start-action-data': data})
178 log.debug("pre-start data: \n%s", data)
179 self.result_queue.put({'pre-start-action-data': data})
181 if "single-shot-action" in self.config:
182 single_action_process = multiprocessing.Process(
183 target=_single_action,
184 name="single-shot-action",
185 args=(self.config["single-shot-action"]["after"],
186 self.config["single-shot-action"]["command"],
188 single_action_process.start()
190 if "periodic-action" in self.config:
191 self.periodic_action_process = multiprocessing.Process(
192 target=_periodic_action,
193 name="periodic-action",
194 args=(self.config["periodic-action"]["interval"],
195 self.config["periodic-action"]["command"],
197 self.periodic_action_process.start()
199 self._run_benchmark(cls, "run", scenario_cfg, context_cfg)
202 """Abort the execution of a scenario"""
205 def join(self, timeout=None):
206 self.process.join(timeout)
207 if self.periodic_action_process:
208 self.periodic_action_process.terminate()
209 self.periodic_action_process = None
211 self.run_post_stop_action()
212 return self.process.exitcode
214 def get_output(self):
216 while not self.output_queue.empty():
217 result.update(self.output_queue.get())
220 def get_result(self):
222 while not self.result_queue.empty():
223 result.append(self.result_queue.get())