From 91f6c24f45cbb0cbf8568a9938059a1a934e6ae6 Mon Sep 17 00:00:00 2001 From: Dan Klein Date: Thu, 26 Nov 2015 13:06:36 +0200 Subject: Initial implementation of stats prompting --- .../trex_control_plane/common/trex_stats.py | 80 ++++++++++++++++++++++ 1 file changed, 80 insertions(+) (limited to 'scripts/automation/trex_control_plane/common') diff --git a/scripts/automation/trex_control_plane/common/trex_stats.py b/scripts/automation/trex_control_plane/common/trex_stats.py index b7e768c1..bf5ba2bb 100755 --- a/scripts/automation/trex_control_plane/common/trex_stats.py +++ b/scripts/automation/trex_control_plane/common/trex_stats.py @@ -1,6 +1,86 @@ #!/router/bin/python +from collections import namedtuple, OrderedDict +from client_utils import text_tables import copy +GLOBAL_STATS = 'g' +PORT_STATS = 'p' +PORT_STATUS = 'ps' +ALL_STATS_OPTS = {GLOBAL_STATS, PORT_STATS, PORT_STATUS} +ExportableStats = namedtuple('ExportableStats', ['raw_data', 'text_table']) + + +class CTRexInformationCenter(object): + + def __init__(self, connection_info, ports_ref, async_stats_ref): + self.connection_info = connection_info + self.server_version = None + self.system_info = None + self._ports = ports_ref + self._async_stats = async_stats_ref + + # def __getitem__(self, item): + # stats_obj = getattr(self, item) + # if stats_obj: + # return stats_obj.get_stats() + # else: + # return None + + def generate_single_statistic(self, statistic_type): + if statistic_type == GLOBAL_STATS: + return self._generate_global_stats() + elif statistic_type == PORT_STATS: + # return generate_global_stats() + pass + elif statistic_type == PORT_STATUS: + pass + else: + # ignore by returning empty object + return {} + + def _generate_global_stats(self): + stats_obj = self._async_stats.get_general_stats() + return_stats_data = \ + OrderedDict([("connection", "{host}, Port {port}".format(host=self.connection_info.get("server"), + port=self.connection_info.get("sync_port"))), + ("version", self.server_version.get("version", "N/A")), + ("cpu_util", stats_obj.get("m_cpu_util")), + ("total_tx", stats_obj.get("m_tx_bps", format=True, suffix="b/sec")), + # {'m_tx_bps': stats_obj.get("m_tx_bps", format= True, suffix= "b/sec"), + # 'm_tx_pps': stats_obj.get("m_tx_pps", format= True, suffix= "pkt/sec"), + # 'm_total_tx_bytes':stats_obj.get_rel("m_total_tx_bytes", + # format= True, + # suffix = "B"), + # 'm_total_tx_pkts': stats_obj.get_rel("m_total_tx_pkts", + # format= True, + # suffix = "pkts")}, + ("total_rx", stats_obj.get("m_rx_bps", format=True, suffix="b/sec")), + # {'m_rx_bps': stats_obj.get("m_rx_bps", format= True, suffix= "b/sec"), + # 'm_rx_pps': stats_obj.get("m_rx_pps", format= True, suffix= "pkt/sec"), + # 'm_total_rx_bytes': stats_obj.get_rel("m_total_rx_bytes", + # format= True, + # suffix = "B"), + # 'm_total_rx_pkts': stats_obj.get_rel("m_total_rx_pkts", + # format= True, + # suffix = "pkts")}, + ("total_pps", stats_obj.format_num(stats_obj.get("m_tx_pps") + stats_obj.get("m_rx_pps"), + suffix="pkt/sec")), + ("total_streams", sum([len(port.streams) + for port in self._ports])), + ("active_ports", sum([port.is_active() + for port in self._ports])) + ] + ) + + # build table representation + stats_table = text_tables.TRexTextInfo() + stats_table.set_cols_align(["l", "l"]) + stats_table.add_rows([[k.replace("_", " ").title(), v] + for k, v in return_stats_data.iteritems()], + header=False) + + return {"global_statistics": ExportableStats(return_stats_data, stats_table)} + class CTRexStatsManager(object): -- cgit 1.2.3-korg