Update sla check for scenarios
[yardstick.git] / yardstick / benchmark / runners / arithmetic.py
1 ##############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 '''A runner that every run arithmetically steps a specified input value to
11 the scenario. This just means a step value is added to the previous value.
12 '''
13
14 import os
15 import multiprocessing
16 import logging
17 import traceback
18 import time
19
20 from yardstick.benchmark.runners import base
21
22 LOG = logging.getLogger(__name__)
23
24
25 def _worker_process(queue, cls, method_name, scenario_cfg):
26
27     sequence = 1
28
29     runner_cfg = scenario_cfg['runner']
30
31     interval = runner_cfg.get("interval", 1)
32     arg_name = runner_cfg.get('name')
33     stop = runner_cfg.get('stop')
34     step = runner_cfg.get('step')
35     options = scenario_cfg['options']
36     start = options.get(arg_name, 0)
37
38     runner_cfg['runner_id'] = os.getpid()
39
40     LOG.info("worker START, step(%s, %d, %d, %d), class %s",
41              arg_name, start, stop, step, cls)
42
43     benchmark = cls(runner_cfg)
44     benchmark.setup()
45     method = getattr(benchmark, method_name)
46
47     queue.put({'runner_id': runner_cfg['runner_id'],
48                'scenario_cfg': scenario_cfg})
49
50     sla_action = None
51     if "sla" in scenario_cfg:
52         sla_action = scenario_cfg["sla"].get("action", "assert")
53     margin = 1 if step > 0 else -1
54
55     for value in range(start, stop+margin, step):
56
57         options[arg_name] = value
58
59         LOG.debug("runner=%(runner)s seq=%(sequence)s START" %
60                   {"runner": runner_cfg["runner_id"], "sequence": sequence})
61
62         data = {}
63         errors = ""
64
65         try:
66             method(scenario_cfg, data)
67         except AssertionError as assertion:
68             # SLA validation failed in scenario, determine what to do now
69             if sla_action == "assert":
70                 raise
71             elif sla_action == "monitor":
72                 LOG.warning("SLA validation failed: %s" % assertion.args)
73                 errors = assertion.args
74         except Exception as e:
75             errors = traceback.format_exc()
76             LOG.exception(e)
77
78         time.sleep(interval)
79
80         benchmark_output = {
81             'timestamp': time.time(),
82             'sequence': sequence,
83             'data': data,
84             'errors': errors
85         }
86
87         record = {'runner_id': runner_cfg['runner_id'],
88                   'benchmark': benchmark_output}
89
90         queue.put(record)
91
92         LOG.debug("runner=%(runner)s seq=%(sequence)s END" %
93                   {"runner": runner_cfg["runner_id"], "sequence": sequence})
94
95         sequence += 1
96
97         if errors:
98             break
99
100     benchmark.teardown()
101     LOG.info("worker END")
102
103
104 class ArithmeticRunner(base.Runner):
105     '''Run a scenario arithmetically stepping an input value
106
107   Parameters
108     interval - time to wait between each scenario invocation
109         type:    int
110         unit:    seconds
111         default: 1 sec
112     name - name of scenario option that will be increased for each invocation
113         type:    string
114         unit:    na
115         default: none
116     start - value to use in first invocation of scenario
117         type:    int
118         unit:    na
119         default: none
120     step - value added to start value in next invocation of scenario
121         type:    int
122         unit:    na
123         default: none
124     stop - value indicating end of invocation
125         type:    int
126         unit:    na
127         default: none
128     '''
129
130     __execution_type__ = 'Arithmetic'
131
132     def _run_benchmark(self, cls, method, scenario_cfg):
133         self.process = multiprocessing.Process(
134             target=_worker_process,
135             args=(self.result_queue, cls, method, scenario_cfg))
136         self.process.start()