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