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