add setup/teardown to scenario base class and runners
[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
52     for value in range(start, stop, step):
53
54         options[arg_name] = value
55
56         LOG.debug("runner=%(runner)s seq=%(sequence)s START" %
57                   {"runner": context["runner"], "sequence": sequence})
58
59         data = {}
60         errors = ""
61
62         try:
63             data = method(scenario_args)
64         except AssertionError as assertion:
65             # SLA validation failed in scenario, determine what to do now
66             if sla_action == "assert":
67                 raise
68             elif sla_action == "monitor":
69                 LOG.warning("SLA validation failed: %s" % assertion.args)
70                 errors = assertion.args
71         except Exception as e:
72             errors = traceback.format_exc()
73             LOG.exception(e)
74
75         time.sleep(interval)
76
77         benchmark_output = {
78             'timestamp': time.time(),
79             'sequence': sequence,
80             'data': data,
81             'errors': errors
82         }
83
84         queue.put({'context': record_context, 'sargs:': scenario_args,
85                    'benchmark': benchmark_output})
86
87         LOG.debug("runner=%(runner)s seq=%(sequence)s END" %
88                   {"runner": context["runner"], "sequence": sequence})
89
90         sequence += 1
91
92         if errors:
93             break
94
95     benchmark.teardown()
96     LOG.info("worker END")
97
98
99 class ArithmeticRunner(base.Runner):
100
101     __execution_type__ = 'Arithmetic'
102
103     def _run_benchmark(self, cls, method, scenario_args):
104         self.process = multiprocessing.Process(
105             target=_worker_process,
106             args=(self.result_queue, cls, method, self.config, scenario_args))
107         self.process.start()