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 '''return class of specified type'''
46 for runner in utils.itersubclasses(Runner):
47 if runner_type == runner.__execution_type__:
49 raise RuntimeError("No such runner_type %s" % runner_type)
53 '''return a list of known runner type (class) names'''
55 for runner in utils.itersubclasses(Runner):
61 """Returns instance of a scenario runner for execution type.
63 # if there is no runner, start the output serializer subprocess
64 if len(Runner.runners) == 0:
65 log.debug("Starting dump process file '%s'" %
66 config["output_filename"])
67 Runner.queue = multiprocessing.Queue()
68 Runner.dump_process = multiprocessing.Process(
69 target=_output_serializer_main,
71 args=(config["output_filename"], Runner.queue))
72 Runner.dump_process.start()
74 return Runner.get_cls(config["type"])(config, Runner.queue)
78 '''Release the runner'''
79 Runner.runners.remove(runner)
80 # if this was the last runner, stop the output serializer subprocess
81 if len(Runner.runners) == 0:
82 log.debug("Stopping dump process")
83 Runner.queue.put('_TERMINATE_')
84 Runner.dump_process.join()
88 '''Terminate all runners (subprocesses)'''
89 log.debug("Terminating all runners")
90 for runner in Runner.runners:
91 runner.process.terminate()
93 Runner.release(runner)
95 def __init__(self, config, queue):
98 self.result_queue = queue
99 Runner.runners.append(self)
101 def run(self, scenario_type, scenario_args):
102 class_name = base_scenario.Scenario.get(scenario_type)
103 path_split = class_name.split(".")
104 module_path = ".".join(path_split[:-1])
105 module = importlib.import_module(module_path)
106 cls = getattr(module, path_split[-1])
108 self.config['object'] = class_name
109 self._run_benchmark(cls, "run", scenario_args)
113 return self.process.exitcode