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
17 LOG = logging.getLogger(__name__)
19 monitor_conf_path = pkg_resources.resource_filename(
20 "yardstick.benchmark.scenarios.availability",
24 class MonitorMgr(object):
25 """docstring for MonitorMgr"""
27 self._monitor_list = []
29 def init_monitors(self, monitor_cfgs, context):
30 LOG.debug("monitorMgr config: %s" % monitor_cfgs)
32 for monitor_cfg in monitor_cfgs:
33 monitor_type = monitor_cfg["monitor_type"]
34 monitor_cls = BaseMonitor.get_monitor_cls(monitor_type)
35 monitor_ins = monitor_cls(monitor_cfg, context)
36 if "key" in monitor_cfg:
37 monitor_ins.key = monitor_cfg["key"]
38 self._monitor_list.append(monitor_ins)
40 def __getitem__(self, item):
41 for obj in self._monitor_list:
44 raise KeyError("No such monitor instance of key - %s" % item)
46 def start_monitors(self):
47 for _monotor_instace in self._monitor_list:
48 _monotor_instace.start_monitor()
50 def wait_monitors(self):
51 for monitor in self._monitor_list:
52 monitor.wait_monitor()
56 for monitor in self._monitor_list:
57 sla_pass = sla_pass & monitor.verify_SLA()
61 class BaseMonitor(multiprocessing.Process):
62 """docstring for BaseMonitor"""
65 def __init__(self, config, context):
66 if not BaseMonitor.monitor_cfgs:
67 with open(monitor_conf_path) as stream:
68 BaseMonitor.monitor_cfgs = yaml.load(stream)
69 multiprocessing.Process.__init__(self)
71 self._context = context
72 self._queue = multiprocessing.Queue()
73 self._event = multiprocessing.Event()
74 self.setup_done = False
77 def get_monitor_cls(monitor_type):
78 '''return monitor class of specified type'''
80 for monitor in utils.itersubclasses(BaseMonitor):
81 if monitor_type == monitor.__monitor_type__:
83 raise RuntimeError("No such monitor_type %s" % monitor_type)
85 def get_script_fullpath(self, path):
86 base_path = os.path.dirname(monitor_conf_path)
87 return os.path.join(base_path, path)
90 LOG.debug("config:%s context:%s" % (self._config, self._context))
93 monitor_time = self._config.get("monitor_time", 0)
102 begin_time = time.time()
104 total_count = total_count + 1
106 one_check_begin_time = time.time()
107 exit_status = self.monitor_func()
108 one_check_end_time = time.time()
110 if exit_status is False:
111 outage_count = outage_count + 1
113 outage_time = outage_time + (
114 one_check_end_time - one_check_begin_time)
117 first_outage = one_check_begin_time
119 last_outage = one_check_end_time
121 if self._event.is_set():
122 LOG.debug("the monitor process stop")
125 if one_check_end_time - begin_time > monitor_time:
126 LOG.debug("the monitor max_time finished and exit!")
129 end_time = time.time()
130 total_time = end_time - begin_time
132 self._queue.put({"total_time": total_time,
133 "outage_time": last_outage-first_outage,
134 "total_count": total_count,
135 "outage_count": outage_count})
137 def start_monitor(self):
140 def wait_monitor(self):
142 self._result = self._queue.get()
143 LOG.debug("the monitor result:%s" % self._result)
145 def setup(self): # pragma: no cover
148 def monitor_func(self): # pragma: no cover
151 def verify_SLA(self):