61d498f02591f68c99372b668bfb142c5f3f774e
[yardstick.git] / yardstick / benchmark / runners / duration.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 runs a specific time 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     duration = context.get("duration", 60)
30     LOG.info("worker START, duration %d sec, class %s", duration, 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     start = time.time()
46     while True:
47
48         LOG.debug("runner=%(runner)s seq=%(sequence)s START" %
49                   {"runner": context["runner"], "sequence": sequence})
50
51         data = {}
52         errors = ""
53
54         try:
55             data = method(scenario_args)
56         except AssertionError as assertion:
57             # SLA validation failed in scenario, determine what to do now
58             if sla_action == "assert":
59                 raise
60             elif sla_action == "monitor":
61                 LOG.warning("SLA validation failed: %s" % assertion.args)
62                 errors = assertion.args
63         except Exception as e:
64             errors = traceback.format_exc()
65             LOG.exception(e)
66
67         time.sleep(interval)
68
69         benchmark_output = {
70             'timestamp': time.time(),
71             'sequence': sequence,
72             'data': data,
73             'errors': errors
74         }
75
76         queue.put({'context': record_context, 'sargs': scenario_args,
77                    'benchmark': benchmark_output})
78
79         LOG.debug("runner=%(runner)s seq=%(sequence)s END" %
80                   {"runner": context["runner"], "sequence": sequence})
81
82         sequence += 1
83
84         if (errors and sla_action is None) or (time.time() - start > duration):
85             LOG.info("worker END")
86             break
87
88     benchmark.teardown()
89
90
91 class DurationRunner(base.Runner):
92
93     __execution_type__ = 'Duration'
94
95     def _run_benchmark(self, cls, method, scenario_args):
96         self.process = multiprocessing.Process(
97             target=_worker_process,
98             args=(self.result_queue, cls, method, self.config, scenario_args))
99         self.process.start()