def test_stream_stats(self): """ For this test we need back-to-back ports. """ ports = self.xm.session.reserve_ports([self.port1, self.port2]) ports[self.port1].load_config( path.join(path.dirname(__file__), 'configs', 'test_config_1.xpc')) ports[self.port2].load_config( path.join(path.dirname(__file__), 'configs', 'test_config_2.xpc')) self.xm.session.start_traffic(blocking=True) tpld_stats = XenaTpldsStats(self.xm.session) print(tpld_stats.read_stats().dumps()) streams_stats = XenaStreamsStats(self.xm.session) streams_stats.read_stats() print(streams_stats.tx_statistics.dumps()) print(streams_stats.statistics.dumps()) # Access TX counter using stream name or stream object, from tx_statistics or statistics. assert (streams_stats.tx_statistics['Stream 1-1']['packets'] == 8000) assert (streams_stats.tx_statistics[ports[self.port1].streams[0]] ['packets'] == 8000) assert (streams_stats.statistics[ports[self.port1].streams[0]]['tx'] ['packets'] == 8000) assert ( streams_stats.statistics['Stream 1-1']['tx']['packets'] == 8000) assert (streams_stats.statistics['/'.join(self.port1.split('/')[1:]) + '/0']['tx']['packets'] == 8000) # Access RX counter with port name on RX side or directly. assert (streams_stats.statistics['Stream 1-1']['rx']['pr_tpldtraffic'] ['pac'] == 8000) assert (streams_stats.statistics['Stream 1-1']['rx'][ports[self.port2]] ['pr_tpldtraffic']['pac'] == 8000) assert (streams_stats.statistics['Stream 1-1']['rx'][self.port2] ['pr_tpldtraffic']['pac'] == 8000)
def traffic(): """ Load configuration and, run traffic and print statistics. """ ports = xm.session.reserve_ports([port1, port2], True) ports[port1].load_config(config1) ports[port2].load_config(config2) ports[port1].wait_for_up(16) ports[port2].wait_for_up(16) xm.session.clear_stats() xm.session.start_traffic(blocking=True) ports_stats = XenaPortsStats(xm.session) ports_stats.read_stats() print(json.dumps(ports_stats.statistics, indent=1)) streams_stats = XenaStreamsStats(xm.session) streams_stats.read_stats() print(json.dumps(streams_stats.statistics, indent=1)) tplds_stats = XenaTpldsStats(xm.session) tplds_stats.read_stats() print(json.dumps(tplds_stats.statistics, indent=1))
def measure(): global ports # Pet ports XenaPort(parent=chassis, index=port0) XenaPort(parent=chassis, index=port1) port_stats = XenaPortsStats(xm.session) streams_stats = XenaStreamsStats(xm.session) # Loop for 10 seconds or any condition you want for _ in range(10): # Get statistics. port_stats.read_stats() streams_stats.read_stats() print(port_stats.statistics.dumps()) print(streams_stats.statistics.dumps()) time.sleep(1)
def test_traffic(self): port = self.xm.session.reserve_ports([self.port1])[self.port1] port.load_config( path.join(path.dirname(__file__), 'configs', 'test_config_loopback.xpc')) self.xm.session.clear_stats() port_stats = port.read_port_stats() print(json.dumps(port_stats, indent=1)) self.xm.session.start_traffic() time.sleep(2) port_stats = port.read_port_stats() print(json.dumps(port_stats, indent=1)) assert (abs(port_stats['pt_total']['packets'] - port_stats['pr_total']['packets']) < 1111) assert (abs(1000 - port.streams[0].read_stats()['pps']) < 111) assert (abs(1000 - port.tplds[0].read_stats()['pr_tpldtraffic']['pps']) < 111) self.xm.session.stop_traffic() self.xm.session.clear_stats() self.xm.session.start_traffic(blocking=True) ports_stats = XenaPortsStats(self.xm.session) ports_stats.read_stats() print(ports_stats.statistics.dumps()) print(json.dumps(ports_stats.get_flat_stats(), indent=1)) streams_stats = XenaStreamsStats(self.xm.session) streams_stats.read_stats() print(streams_stats.statistics.dumps()) print(json.dumps(streams_stats.get_flat_stats(), indent=1)) tplds_stats = XenaTpldsStats(self.xm.session) tplds_stats.read_stats() print(tplds_stats.statistics.dumps()) print(json.dumps(tplds_stats.get_flat_stats(), indent=1))
def traffic(): """ Run traffic. Get statistics. Get capture. """ # Run traffic with capture on all ports. xm.session.clear_stats() xm.session.start_capture() xm.session.start_traffic(blocking=True) xm.session.stop_capture() # Get port level statistics. ports_stats = XenaPortsStats(xm.session) ports_stats.read_stats() print(ports_stats.statistics.dumps()) # Get stream level statistics. # For each stream the returned dictionary includes the TX statistics and the associated TPLD statistics. streams_stats = XenaStreamsStats(xm.session) streams_stats.read_stats() print(streams_stats.statistics.dumps()) # Get TPLD level statistics. tplds_stats = XenaTpldsStats(xm.session) tplds_stats.read_stats() print(tplds_stats.statistics.dumps()) # Run traffic on one port and capture on the second port. xm.session.clear_stats() ports[port0].start_capture() ports[port1].start_traffic(blocking=True) ports[port0].stop_capture() # Get individual port statistics. print(json.dumps(ports[port0].read_port_stats(), indent=1)) # Get individual stream statistics. print(json.dumps(ports[port0].streams[0].read_stats(), indent=1)) # Get first two captured packets in raw format - note that MAC address changed due to modifier. packets = ports[port0].capture.get_packets( to_index=2, cap_type=XenaCaptureBufferType.raw) for packet in packets: packet = Ethernet(binascii.unhexlify(packet)) print(packet.dst_s) # Get first two packets in wireshark text format. packets = ports[port0].capture.get_packets(from_index=10, to_index=12) for packet in packets: print(packet) # Analyze capture buffer with tshark. tshark = Tshark(wireshark_path) packets = ports[port0].capture.get_packets( cap_type=XenaCaptureBufferType.pcap, file_name=pcap_file, tshark=tshark) analyser = TsharkAnalyzer() analyser.add_field('ip.src') analyser.add_field('ip.dst') fields = tshark.analyze(pcap_file, analyser) print(len(fields)) analyser.set_read_filter('eth.dst == 11:11:11:11:00:11') fields = tshark.analyze(pcap_file, analyser) print(len(fields))
def traffic(): global ports with open(cap_file, 'w+') as _: pass with open(results_file, 'w+') as _: pass # Reserve ports ports = xm.session.reserve_ports([port0, port1], True) ports[port0].wait_for_up(16) ports[port1].wait_for_up(16) # Load configuration on port-0. ports[port0].load_config(config0) # Disable all streams for stream in ports[port0].streams.values(): stream.set_attributes(PS_ENABLE='OFF') for sid, stream in ports[port0].streams.items(): # Enable current stream stream.set_attributes(PS_ENABLE='ON') # Run traffic with capture on all ports. # Run traffic on one port and capture on the other port. xm.session.clear_stats() ports[port1].start_capture() ports[port0].start_traffic(blocking=True) ports[port1].stop_capture() # Get port level statistics. streams_stats = XenaStreamsStats(xm.session) streams_stats.read_stats() print(streams_stats.statistics.dumps()) with open(results_file, 'a+') as f: f.write('{} TX packets = {}\n'.format( stream.name, streams_stats.statistics[stream.name]['tx']['packets'])) f.write('{} RX packets = {}\n'.format( stream.name, streams_stats.statistics[stream.name]['rx'] ['pr_tpldtraffic']['pac'])) f.write('{} TX bytes = {}\n'.format( stream.name, streams_stats.statistics[stream.name]['tx']['bytes'])) f.write('{} RX bytes = {}\n'.format( stream.name, streams_stats.statistics[stream.name]['rx'] ['pr_tpldtraffic']['byt'])) # Get first captured packet in raw format. packet = ports[port1].capture.get_packets( to_index=1, cap_type=XenaCaptureBufferType.text)[0] with open(cap_file, 'a+') as f: f.write('{} first packet = {}\n\n'.format(stream.name, packet)) # Disable current stream stream.set_attributes(PS_ENABLE='OFF')