Merge "Acquire NSB specific data from Heat."
[yardstick.git] / yardstick / benchmark / runners / iteration.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 configurable number of times 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     iterations = runner_cfg.get("iterations", 1)
43     run_step = runner_cfg.get("run_step", "setup,run,teardown")
44
45     delta = runner_cfg.get("delta", 2)
46     LOG.info("worker START, iterations %d times, class %s", iterations, cls)
47
48     runner_cfg['runner_id'] = os.getpid()
49
50     benchmark = cls(scenario_cfg, context_cfg)
51     if "setup" in run_step:
52         benchmark.setup()
53
54     method = getattr(benchmark, method_name)
55
56     queue.put({'runner_id': runner_cfg['runner_id'],
57                'scenario_cfg': scenario_cfg,
58                'context_cfg': context_cfg})
59
60     sla_action = None
61     if "sla" in scenario_cfg:
62         sla_action = scenario_cfg["sla"].get("action", "assert")
63     if "run" in run_step:
64         while True:
65
66             LOG.debug("runner=%(runner)s seq=%(sequence)s START",
67                       {"runner": runner_cfg["runner_id"],
68                        "sequence": sequence})
69
70             data = {}
71             errors = ""
72
73             try:
74                 result = method(data)
75             except AssertionError as assertion:
76                 # SLA validation failed in scenario, determine what to do now
77                 if sla_action == "assert":
78                     raise
79                 elif sla_action == "monitor":
80                     LOG.warning("SLA validation failed: %s", assertion.args)
81                     errors = assertion.args
82                 elif sla_action == "rate-control":
83                     try:
84                         scenario_cfg['options']['rate']
85                     except KeyError:
86                         scenario_cfg.setdefault('options', {})
87                         scenario_cfg['options']['rate'] = 100
88
89                     scenario_cfg['options']['rate'] -= delta
90                     sequence = 1
91                     continue
92             except Exception as e:
93                 errors = traceback.format_exc()
94                 LOG.exception(e)
95             else:
96                 if result:
97                     output_queue.put(result)
98
99             time.sleep(interval)
100
101             benchmark_output = {
102                 'timestamp': time.time(),
103                 'sequence': sequence,
104                 'data': data,
105                 'errors': errors
106             }
107
108             record = {'runner_id': runner_cfg['runner_id'],
109                       'benchmark': benchmark_output}
110
111             queue.put(record)
112
113             LOG.debug("runner=%(runner)s seq=%(sequence)s END",
114                       {"runner": runner_cfg["runner_id"],
115                        "sequence": sequence})
116
117             sequence += 1
118
119             if (errors and sla_action is None) or \
120                     (sequence > iterations or aborted.is_set()):
121                 LOG.info("worker END")
122                 break
123     if "teardown" in run_step:
124         benchmark.teardown()
125
126
127 class IterationRunner(base.Runner):
128     """Run a scenario for a configurable number of times
129
130 If the scenario ends before the time has elapsed, it will be started again.
131
132   Parameters
133     iterations - amount of times the scenario will be run for
134         type:    int
135         unit:    na
136         default: 1
137     interval - time to wait between each scenario invocation
138         type:    int
139         unit:    seconds
140         default: 1 sec
141     """
142     __execution_type__ = 'Iteration'
143
144     def _run_benchmark(self, cls, method, scenario_cfg, context_cfg):
145         self.process = multiprocessing.Process(
146             target=_worker_process,
147             args=(self.result_queue, cls, method, scenario_cfg,
148                   context_cfg, self.aborted, self.output_queue))
149         self.process.start()