1 ##############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
13 import multiprocessing
17 log = logging.getLogger(__name__)
19 import yardstick.common.utils as utils
20 from yardstick.benchmark.scenarios import base as base_scenario
23 def _output_serializer_main(filename, queue):
24 '''entrypoint for the singleton subprocess writing to outfile
25 Use of this process enables multiple instances of a scenario without
26 messing up the output file.
28 with open(filename, 'a+') as outfile:
30 # blocks until data becomes available
32 if record == '_TERMINATE_':
36 json.dump(record, outfile)
40 def _single_action(seconds, command, queue):
41 '''entrypoint for the single action process'''
42 log.debug("single action, fires after %d seconds (from now)", seconds)
44 log.debug("single action: executing command: '%s'", command)
45 data = subprocess.check_output(command, shell=True)
46 log.debug("\n%s" % data)
49 def _periodic_action(interval, command, queue):
50 '''entrypoint for the periodic action process'''
51 log.debug("periodic action, fires every: %d seconds", interval)
55 time_spent += interval
56 log.debug("periodic action, executing command: '%s'", command)
57 data = subprocess.check_output(command, shell=True)
58 log.debug("\n%s" % data)
67 def get_cls(runner_type):
68 '''return class of specified type'''
69 for runner in utils.itersubclasses(Runner):
70 if runner_type == runner.__execution_type__:
72 raise RuntimeError("No such runner_type %s" % runner_type)
76 '''return a list of known runner type (class) names'''
78 for runner in utils.itersubclasses(Runner):
84 """Returns instance of a scenario runner for execution type.
86 # if there is no runner, start the output serializer subprocess
87 if len(Runner.runners) == 0:
88 log.debug("Starting dump process file '%s'" %
89 config["output_filename"])
90 Runner.queue = multiprocessing.Queue()
91 Runner.dump_process = multiprocessing.Process(
92 target=_output_serializer_main,
94 args=(config["output_filename"], Runner.queue))
95 Runner.dump_process.start()
97 return Runner.get_cls(config["type"])(config, Runner.queue)
101 '''Release the runner'''
102 Runner.runners.remove(runner)
103 # if this was the last runner, stop the output serializer subprocess
104 if len(Runner.runners) == 0:
105 log.debug("Stopping dump process")
106 Runner.queue.put('_TERMINATE_')
107 Runner.dump_process.join()
111 '''Terminate all runners (subprocesses)'''
112 log.debug("Terminating all runners")
113 for runner in Runner.runners:
114 if runner.periodic_action_process:
115 log.debug("Terminating periodic action process")
116 runner.periodic_action_process.terminate()
117 runner.periodic_action_process = None
118 runner.process.terminate()
119 runner.process.join()
120 Runner.release(runner)
122 def __init__(self, config, queue):
125 self.periodic_action_process = None
126 self.result_queue = queue
127 Runner.runners.append(self)
129 def run_pre_start_action(self):
130 '''run a potentially configured pre-start action'''
131 if "pre-start-action" in self.config:
132 command = self.config["pre-start-action"]["command"]
133 log.debug("pre start action: command: '%s'" % command)
134 data = subprocess.check_output(command, shell=True)
135 log.debug("pre-start data: \n%s" % data)
136 output = "{'pre-start-action-data': %s}" % data
137 self.result_queue.put(output)
139 def run_post_stop_action(self):
140 '''run a potentially configured post-stop action'''
141 if "post-stop-action" in self.config:
142 command = self.config["post-stop-action"]["command"]
143 log.debug("post stop action: command: '%s'" % command)
144 data = subprocess.check_output(command, shell=True)
145 log.debug("post-stop data: \n%s" % data)
146 output = "{'post-stop-action-data': %s}" % data
147 self.result_queue.put(output)
149 def run(self, scenario_type, scenario_args):
150 class_name = base_scenario.Scenario.get(scenario_type)
151 path_split = class_name.split(".")
152 module_path = ".".join(path_split[:-1])
153 module = importlib.import_module(module_path)
154 cls = getattr(module, path_split[-1])
156 self.config['object'] = class_name
158 self.run_pre_start_action()
160 if "single-shot-action" in self.config:
161 single_action_process = multiprocessing.Process(
162 target=_single_action,
163 name="single-shot-action",
164 args=(self.config["single-shot-action"]["after"],
165 self.config["single-shot-action"]["command"],
167 single_action_process.start()
169 if "periodic-action" in self.config:
170 self.periodic_action_process = multiprocessing.Process(
171 target=_periodic_action,
172 name="periodic-action",
173 args=(self.config["periodic-action"]["interval"],
174 self.config["periodic-action"]["command"],
176 self.periodic_action_process.start()
178 self._run_benchmark(cls, "run", scenario_args)
182 if self.periodic_action_process:
183 self.periodic_action_process.terminate()
184 self.periodic_action_process = None
185 self.run_post_stop_action()
186 return self.process.exitcode