blob: a3bf33f38ce14012d6b2ef5d2206094bd7b16e09 [file] [log] [blame]
Moshe0bb532c2018-02-26 13:39:57 +02001##############################################################################
2# Copyright 2018 EuropeanSoftwareMarketingLtd.
3# ===================================================================
4# Licensed under the ApacheLicense, Version2.0 (the"License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# software distributed under the License is distributed on an "AS IS" BASIS,
10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11# See the License for the specific language governing permissions and limitations under
12# the License
13##############################################################################
14# vnftest comment: this is a modified copy of
15# rally/rally/benchmark/runners/duration.py
16
17"""A runner that runs a specific time before it returns
18"""
19
20from __future__ import absolute_import
Moshe0bb532c2018-02-26 13:39:57 +020021
Moshe30497ac2018-03-14 14:22:13 +020022import logging
23import multiprocessing
24import time
25import traceback
26
27import os
28
29from vnftest.runners import base
Moshe0bb532c2018-02-26 13:39:57 +020030
31LOG = logging.getLogger(__name__)
32
33
34QUEUE_PUT_TIMEOUT = 10
35
36
37def _worker_process(queue, cls, method_name, step_cfg,
38 context_cfg, aborted, output_queue):
39
40 sequence = 1
41
42 runner_cfg = step_cfg['runner']
43
44 interval = runner_cfg.get("interval", 1)
45 duration = runner_cfg.get("duration", 60)
46 LOG.info("Worker START, duration is %ds", duration)
47 LOG.debug("class is %s", cls)
48
49 runner_cfg['runner_id'] = os.getpid()
50
51 step = cls(step_cfg, context_cfg)
52 step.setup()
53 method = getattr(step, method_name)
54
55 sla_action = None
56 if "sla" in step_cfg:
57 sla_action = step_cfg["sla"].get("action", "assert")
58
59 start = time.time()
60 timeout = start + duration
61 while True:
62
63 LOG.debug("runner=%(runner)s seq=%(sequence)s START",
64 {"runner": runner_cfg["runner_id"], "sequence": sequence})
65
66 data = {}
67 errors = ""
68
69 try:
70 result = method(data)
71 except AssertionError as assertion:
72 # SLA validation failed in scenario, determine what to do now
73 if sla_action == "assert":
74 raise
75 elif sla_action == "monitor":
76 LOG.warning("SLA validation failed: %s", assertion.args)
77 errors = assertion.args
78 # catch all exceptions because with multiprocessing we can have un-picklable exception
79 # problems https://bugs.python.org/issue9400
80 except Exception:
81 errors = traceback.format_exc()
82 LOG.exception("")
83 else:
84 if result:
85 # add timeout for put so we don't block test
86 # if we do timeout we don't care about dropping individual KPIs
87 output_queue.put(result, True, QUEUE_PUT_TIMEOUT)
88
89 time.sleep(interval)
90
91 step_output = {
92 'timestamp': time.time(),
93 'sequence': sequence,
94 'data': data,
95 'errors': errors
96 }
97
98 queue.put(step_output, True, QUEUE_PUT_TIMEOUT)
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 time.time() > timeout or aborted.is_set():
106 LOG.info("Worker END")
107 break
108
109 try:
110 step.teardown()
111 except Exception:
112 # catch any exception in teardown and convert to simple exception
113 # never pass exceptions back to multiprocessing, because some exceptions can
114 # be unpicklable
115 # https://bugs.python.org/issue9400
116 LOG.exception("")
117 raise SystemExit(1)
118
119 LOG.debug("queue.qsize() = %s", queue.qsize())
120 LOG.debug("output_queue.qsize() = %s", output_queue.qsize())
121
122
123class DurationRunner(base.Runner):
124 """Run a scenario for a certain amount of time
125
126If the scenario ends before the time has elapsed, it will be started again.
127
128 Parameters
129 duration - amount of time the scenario will be run for
130 type: int
131 unit: seconds
132 default: 1 sec
133 interval - time to wait between each scenario invocation
134 type: int
135 unit: seconds
136 default: 1 sec
137 """
138 __execution_type__ = 'Duration'
139
140 def _run_step(self, cls, method, step_cfg, context_cfg):
141 name = "{}-{}-{}".format(self.__execution_type__, step_cfg.get("type"), os.getpid())
142 self.process = multiprocessing.Process(
143 name=name,
144 target=_worker_process,
145 args=(self.result_queue, cls, method, step_cfg,
146 context_cfg, self.aborted, self.output_queue))
147 self.process.start()