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