Merge "NSB PROX test hang fixes"
[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
34 LOG = logging.getLogger(__name__)
35
36
37 def _worker_process(queue, cls, method_name, scenario_cfg,
38                     context_cfg, aborted, output_queue):
39
40     sequence = 1
41
42     runner_cfg = scenario_cfg['runner']
43
44     interval = runner_cfg.get("interval", 1)
45     arg_name = runner_cfg.get('scenario_option_name')
46     sequence_values = runner_cfg.get('sequence')
47
48     if 'options' not in scenario_cfg:
49         scenario_cfg['options'] = {}
50
51     options = scenario_cfg['options']
52
53     runner_cfg['runner_id'] = os.getpid()
54
55     LOG.info("worker START, sequence_values(%s, %s), class %s",
56              arg_name, sequence_values, cls)
57
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"], "sequence": sequence})
71
72         data = {}
73         errors = ""
74
75         try:
76             result = 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         except Exception as e:
85             errors = traceback.format_exc()
86             LOG.exception(e)
87         else:
88             if result:
89                 output_queue.put(result)
90
91         time.sleep(interval)
92
93         benchmark_output = {
94             'timestamp': time.time(),
95             'sequence': sequence,
96             'data': data,
97             'errors': errors
98         }
99
100         queue.put(benchmark_output)
101
102         LOG.debug("runner=%(runner)s seq=%(sequence)s END",
103                   {"runner": runner_cfg["runner_id"], "sequence": sequence})
104
105         sequence += 1
106
107         if (errors and sla_action is None) or aborted.is_set():
108             break
109
110     try:
111         benchmark.teardown()
112     except Exception:
113         # catch any exception in teardown and convert to simple exception
114         # never pass exceptions back to multiprocessing, because some exceptions can
115         # be unpicklable
116         # https://bugs.python.org/issue9400
117         LOG.exception("")
118         raise SystemExit(1)
119     LOG.info("worker END")
120     LOG.debug("queue.qsize() = %s", queue.qsize())
121     LOG.debug("output_queue.qsize() = %s", output_queue.qsize())
122
123
124 class SequenceRunner(base.Runner):
125     """Run a scenario by changing an input value defined in a list
126
127   Parameters
128     interval - time to wait between each scenario invocation
129         type:    int
130         unit:    seconds
131         default: 1 sec
132     scenario_option_name - name of the option that is increased each invocation
133         type:    string
134         unit:    na
135         default: none
136     sequence - list of values which are executed in their respective scenarios
137         type:    [int]
138         unit:    na
139         default: none
140     """
141
142     __execution_type__ = 'Sequence'
143
144     def _run_benchmark(self, cls, method, scenario_cfg, context_cfg):
145         name = "{}-{}-{}".format(self.__execution_type__, scenario_cfg.get("type"), os.getpid())
146         self.process = multiprocessing.Process(
147             name=name,
148             target=_worker_process,
149             args=(self.result_queue, cls, method, scenario_cfg,
150                   context_cfg, self.aborted, self.output_queue))
151         self.process.start()