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.monitor",
23 class BaseMonitor(multiprocessing.Process):
24 """docstring for BaseMonitor"""
26 def __init__(self, config, context):
27 multiprocessing.Process.__init__(self)
29 self._context = context
30 self._queue = multiprocessing.Queue()
31 self._event = multiprocessing.Event()
32 self.setup_done = False
35 def get_monitor_cls(monitor_type):
36 '''return monitor class of specified type'''
38 for monitor in utils.itersubclasses(BaseMonitor):
39 if monitor_type == monitor.__monitor_type__:
41 raise RuntimeError("No such monitor_type %s" % monitor_type)
43 def get_script_fullpath(self, path):
44 base_path = os.path.dirname(monitor_conf_path)
45 return os.path.join(base_path, path)
48 LOG.debug("config:%s context:%s" % (self._config, self._context))
51 monitor_time = self._config.get("monitor_time", 0)
60 begin_time = time.time()
62 total_count = total_count + 1
64 one_check_begin_time = time.time()
65 exit_status = self.monitor_func()
66 one_check_end_time = time.time()
68 if exit_status is False:
69 outage_count = outage_count + 1
71 outage_time = outage_time + (
72 one_check_end_time - one_check_begin_time)
75 first_outage = one_check_begin_time
77 last_outage = one_check_end_time
79 if self._event.is_set():
80 LOG.debug("the monitor process stop")
83 if one_check_end_time - begin_time > monitor_time:
84 LOG.debug("the monitor max_time finished and exit!")
87 end_time = time.time()
88 total_time = end_time - begin_time
90 self._queue.put({"total_time": total_time,
91 "outage_time": last_outage-first_outage,
92 "total_count": total_count,
93 "outage_count": outage_count})
95 def start_monitor(self):
98 def wait_monitor(self):
100 self._result = self._queue.get()
101 LOG.debug("the monitor result:%s" % self._result)
103 def setup(self): # pragma: no cover
106 def monitor_func(self): # pragma: no cover
109 def verify_SLA(self):
113 class MonitorMgr(object):
114 """docstring for MonitorMgr"""
116 self._monitor_list = []
118 def init_monitors(self, monitor_cfgs, context):
119 LOG.debug("monitorMgr config: %s" % monitor_cfgs)
121 for monitor_cfg in monitor_cfgs:
122 monitor_type = monitor_cfg["monitor_type"]
123 monitor_cls = BaseMonitor.get_monitor_cls(monitor_type)
124 monitor_ins = monitor_cls(monitor_cfg, context)
126 self._monitor_list.append(monitor_ins)
128 def start_monitors(self):
129 for _monotor_instace in self._monitor_list:
130 _monotor_instace.start_monitor()
132 def wait_monitors(self):
133 for monitor in self._monitor_list:
134 monitor.wait_monitor()
136 def verify_SLA(self):
138 for monitor in self._monitor_list:
139 sla_pass = sla_pass & monitor.verify_SLA()