bac7efb490bd8dcfb0c4113152717baa6f134136
[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     benchmark = cls(context)
30     method = getattr(benchmark, method_name)
31
32     interval = context.get("interval", 1)
33     arg_name = context.get('name')
34     stop = context.get('stop')
35     step = context.get('step')
36     options = scenario_args['options']
37     start = options.get(arg_name, 0)
38
39     context['runner'] = os.getpid()
40
41     LOG.info("worker START, step(%s, %d, %d, %d), class %s",
42              arg_name, start, stop, step, cls)
43
44     record_context = {"runner": context["runner"],
45                       "host": context["host"]}
46
47     sla_action = None
48     if "sla" in scenario_args:
49         sla_action = scenario_args["sla"].get("action", "assert")
50
51     for value in range(start, stop, step):
52
53         options[arg_name] = value
54
55         LOG.debug("runner=%(runner)s seq=%(sequence)s START" %
56                   {"runner": context["runner"], "sequence": sequence})
57
58         data = {}
59         errors = ""
60
61         try:
62             data = method(scenario_args)
63         except AssertionError as assertion:
64             # SLA validation failed in scenario, determine what to do now
65             if sla_action == "assert":
66                 raise
67             elif sla_action == "monitor":
68                 LOG.warning("SLA validation failed: %s" % assertion.args)
69                 errors = assertion.args
70         except Exception as e:
71             errors = traceback.format_exc()
72             LOG.exception(e)
73
74         time.sleep(interval)
75
76         benchmark_output = {
77             'timestamp': time.time(),
78             'sequence': sequence,
79             'data': data,
80             'errors': errors
81         }
82
83         queue.put({'context': record_context, 'sargs:': scenario_args,
84                    'benchmark': benchmark_output})
85
86         LOG.debug("runner=%(runner)s seq=%(sequence)s END" %
87                   {"runner": context["runner"], "sequence": sequence})
88
89         sequence += 1
90
91         if errors:
92             break
93
94     LOG.info("worker END")
95
96
97 class ArithmeticRunner(base.Runner):
98
99     __execution_type__ = 'Arithmetic'
100
101     def _run_benchmark(self, cls, method, scenario_args):
102         self.process = multiprocessing.Process(
103             target=_worker_process,
104             args=(self.result_queue, cls, method, self.config, scenario_args))
105         self.process.start()