1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd. and others
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 ##############################################################################
11 import multiprocessing
14 import yardstick.common.utils as utils
16 LOG = logging.getLogger(__name__)
18 monitor_conf_path = pkg_resources.resource_filename(
19 "yardstick.benchmark.scenarios.availability",
23 class MonitorMgr(object):
24 """docstring for MonitorMgr"""
26 self._monitor_list = []
28 def init_monitors(self, monitor_cfgs, context):
29 LOG.debug("monitorMgr config: %s" % monitor_cfgs)
31 for monitor_cfg in monitor_cfgs:
32 monitor_type = monitor_cfg["monitor_type"]
33 monitor_cls = BaseMonitor.get_monitor_cls(monitor_type)
34 monitor_ins = monitor_cls(monitor_cfg, context)
36 self._monitor_list.append(monitor_ins)
38 def start_monitors(self):
39 for _monotor_instace in self._monitor_list:
40 _monotor_instace.start_monitor()
42 def wait_monitors(self):
43 for monitor in self._monitor_list:
44 monitor.wait_monitor()
48 for monitor in self._monitor_list:
49 sla_pass = sla_pass & monitor.verify_SLA()
53 class BaseMonitor(multiprocessing.Process):
54 """docstring for BaseMonitor"""
56 def __init__(self, config, context):
57 multiprocessing.Process.__init__(self)
59 self._context = context
60 self._queue = multiprocessing.Queue()
61 self._event = multiprocessing.Event()
62 self.setup_done = False
65 def get_monitor_cls(monitor_type):
66 '''return monitor class of specified type'''
68 for monitor in utils.itersubclasses(BaseMonitor):
69 if monitor_type == monitor.__monitor_type__:
71 raise RuntimeError("No such monitor_type %s" % monitor_type)
73 def get_script_fullpath(self, path):
74 base_path = os.path.dirname(monitor_conf_path)
75 return os.path.join(base_path, path)
78 LOG.debug("config:%s context:%s" % (self._config, self._context))
81 monitor_time = self._config.get("monitor_time", 0)
90 begin_time = time.time()
92 total_count = total_count + 1
94 one_check_begin_time = time.time()
95 exit_status = self.monitor_func()
96 one_check_end_time = time.time()
98 if exit_status is False:
99 outage_count = outage_count + 1
101 outage_time = outage_time + (
102 one_check_end_time - one_check_begin_time)
105 first_outage = one_check_begin_time
107 last_outage = one_check_end_time
109 if self._event.is_set():
110 LOG.debug("the monitor process stop")
113 if one_check_end_time - begin_time > monitor_time:
114 LOG.debug("the monitor max_time finished and exit!")
117 end_time = time.time()
118 total_time = end_time - begin_time
120 self._queue.put({"total_time": total_time,
121 "outage_time": last_outage-first_outage,
122 "total_count": total_count,
123 "outage_count": outage_count})
125 def start_monitor(self):
128 def wait_monitor(self):
130 self._result = self._queue.get()
131 LOG.debug("the monitor result:%s" % self._result)
133 def setup(self): # pragma: no cover
136 def monitor_func(self): # pragma: no cover
139 def verify_SLA(self):