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 ##############################################################################
11 import multiprocessing
15 log = logging.getLogger(__name__)
17 import yardstick.common.utils as utils
18 from yardstick.benchmark.scenarios import base as base_scenario
21 def _output_serializer_main(filename, queue):
22 '''entrypoint for the singleton subprocess writing to outfile
23 Use of this process enables multiple instances of a scenario without
24 messing up the output file.
26 with open(filename, 'w') as outfile:
28 # blocks until data becomes available
30 if record == '_TERMINATE_':
34 json.dump(record, outfile)
44 def _get_cls(runner_type):
45 for runner in utils.itersubclasses(Runner):
46 if runner_type == runner.__execution_type__:
48 raise RuntimeError("No such runner_type %s" % runner_type)
52 """Returns instance of a scenario runner for execution type.
54 # if there is no runner, start the output serializer subprocess
55 if len(Runner.runners) == 0:
56 log.debug("Starting dump process file '%s'" %
57 config["output_filename"])
58 Runner.queue = multiprocessing.Queue()
59 Runner.dump_process = multiprocessing.Process(
60 target=_output_serializer_main,
62 args=(config["output_filename"], Runner.queue))
63 Runner.dump_process.start()
65 return Runner._get_cls(config["type"])(config, Runner.queue)
69 '''Release the runner'''
70 Runner.runners.remove(runner)
71 # if this was the last runner, stop the output serializer subprocess
72 if len(Runner.runners) == 0:
73 log.debug("Stopping dump process")
74 Runner.queue.put('_TERMINATE_')
75 Runner.dump_process.join()
79 '''Terminate all runners (subprocesses)'''
80 log.debug("Terminating all runners")
81 for runner in Runner.runners:
82 runner.process.terminate()
84 Runner.release(runner)
86 def __init__(self, config, queue):
89 self.result_queue = queue
90 Runner.runners.append(self)
92 def run(self, scenario_type, scenario_args):
93 class_name = base_scenario.Scenario.get(scenario_type)
94 path_split = class_name.split(".")
95 module_path = ".".join(path_split[:-1])
96 module = importlib.import_module(module_path)
97 cls = getattr(module, path_split[-1])
99 self.config['object'] = class_name
100 self._run_benchmark(cls, "run", scenario_args)
104 return self.process.exitcode