When testing network performance or monitoring tools, you often need to generate sustained network traffic between hosts at a specific rate. While tools like iperf exist for short-term bandwidth testing, they aren't ideal for long-running scenarios where you need:
- Constant traffic flow for days or weeks
- Precise control over bandwidth rates
- Minimal CPU overhead
- Cross-platform compatibility (Windows/macOS)
One effective approach is combining packet generation with traffic control utilities. Here's a solution using tcpreplay and custom scripts:
# First, create a sample pcap file with random packets
dd if=/dev/urandom of=random_data.bin bs=1M count=100
editcap -c 1000 random_data.bin random_traffic.pcap
# Then replay it at controlled rates (10Mbps in this case)
tcpreplay --loop=0 --mbps=10 -i eth0 random_traffic.pcap
For more control, here's a Python solution using sockets and traffic shaping:
import socket
import time
import random
def generate_traffic(target_ip, target_port, duration_sec, rate_mbps):
packet_size = 1400 # bytes
packets_per_second = (rate_mbps * 125000) / packet_size
delay = 1.0 / packets_per_second
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
start_time = time.time()
while time.time() - start_time < duration_sec:
data = bytes([random.randint(0, 255) for _ in range(packet_size)])
sock.sendto(data, (target_ip, target_port))
time.sleep(delay)
# Example usage:
generate_traffic("192.168.1.100", 9999, 604800, 10) # 1 week at 10Mbps
For multi-week operation, consider these optimizations:
- Use UDP instead of TCP to avoid connection overhead
- Implement proper error handling and reconnection logic
- Add logging to monitor the generator's health
- Consider running in a container for better resource control
The Python solution works across platforms, but for Windows-specific optimizations:
# Windows performance tweaks
import win32api
import win32process
win32process.SetPriorityClass(win32api.GetCurrentProcess(),
win32process.BELOW_NORMAL_PRIORITY_CLASS)
While generating traffic, monitor its impact with:
# On Linux/macOS:
iftop -i eth0 -B -f "port 9999"
# On Windows (PowerShell):
Get-NetTCPConnection -State Established | Where-Object {$_.RemotePort -eq 9999}
When testing network monitoring tools or evaluating QoS configurations, you often need sustained background traffic that runs for extended periods (days/weeks) at precise bandwidth levels. While utilities like iperf work for short bursts, they aren't optimized for long-duration, low-CPU scenarios.
Traditional bandwidth testers focus on maximum throughput measurement rather than steady-state traffic generation. They create TCP/UDP streams that either:
- Flood the network (iperf)
- Require complex scripting (tcpreplay)
- Lack rate control (ping flood)
For Windows/macOS systems, we can combine native utilities with simple scripts to create controlled traffic flows. Here's a multi-platform solution:
# macOS/Linux (run on both hosts)
while true; do
dd if=/dev/urandom bs=1M count=10 | nc -l 5000 &
sleep 0.1
nc [DEST_IP] 5000 > /dev/null &
sleep 300 # Adjust interval for desired average rate
done
:: Windows PowerShell (requires admin)
while ($true) {
$random = New-Object byte[] 1MB
(New-Object Random).NextBytes($random)
$random | Out-Network -Port 5000 -IPAddress [DEST_IP]
Start-Sleep -Milliseconds 100
}
Key considerations for week-long operation:
- Use UDP instead of TCP to avoid congestion control
- Implement packet size variation (500-1500 bytes)
- Add random sleep intervals (0-200ms)
- Log start/stop times for verification
When scripting isn't ideal, these specialized utilities can help:
Tool | Platform | Protocol | Rate Control |
---|---|---|---|
ostinato | Cross-platform | L2-L4 | Precise |
trafgen | Linux/macOS | Raw packets | Microsecond |
SoftPerfect | Windows | TCP/UDP | GUI config |
Use Wireshark with these display filters to confirm behavior:
# Check packet size variation
frame.len >= 500 && frame.len <= 1500
# Verify rate over time
io.stat_interval==1 && tcp || udp