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, scenario_cfg, context_cfg):
28 runner_cfg = scenario_cfg['runner']
30 interval = runner_cfg.get("interval", 1)
31 duration = runner_cfg.get("duration", 60)
32 LOG.info("worker START, duration %d sec, class %s", duration, cls)
34 runner_cfg['runner_id'] = os.getpid()
36 benchmark = cls(scenario_cfg, context_cfg)
38 method = getattr(benchmark, method_name)
41 if "sla" in scenario_cfg:
42 sla_action = scenario_cfg["sla"].get("action", "assert")
44 queue.put({'runner_id': runner_cfg['runner_id'],
45 'scenario_cfg': scenario_cfg,
46 'context_cfg': context_cfg})
51 LOG.debug("runner=%(runner)s seq=%(sequence)s START" %
52 {"runner": runner_cfg["runner_id"], "sequence": sequence})
59 except AssertionError as assertion:
60 # SLA validation failed in scenario, determine what to do now
61 if sla_action == "assert":
63 elif sla_action == "monitor":
64 LOG.warning("SLA validation failed: %s" % assertion.args)
65 errors = assertion.args
66 except Exception as e:
67 errors = traceback.format_exc()
73 'timestamp': time.time(),
79 record = {'runner_id': runner_cfg['runner_id'],
80 'benchmark': benchmark_output}
84 LOG.debug("runner=%(runner)s seq=%(sequence)s END" %
85 {"runner": runner_cfg["runner_id"], "sequence": sequence})
89 if (errors and sla_action is None) or (time.time() - start > duration):
90 LOG.info("worker END")
96 class DurationRunner(base.Runner):
97 '''Run a scenario for a certain amount of time
99 If the scenario ends before the time has elapsed, it will be started again.
102 duration - amount of time the scenario will be run for
106 interval - time to wait between each scenario invocation
111 __execution_type__ = 'Duration'
113 def _run_benchmark(self, cls, method, scenario_cfg, context_cfg):
114 self.process = multiprocessing.Process(
115 target=_worker_process,
116 args=(self.result_queue, cls, method, scenario_cfg, context_cfg))