1 # Copyright 2014: Mirantis Inc.
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
16 # yardstick comment: this is a modified copy of
17 # rally/rally/benchmark/runners/constant.py
19 """A runner that runs a specific time before it returns
22 from __future__ import absolute_import
24 import multiprocessing
29 from yardstick.benchmark.runners import base
31 LOG = logging.getLogger(__name__)
34 def _worker_process(queue, cls, method_name, scenario_cfg,
35 context_cfg, aborted, output_queue):
39 # if we don't do this we can hang waiting for the queue to drain
40 # have to do this in the subprocess
41 queue.cancel_join_thread()
42 output_queue.cancel_join_thread()
44 runner_cfg = scenario_cfg['runner']
46 interval = runner_cfg.get("interval", 1)
47 duration = runner_cfg.get("duration", 60)
48 LOG.info("Worker START, duration is %ds", duration)
49 LOG.debug("class is %s", cls)
51 runner_cfg['runner_id'] = os.getpid()
53 benchmark = cls(scenario_cfg, context_cfg)
55 method = getattr(benchmark, method_name)
58 if "sla" in scenario_cfg:
59 sla_action = scenario_cfg["sla"].get("action", "assert")
62 timeout = start + duration
65 LOG.debug("runner=%(runner)s seq=%(sequence)s START",
66 {"runner": runner_cfg["runner_id"], "sequence": sequence})
73 except AssertionError as assertion:
74 # SLA validation failed in scenario, determine what to do now
75 if sla_action == "assert":
77 elif sla_action == "monitor":
78 LOG.warning("SLA validation failed: %s", assertion.args)
79 errors = assertion.args
80 # catch all exceptions because with multiprocessing we can have un-picklable exception
81 # problems https://bugs.python.org/issue9400
83 errors = traceback.format_exc()
87 output_queue.put(result)
92 'timestamp': time.time(),
98 queue.put(benchmark_output)
100 LOG.debug("runner=%(runner)s seq=%(sequence)s END",
101 {"runner": runner_cfg["runner_id"], "sequence": sequence})
105 if (errors and sla_action is None) or time.time() > timeout or aborted.is_set():
106 LOG.info("Worker END")
112 # catch any exception in teardown and convert to simple exception
113 # never pass exceptions back to multiprocessing, because some exceptions can
115 # https://bugs.python.org/issue9400
120 class DurationRunner(base.Runner):
121 """Run a scenario for a certain amount of time
123 If the scenario ends before the time has elapsed, it will be started again.
126 duration - amount of time the scenario will be run for
130 interval - time to wait between each scenario invocation
135 __execution_type__ = 'Duration'
137 def _run_benchmark(self, cls, method, scenario_cfg, context_cfg):
138 self.process = multiprocessing.Process(
139 target=_worker_process,
140 args=(self.result_queue, cls, method, scenario_cfg,
141 context_cfg, self.aborted, self.output_queue))