4eab6643efabe27c804716937a2da1d5e6ea261b
[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                     context_cfg, aborted):
27
28     sequence = 1
29
30     runner_cfg = scenario_cfg['runner']
31
32     interval = runner_cfg.get("interval", 1)
33     arg_name = runner_cfg.get('name')
34     stop = runner_cfg.get('stop')
35     step = runner_cfg.get('step')
36     options = scenario_cfg['options']
37     start = options.get(arg_name, 0)
38
39     runner_cfg['runner_id'] = os.getpid()
40
41     LOG.info("worker START, step(%s, %d, %d, %d), class %s",
42              arg_name, start, stop, step, cls)
43
44     benchmark = cls(scenario_cfg, context_cfg)
45     benchmark.setup()
46     method = getattr(benchmark, method_name)
47
48     queue.put({'runner_id': runner_cfg['runner_id'],
49                'scenario_cfg': scenario_cfg,
50                'context_cfg': context_cfg})
51
52     sla_action = None
53     if "sla" in scenario_cfg:
54         sla_action = scenario_cfg["sla"].get("action", "assert")
55     margin = 1 if step > 0 else -1
56
57     for value in range(start, stop+margin, step):
58
59         if aborted.is_set():
60             break
61
62         options[arg_name] = value
63
64         LOG.debug("runner=%(runner)s seq=%(sequence)s START" %
65                   {"runner": runner_cfg["runner_id"], "sequence": sequence})
66
67         data = {}
68         errors = ""
69
70         try:
71             method(data)
72         except AssertionError as assertion:
73             # SLA validation failed in scenario, determine what to do now
74             if sla_action == "assert":
75                 raise
76             elif sla_action == "monitor":
77                 LOG.warning("SLA validation failed: %s" % assertion.args)
78                 errors = assertion.args
79         except Exception as e:
80             errors = traceback.format_exc()
81             LOG.exception(e)
82
83         time.sleep(interval)
84
85         benchmark_output = {
86             'timestamp': time.time(),
87             'sequence': sequence,
88             'data': data,
89             'errors': errors
90         }
91
92         record = {'runner_id': runner_cfg['runner_id'],
93                   'benchmark': benchmark_output}
94
95         queue.put(record)
96
97         LOG.debug("runner=%(runner)s seq=%(sequence)s END" %
98                   {"runner": runner_cfg["runner_id"], "sequence": sequence})
99
100         sequence += 1
101
102         if errors:
103             break
104
105     benchmark.teardown()
106     LOG.info("worker END")
107
108
109 class ArithmeticRunner(base.Runner):
110     '''Run a scenario arithmetically stepping an input value
111
112   Parameters
113     interval - time to wait between each scenario invocation
114         type:    int
115         unit:    seconds
116         default: 1 sec
117     name - name of scenario option that will be increased for each invocation
118         type:    string
119         unit:    na
120         default: none
121     start - value to use in first invocation of scenario
122         type:    int
123         unit:    na
124         default: none
125     step - value added to start value in next invocation of scenario
126         type:    int
127         unit:    na
128         default: none
129     stop - value indicating end of invocation
130         type:    int
131         unit:    na
132         default: none
133     '''
134
135     __execution_type__ = 'Arithmetic'
136
137     def _run_benchmark(self, cls, method, scenario_cfg, context_cfg):
138         self.process = multiprocessing.Process(
139             target=_worker_process,
140             args=(self.result_queue, cls, method, scenario_cfg,
141                   context_cfg, self.aborted))
142         self.process.start()