Merge "Bugfix: task_id parameter from API can not pass to yardstick core"
[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):
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     queue.put({'runner_id': runner_cfg['runner_id'],
61                'scenario_cfg': scenario_cfg,
62                'context_cfg': context_cfg})
63
64     sla_action = None
65     if "sla" in scenario_cfg:
66         sla_action = scenario_cfg["sla"].get("action", "assert")
67
68     for value in sequence_values:
69         options[arg_name] = value
70
71         LOG.debug("runner=%(runner)s seq=%(sequence)s START",
72                   {"runner": runner_cfg["runner_id"], "sequence": sequence})
73
74         data = {}
75         errors = ""
76
77         try:
78             method(data)
79         except AssertionError as assertion:
80             # SLA validation failed in scenario, determine what to do now
81             if sla_action == "assert":
82                 raise
83             elif sla_action == "monitor":
84                 LOG.warning("SLA validation failed: %s", assertion.args)
85                 errors = assertion.args
86         except Exception as e:
87             errors = traceback.format_exc()
88             LOG.exception(e)
89
90         time.sleep(interval)
91
92         benchmark_output = {
93             'timestamp': time.time(),
94             'sequence': sequence,
95             'data': data,
96             'errors': errors
97         }
98
99         record = {'runner_id': runner_cfg['runner_id'],
100                   'benchmark': benchmark_output}
101
102         queue.put(record)
103
104         LOG.debug("runner=%(runner)s seq=%(sequence)s END",
105                   {"runner": runner_cfg["runner_id"], "sequence": sequence})
106
107         sequence += 1
108
109         if (errors and sla_action is None) or aborted.is_set():
110             break
111
112     benchmark.teardown()
113     LOG.info("worker END")
114
115
116 class SequenceRunner(base.Runner):
117     """Run a scenario by changing an input value defined in a list
118
119   Parameters
120     interval - time to wait between each scenario invocation
121         type:    int
122         unit:    seconds
123         default: 1 sec
124     scenario_option_name - name of the option that is increased each invocation
125         type:    string
126         unit:    na
127         default: none
128     sequence - list of values which are executed in their respective scenarios
129         type:    [int]
130         unit:    na
131         default: none
132     """
133
134     __execution_type__ = 'Sequence'
135
136     def _run_benchmark(self, cls, method, scenario_cfg, context_cfg):
137         self.process = multiprocessing.Process(
138             target=_worker_process,
139             args=(self.result_queue, cls, method, scenario_cfg,
140                   context_cfg, self.aborted))
141         self.process.start()