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 ##############################################################################
10 '''A runner that runs a specific time before it returns
14 import multiprocessing
19 from yardstick.benchmark.runners import base
21 LOG = logging.getLogger(__name__)
24 def _worker_process(queue, cls, method_name, context, scenario_args):
28 interval = context.get("interval", 1)
29 duration = context.get("duration", 60)
30 LOG.info("worker START, duration %d sec, class %s", duration, cls)
32 context['runner'] = os.getpid()
34 benchmark = cls(context)
36 method = getattr(benchmark, method_name)
38 record_context = {"runner": context["runner"],
39 "host": context["host"]}
42 if "sla" in scenario_args:
43 sla_action = scenario_args["sla"].get("action", "assert")
48 LOG.debug("runner=%(runner)s seq=%(sequence)s START" %
49 {"runner": context["runner"], "sequence": sequence})
55 data = method(scenario_args)
56 except AssertionError as assertion:
57 # SLA validation failed in scenario, determine what to do now
58 if sla_action == "assert":
60 elif sla_action == "monitor":
61 LOG.warning("SLA validation failed: %s" % assertion.args)
62 errors = assertion.args
63 except Exception as e:
64 errors = traceback.format_exc()
70 'timestamp': time.time(),
76 queue.put({'context': record_context, 'sargs': scenario_args,
77 'benchmark': benchmark_output})
79 LOG.debug("runner=%(runner)s seq=%(sequence)s END" %
80 {"runner": context["runner"], "sequence": sequence})
84 if (errors and sla_action is None) or (time.time() - start > duration):
85 LOG.info("worker END")
91 class DurationRunner(base.Runner):
92 '''Run a scenario for a certain amount of time
94 If the scenario ends before the time has elapsed, it will be started again.
97 duration - amount of time the scenario will be run for
101 interval - time to wait between each scenario invocation
106 __execution_type__ = 'Duration'
108 def _run_benchmark(self, cls, method, scenario_args):
109 self.process = multiprocessing.Process(
110 target=_worker_process,
111 args=(self.result_queue, cls, method, self.config, scenario_args))