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
30 from yardstick.common import exceptions as y_exc
32 LOG = logging.getLogger(__name__)
35 QUEUE_PUT_TIMEOUT = 10
38 def _worker_process(queue, cls, method_name, scenario_cfg,
39 context_cfg, aborted, output_queue):
43 runner_cfg = scenario_cfg['runner']
45 interval = runner_cfg.get("interval", 1)
46 duration = runner_cfg.get("duration", 60)
47 LOG.info("Worker START, duration is %ds", duration)
48 LOG.debug("class is %s", cls)
50 runner_cfg['runner_id'] = os.getpid()
52 benchmark = cls(scenario_cfg, context_cfg)
54 method = getattr(benchmark, method_name)
57 if "sla" in scenario_cfg:
58 sla_action = scenario_cfg["sla"].get("action", "assert")
61 timeout = start + duration
64 LOG.debug("runner=%(runner)s seq=%(sequence)s START",
65 {"runner": runner_cfg["runner_id"], "sequence": sequence})
70 benchmark.pre_run_wait_time(interval)
74 except y_exc.SLAValidationError as error:
75 # SLA validation failed in scenario, determine what to do now
76 if sla_action == "assert":
78 elif sla_action == "monitor":
79 LOG.warning("SLA validation failed: %s", error.args)
81 # catch all exceptions because with multiprocessing we can have un-picklable exception
82 # problems https://bugs.python.org/issue9400
83 except Exception: # pylint: disable=broad-except
84 errors = traceback.format_exc()
88 # add timeout for put so we don't block test
89 # if we do timeout we don't care about dropping individual KPIs
90 output_queue.put(result, True, QUEUE_PUT_TIMEOUT)
92 benchmark.post_run_wait_time(interval)
95 'timestamp': time.time(),
101 queue.put(benchmark_output, True, QUEUE_PUT_TIMEOUT)
103 LOG.debug("runner=%(runner)s seq=%(sequence)s END",
104 {"runner": runner_cfg["runner_id"], "sequence": sequence})
108 if (errors and sla_action is None) or time.time() > timeout or aborted.is_set():
109 LOG.info("Worker END")
115 # catch any exception in teardown and convert to simple exception
116 # never pass exceptions back to multiprocessing, because some exceptions can
118 # https://bugs.python.org/issue9400
122 LOG.debug("queue.qsize() = %s", queue.qsize())
123 LOG.debug("output_queue.qsize() = %s", output_queue.qsize())
126 class DurationRunner(base.Runner):
127 """Run a scenario for a certain amount of time
129 If the scenario ends before the time has elapsed, it will be started again.
132 duration - amount of time the scenario will be run for
136 interval - time to wait between each scenario invocation
141 __execution_type__ = 'Duration'
143 def _run_benchmark(self, cls, method, scenario_cfg, context_cfg):
144 name = "{}-{}-{}".format(self.__execution_type__, scenario_cfg.get("type"), os.getpid())
145 self.process = multiprocessing.Process(
147 target=_worker_process,
148 args=(self.result_queue, cls, method, scenario_cfg,
149 context_cfg, self.aborted, self.output_queue))