Network Latency Benchmarking Tool for Two-Way Packet Verification Between Hosts


5 views

When testing network performance between two endpoints, we need more than just simple ping tests. The ideal solution should:

  • Establish bidirectional communication
  • Verify packet integrity
  • Measure round-trip latency
  • Generate statistical reports
  • Support configurable packet sizes

Here's a Python implementation using sockets that meets these requirements:


# server.py (run on Machine 2)
import socket
import hashlib

def calculate_checksum(data):
    return hashlib.md5(data).hexdigest()

HOST = '0.0.0.0'
PORT = 65432

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen()
    conn, addr = s.accept()
    with conn:
        while True:
            data = conn.recv(1024)
            if not data:
                break
            # Return packet with checksum
            checksum = calculate_checksum(data)
            response = data + b'|' + checksum.encode()
            conn.sendall(response)

# client.py (run on Machine 1)
import socket
import time
import hashlib
import statistics

def generate_test_packet(size=512):
    return os.urandom(size)

def verify_response(data):
    packet, checksum = data.rsplit(b'|', 1)
    return hashlib.md5(packet).hexdigest() == checksum.decode()

HOST = 'machine2_ip'  # Replace with actual IP
PORT = 65432
NUM_PACKETS = 1000
latencies = []

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((HOST, PORT))
    for i in range(NUM_PACKETS):
        packet = generate_test_packet()
        start_time = time.time()
        s.sendall(packet)
        data = s.recv(1024)
        latency = (time.time() - start_time) * 1000  # in ms
        latencies.append(latency)
        
        if not verify_response(data):
            print(f"Packet verification failed for packet {i}")
            
    print(f"Average latency: {statistics.mean(latencies):.2f}ms")
    print(f"Max latency: {max(latencies):.2f}ms")
    print(f"Min latency: {min(latencies):.2f}ms")

For enterprise environments, consider these established tools:

1. iPerf3

While primarily a bandwidth tool, iPerf3 can be configured for two-way testing:


# On Machine 2 (server):
iperf3 -s

# On Machine 1 (client):
iperf3 -c machine2_ip --bidir --time 60

2. Ntttcp

Microsoft's network testing tool supports verification mode:


# Receiver:
ntttcp -r -m 4,0,192.168.1.2 -a 4 -t 60 -x

# Sender:
ntttcp -s -m 4,0,192.168.1.2 -a 4 -t 60 -x -v

3. Custom Wireshark Analysis

For protocol-specific testing, setup Wireshark with display filters:


# Filter for round-trip TCP packets
tcp.stream eq 1 && tcp.analysis.ack_rtt > 0
  • Disable interrupt moderation on NICs for microsecond precision
  • Use hardware timestamps when available (PTP/1588 support)
  • Consider kernel bypass techniques like DPDK for high-frequency testing
  • Run tests during off-peak hours for baseline measurements

When evaluating network performance between two endpoints, we often need to test:

  • Round-trip latency under heavy load
  • Packet loss rates during sustained transmission
  • Data integrity verification mechanisms
  • Throughput consistency over time

Common tools like ping or iPerf don't fully address the specific requirement of:

1. Bidirectional packet verification
2. Continuous stress testing with payload validation
3. Dynamic packet generation

Here's a Python implementation using sockets and hashing for verification:

import socket
import hashlib
import time
import random

def generate_packet():
    payload = str(random.getrandbits(256)).encode()
    checksum = hashlib.sha256(payload).hexdigest()
    return payload + b'|' + checksum.encode()

def verify_packet(data):
    payload, received_checksum = data.split(b'|')
    calculated_checksum = hashlib.sha256(payload).hexdigest().encode()
    return calculated_checksum == received_checksum

def stress_test(host, port, duration=60):
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.connect((host, port))
        start_time = time.time()
        packets_sent = 0
        successful_roundtrips = 0
        
        while time.time() - start_time < duration:
            packet = generate_packet()
            s.sendall(packet)
            response = s.recv(1024)
            
            if verify_packet(response):
                successful_roundtrips += 1
            packets_sent += 1
            
        print(f"Test completed: {packets_sent} packets sent, "
              f"{successful_roundtrips} successful roundtrips")
Tool Bidirectional Payload Verification Stress Capability
ping No No Limited
iPerf Yes No High
netperf Yes Basic High
Custom Solution Yes Full Configurable

For high-performance testing scenarios:

# Use UDP instead of TCP for reduced overhead
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# Implement packet buffering for higher throughput
packet_buffer = [generate_packet() for _ in range(1000)]
for packet in packet_buffer:
    s.sendto(packet, (host, port))

For production environments consider:

  • Spirent TestCenter
  • Ixia BreakingPoint
  • Ostinato Traffic Generator