add yardstick iruya 9.0.0 release notes
[yardstick.git] / yardstick / benchmark / runners / sequence.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 every run changes a specified input value to the scenario.
20 The input value in the sequence is specified in a list in the input file.
21 """
22
23 from __future__ import absolute_import
24
25 import logging
26 import multiprocessing
27 import time
28 import traceback
29
30 import os
31
32 from yardstick.benchmark.runners import base
33 from yardstick.common import exceptions as y_exc
34
35 LOG = logging.getLogger(__name__)
36
37
38 def _worker_process(queue, cls, method_name, scenario_cfg,
39                     context_cfg, aborted, output_queue):
40
41     runner_cfg = scenario_cfg['runner']
42
43     interval = runner_cfg.get("interval", 1)
44     arg_name = runner_cfg.get('scenario_option_name')
45     sequence_values = runner_cfg.get('sequence')
46
47     if 'options' not in scenario_cfg:
48         scenario_cfg['options'] = {}
49
50     options = scenario_cfg['options']
51
52     runner_cfg['runner_id'] = os.getpid()
53
54     LOG.info("worker START, sequence_values(%s, %s), class %s",
55              arg_name, sequence_values, cls)
56
57     scenario_output = base.ScenarioOutput(queue, sequence=1, errors="")
58     benchmark = cls(scenario_cfg, context_cfg)
59     benchmark.setup()
60     method = getattr(benchmark, method_name)
61
62     sla_action = None
63     if "sla" in scenario_cfg:
64         sla_action = scenario_cfg["sla"].get("action", "assert")
65
66     for value in sequence_values:
67         options[arg_name] = value
68
69         LOG.debug("runner=%(runner)s seq=%(sequence)s START",
70                   {"runner": runner_cfg["runner_id"],
71                    "sequence": scenario_output.sequence})
72
73         scenario_output.clear()
74         scenario_output.errors = ""
75
76         try:
77             result = method(scenario_output)
78         except y_exc.SLAValidationError as error:
79             # SLA validation failed in scenario, determine what to do now
80             if sla_action == "assert":
81                 raise
82             elif sla_action == "monitor":
83                 LOG.warning("SLA validation failed: %s", error.args)
84                 scenario_output.errors = error.args
85         except Exception as e:  # pylint: disable=broad-except
86             scenario_output.errors = traceback.format_exc()
87             LOG.exception(e)
88         else:
89             if result:
90                 output_queue.put(result)
91
92         time.sleep(interval)
93
94         if scenario_output:
95             scenario_output.push()
96
97         LOG.debug("runner=%(runner)s seq=%(sequence)s END",
98                   {"runner": runner_cfg["runner_id"],
99                    "sequence": scenario_output.sequence})
100
101         scenario_output.sequence += 1
102
103         if (scenario_output.errors and sla_action is None) or aborted.is_set():
104             break
105
106     try:
107         benchmark.teardown()
108     except Exception:
109         # catch any exception in teardown and convert to simple exception
110         # never pass exceptions back to multiprocessing, because some exceptions can
111         # be unpicklable
112         # https://bugs.python.org/issue9400
113         LOG.exception("")
114         raise SystemExit(1)
115     LOG.info("worker END")
116     LOG.debug("queue.qsize() = %s", queue.qsize())
117     LOG.debug("output_queue.qsize() = %s", output_queue.qsize())
118
119
120 class SequenceRunner(base.Runner):
121     """Run a scenario by changing an input value defined in a list
122
123   Parameters
124     interval - time to wait between each scenario invocation
125         type:    int
126         unit:    seconds
127         default: 1 sec
128     scenario_option_name - name of the option that is increased each invocation
129         type:    string
130         unit:    na
131         default: none
132     sequence - list of values which are executed in their respective scenarios
133         type:    [int]
134         unit:    na
135         default: none
136     """
137
138     __execution_type__ = 'Sequence'
139
140     def _run_benchmark(self, cls, method, scenario_cfg, context_cfg):
141         name = "{}-{}-{}".format(self.__execution_type__, scenario_cfg.get("type"), os.getpid())
142         self.process = multiprocessing.Process(
143             name=name,
144             target=_worker_process,
145             args=(self.result_queue, cls, method, scenario_cfg,
146                   context_cfg, self.aborted, self.output_queue))
147         self.process.start()