Merge "NSB: fix port topology"
[yardstick.git] / yardstick / benchmark / runners / duration.py
1 # Copyright 2014: Mirantis Inc.
2 # All Rights Reserved.
3 #
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
7 #
8 #         http://www.apache.org/licenses/LICENSE-2.0
9 #
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
14 #    under the License.
15
16 # yardstick comment: this is a modified copy of
17 # rally/rally/benchmark/runners/constant.py
18
19 """A runner that runs a specific time before it returns
20 """
21
22 from __future__ import absolute_import
23 import os
24 import multiprocessing
25 import logging
26 import traceback
27 import time
28
29 from yardstick.benchmark.runners import base
30
31 LOG = logging.getLogger(__name__)
32
33
34 def _worker_process(queue, cls, method_name, scenario_cfg,
35                     context_cfg, aborted, output_queue):
36
37     sequence = 1
38
39     runner_cfg = scenario_cfg['runner']
40
41     interval = runner_cfg.get("interval", 1)
42     duration = runner_cfg.get("duration", 60)
43     LOG.info("Worker START, duration is %ds", duration)
44     LOG.debug("class is %s", cls)
45
46     runner_cfg['runner_id'] = os.getpid()
47
48     benchmark = cls(scenario_cfg, context_cfg)
49     benchmark.setup()
50     method = getattr(benchmark, method_name)
51
52     sla_action = None
53     if "sla" in scenario_cfg:
54         sla_action = scenario_cfg["sla"].get("action", "assert")
55
56     start = time.time()
57     while True:
58
59         LOG.debug("runner=%(runner)s seq=%(sequence)s START",
60                   {"runner": runner_cfg["runner_id"], "sequence": sequence})
61
62         data = {}
63         errors = ""
64
65         try:
66             result = method(data)
67         except AssertionError as assertion:
68             # SLA validation failed in scenario, determine what to do now
69             if sla_action == "assert":
70                 raise
71             elif sla_action == "monitor":
72                 LOG.warning("SLA validation failed: %s", assertion.args)
73                 errors = assertion.args
74         except Exception as e:
75             errors = traceback.format_exc()
76             LOG.exception(e)
77         else:
78             if result:
79                 output_queue.put(result)
80
81         time.sleep(interval)
82
83         benchmark_output = {
84             'timestamp': time.time(),
85             'sequence': sequence,
86             'data': data,
87             'errors': errors
88         }
89
90         queue.put(benchmark_output)
91
92         LOG.debug("runner=%(runner)s seq=%(sequence)s END",
93                   {"runner": runner_cfg["runner_id"], "sequence": sequence})
94
95         sequence += 1
96
97         if (errors and sla_action is None) or \
98                 (time.time() - start > duration or aborted.is_set()):
99             LOG.info("Worker END")
100             break
101
102     benchmark.teardown()
103
104
105 class DurationRunner(base.Runner):
106     """Run a scenario for a certain amount of time
107
108 If the scenario ends before the time has elapsed, it will be started again.
109
110   Parameters
111     duration - amount of time the scenario will be run for
112         type:    int
113         unit:    seconds
114         default: 1 sec
115     interval - time to wait between each scenario invocation
116         type:    int
117         unit:    seconds
118         default: 1 sec
119     """
120     __execution_type__ = 'Duration'
121
122     def _run_benchmark(self, cls, method, scenario_cfg, context_cfg):
123         self.process = multiprocessing.Process(
124             target=_worker_process,
125             args=(self.result_queue, cls, method, scenario_cfg,
126                   context_cfg, self.aborted, self.output_queue))
127         self.process.start()