def get_statistics(self, context, view_name, output_type): stats_obj = StcStats(self.stc.project, view_name) stats_obj.read_stats() statistics = OrderedDict() for obj_name in stats_obj.statistics['topLevelName']: statistics[obj_name] = stats_obj.get_object_stats(obj_name) if output_type.strip().lower() == 'json': statistics_str = json.dumps(statistics, indent=4, sort_keys=True, ensure_ascii=False) return json.loads(statistics_str) elif output_type.strip().lower() == 'csv': captions = statistics[stats_obj.statistics['topLevelName'] [0]].keys() output = io.BytesIO() w = csv.DictWriter(output, captions) w.writeheader() for obj_name in statistics: w.writerow(statistics[obj_name]) attach_stats_csv(context, self.logger, view_name, output.getvalue().strip()) return output.getvalue().strip() else: raise Exception('Output type should be CSV/JSON - got "{}"'.format( output_type))
def get_statistics(self, context, view_name, output_type): stats_obj = StcStats(view_name) stats_obj.read_stats() statistics = OrderedDict() for obj, obj_values in stats_obj.statistics.items(): statistics[obj.name] = obj_values if output_type.strip().lower() == "json": statistics_str = json.dumps(statistics, indent=4, sort_keys=True, ensure_ascii=False) return json.loads(statistics_str) elif output_type.strip().lower() == "csv": captions = list(list(statistics.values())[0].keys()) output = io.StringIO() w = csv.DictWriter(output, captions) w.writeheader() for obj_values in statistics.values(): w.writerow(obj_values) attach_stats_csv(context, self.logger, view_name, output.getvalue().strip()) return output.getvalue().strip() else: raise TgnError( f'Output type should be CSV/JSON - got "{output_type}"')
def test_stats_no_traffic(self, api): gen_stats = StcStats(self.stc.project, 'GeneratorPortResults') assert not gen_stats.get_all_stats() assert not gen_stats.get_stats() assert not gen_stats.get_object_stats('Port 1') assert gen_stats.get_stat('Port 1', 'GeneratorFrameCount') == '-1' assert gen_stats.get_counter('Port 1', 'GeneratorFrameCount') == -1
def manage_traffic() -> None: """Demonstrates how to manage traffic - start/stop/statistics...""" stc.start_traffic() time.sleep(8) stc.stop_traffic() port_stats = StcStats("generatorportresults") port_stats.read_stats() # You can get a table of all counters, similar to the GUI. print(port_stats.statistics.dumps(indent=2)) # Or a table of all counters of a specific object. print(json.dumps(port_stats.statistics["Port 1"], indent=2)) # Or a list of all values of a specific counters for all objects. print(port_stats.get_column_stats("TotalFrameCount")) # Or a single value of of a single counter os a single object. print(port_stats.statistics["Port 1"]["TotalFrameCount"])
def test_sequencer(self, api): """ Test Sequencer commands. """ self.logger.info(TestStcOnline.test_sequencer.__doc__.strip()) self.stc.load_config( path.join(path.dirname(__file__), 'configs/test_sequencer.tcc')) self._reserve_ports() self.stc.sequencer_command(StcSequencerOperation.start) self.stc.sequencer_command(StcSequencerOperation.wait) gen_stats = StcStats(self.stc.project, 'GeneratorPortResults') analyzer_stats = StcStats(self.stc.project, 'analyzerportresults') gen_stats.read_stats() analyzer_stats.read_stats() assert (gen_stats.get_counter('Port 1', 'GeneratorFrameCount') == 8000)
def test_traffic(self): self.test_reserve_ports() self.stc.start_traffic() time.sleep(8) self.stc.stop_traffic() port_stats = StcStats(self.stc.project, 'generatorportresults') port_stats.read_stats() print(port_stats.get_object_stats('Port 1')) print(port_stats.get_stats('TotalFrameCount')) print(port_stats.get_stat('Port 1', 'TotalFrameCount'))
def test_single_port_traffic(self, api): """ Test traffic and counters in loopback mode. """ self.logger.info( TestStcOnline.test_single_port_traffic.__doc__.strip()) self.stc_config_file = path.join(path.dirname(__file__), 'configs/loopback.tcc') self.stc.load_config(self.stc_config_file) self.ports = self.stc.project.get_children('port') self.stc.project.get_object_by_name('Port 1').reserve( self.config.get('STC', 'port1')) gen_stats = StcStats(self.stc.project, 'generatorportresults') analyzer_stats = StcStats(self.stc.project, 'analyzerportresults') gen_stats.read_stats() analyzer_stats.read_stats() assert (gen_stats.get_counter('Port 1', 'GeneratorFrameCount') == 0) assert (analyzer_stats.get_counter('Port 1', 'SigFrameCount') == 0) assert (gen_stats.get_counter( 'Port 1', 'GeneratorFrameCount') == analyzer_stats.get_counter( 'Port 1', 'SigFrameCount')) self.ports[0].start() self.ports[0].stop() gen_stats.read_stats() analyzer_stats.read_stats() assert (gen_stats.get_counter('Port 1', 'GeneratorFrameCount') != 0) assert (analyzer_stats.get_counter('Port 1', 'SigFrameCount') != 0) assert (gen_stats.get_counter( 'Port 1', 'GeneratorFrameCount') == analyzer_stats.get_counter( 'Port 1', 'SigFrameCount')) self.ports[0].clear_results() gen_stats.read_stats() analyzer_stats.read_stats() assert (gen_stats.get_counter('Port 1', 'GeneratorFrameCount') == 0) assert (analyzer_stats.get_counter('Port 1', 'SigFrameCount') == 0)
def test_custom_view(self, api): """ Test custom statistics view. """ self.logger.info(TestStcOnline.test_custom_view.__doc__.strip()) self.stc.load_config( path.join(path.dirname(__file__), 'configs/test_config.tcc')) self._reserve_ports() self.ports[0].start(blocking=True) user_stats = StcStats(self.stc.project, 'UserDynamicResultView') gen_stats = StcStats(self.stc.project, 'GeneratorPortResults') gen_stats.read_stats() print(gen_stats.statistics) user_stats.read_stats() print(user_stats.statistics) print(user_stats.get_stats('Port.GeneratorFrameCount')) print(user_stats.get_object_stats('Port 1', obj_id_stat='Port.Name')) print( user_stats.get_stat('Port 1', 'Port.GeneratorFrameCount', obj_id_stat='Port.Name'))
def test_port_traffic(self, api): """ Test traffic and counters. """ self.logger.info(TestStcOnline.test_port_traffic.__doc__.strip()) self.stc.load_config( path.join(path.dirname(__file__), 'configs/test_config.tcc')) self._reserve_ports() gen_stats = StcStats(self.stc.project, 'GeneratorPortResults') analyzer_stats = StcStats(self.stc.project, 'analyzerportresults') gen_stats.read_stats() analyzer_stats.read_stats() assert (gen_stats.get_counter('Port 1', 'GeneratorFrameCount') == 0) assert (analyzer_stats.get_counter('Port 2', 'SigFrameCount') == 0) assert (gen_stats.get_counter( 'Port 1', 'GeneratorFrameCount') == analyzer_stats.get_counter( 'Port 2', 'SigFrameCount')) self.ports[0].start() self.ports[0].stop() gen_stats.read_stats() analyzer_stats.read_stats() assert (gen_stats.get_counter( 'Port 1', 'GeneratorFrameCount') == analyzer_stats.get_counter( 'Port 2', 'SigFrameCount')) self.ports[0].clear_results() gen_stats.read_stats() analyzer_stats.read_stats() assert (gen_stats.get_counter('Port 1', 'GeneratorFrameCount') == 0) assert (analyzer_stats.get_counter('Port 2', 'SigFrameCount') != 0) self.ports[1].clear_results() analyzer_stats.read_stats() assert (analyzer_stats.get_counter('Port 2', 'SigFrameCount') == 0) self.stc.start_traffic(blocking=True) self.stc.stop_traffic() gen_stats.read_stats() analyzer_stats.read_stats() assert (gen_stats.get_counter( 'Port 2', 'GeneratorFrameCount') == analyzer_stats.get_counter( 'Port 1', 'SigFrameCount')) self.stc.clear_results() gen_stats.read_stats('GeneratorFrameCount') analyzer_stats.read_stats('SigFrameCount') assert (gen_stats.get_counter('Port 1', 'GeneratorFrameCount') == 0) assert (analyzer_stats.get_counter('Port 2', 'SigFrameCount') == 0)