1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd 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 configurable number of times 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,
25 context_cfg, aborted):
29 runner_cfg = scenario_cfg['runner']
31 interval = runner_cfg.get("interval", 1)
32 iterations = runner_cfg.get("iterations", 1)
33 run_step = runner_cfg.get("run_step", "setup,run,teardown")
34 LOG.info("worker START, iterations %d times, class %s", iterations, cls)
36 runner_cfg['runner_id'] = os.getpid()
38 benchmark = cls(scenario_cfg, context_cfg)
39 if "setup" in run_step:
42 method = getattr(benchmark, method_name)
44 queue.put({'runner_id': runner_cfg['runner_id'],
45 'scenario_cfg': scenario_cfg,
46 'context_cfg': context_cfg})
49 if "sla" in scenario_cfg:
50 sla_action = scenario_cfg["sla"].get("action", "assert")
54 LOG.debug("runner=%(runner)s seq=%(sequence)s START" %
55 {"runner": runner_cfg["runner_id"],
56 "sequence": sequence})
63 except AssertionError as assertion:
64 # SLA validation failed in scenario, determine what to do now
65 if sla_action == "assert":
67 elif sla_action == "monitor":
68 LOG.warning("SLA validation failed: %s" % assertion.args)
69 errors = assertion.args
70 except Exception as e:
71 errors = traceback.format_exc()
77 'timestamp': time.time(),
83 record = {'runner_id': runner_cfg['runner_id'],
84 'benchmark': benchmark_output}
88 LOG.debug("runner=%(runner)s seq=%(sequence)s END" %
89 {"runner": runner_cfg["runner_id"],
90 "sequence": sequence})
94 if (errors and sla_action is None) or \
95 (sequence > iterations or aborted.is_set()):
96 LOG.info("worker END")
98 if "teardown" in run_step:
102 class IterationRunner(base.Runner):
103 '''Run a scenario for a configurable number of times
105 If the scenario ends before the time has elapsed, it will be started again.
108 iterations - amount of times the scenario will be run for
112 interval - time to wait between each scenario invocation
117 __execution_type__ = 'Iteration'
119 def _run_benchmark(self, cls, method, scenario_cfg, context_cfg):
120 self.process = multiprocessing.Process(
121 target=_worker_process,
122 args=(self.result_queue, cls, method, scenario_cfg,
123 context_cfg, self.aborted))