Merge "Add iteration type for Runner"
[yardstick.git] / yardstick / benchmark / runners / iteration.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd 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 runs a configurable number of times before it returns
11 '''
12
13 import os
14 import multiprocessing
15 import logging
16 import traceback
17 import time
18
19 from yardstick.benchmark.runners import base
20
21 LOG = logging.getLogger(__name__)
22
23
24 def _worker_process(queue, cls, method_name, context, scenario_args):
25
26     sequence = 1
27
28     interval = context.get("interval", 1)
29     iterations = context.get("iterations", 1)
30     LOG.info("worker START, iterations %d times, class %s", iterations, cls)
31
32     context['runner'] = os.getpid()
33
34     benchmark = cls(context)
35     benchmark.setup()
36     method = getattr(benchmark, method_name)
37
38     record_context = {"runner": context["runner"],
39                       "host": context["host"]}
40
41     sla_action = None
42     if "sla" in scenario_args:
43         sla_action = scenario_args["sla"].get("action", "assert")
44
45     while True:
46
47         LOG.debug("runner=%(runner)s seq=%(sequence)s START" %
48                   {"runner": context["runner"], "sequence": sequence})
49
50         data = {}
51         errors = ""
52
53         try:
54             data = method(scenario_args)
55         except AssertionError as assertion:
56             # SLA validation failed in scenario, determine what to do now
57             if sla_action == "assert":
58                 raise
59             elif sla_action == "monitor":
60                 LOG.warning("SLA validation failed: %s" % assertion.args)
61                 errors = assertion.args
62         except Exception as e:
63             errors = traceback.format_exc()
64             LOG.exception(e)
65
66         time.sleep(interval)
67
68         benchmark_output = {
69             'timestamp': time.time(),
70             'sequence': sequence,
71             'data': data,
72             'errors': errors
73         }
74
75         queue.put({'context': record_context, 'sargs': scenario_args,
76                    'benchmark': benchmark_output})
77
78         LOG.debug("runner=%(runner)s seq=%(sequence)s END" %
79                   {"runner": context["runner"], "sequence": sequence})
80
81         sequence += 1
82
83         if (errors and sla_action is None) or (sequence > iterations):
84             LOG.info("worker END")
85             break
86
87     benchmark.teardown()
88
89
90 class IterationRunner(base.Runner):
91     '''Run a scenario for a configurable number of times
92
93 If the scenario ends before the time has elapsed, it will be started again.
94
95   Parameters
96     iterations - amount of times the scenario will be run for
97         type:    int
98         unit:    na
99         default: 1
100     interval - time to wait between each scenario invocation
101         type:    int
102         unit:    seconds
103         default: 1 sec
104     '''
105     __execution_type__ = 'Iteration'
106
107     def _run_benchmark(self, cls, method, scenario_args):
108         self.process = multiprocessing.Process(
109             target=_worker_process,
110             args=(self.result_queue, cls, method, self.config, scenario_args))
111         self.process.start()