Files
zigp2p/docker-tests/README.md
2026-02-06 21:03:59 -03:00

196 lines
4.9 KiB
Markdown

# P2P Cross-Machine Testing with Docker
This directory contains Docker-based testing infrastructure for simulating cross-machine P2P file transfers on a single host.
## Overview
```
┌─────────────────────────────────────────────────────────────┐
│ p2p-test-network │
│ (172.28.0.0/16) │
│ │
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
│ │ seeder │◄───│leecher-1│ │leecher-2│ ... │
│ │172.28.1.│ │ :8081 │ │ :8082 │ │
│ │ :8080 │ └─────────┘ └─────────┘ │
│ └─────────┘ │
└─────────────────────────────────────────────────────────────┘
```
## Quick Start
```bash
# Run basic test (1 seeder, 3 leechers, 10MB file)
./run-tests.sh
# Run with more leechers
./run-tests.sh --leechers 10
# Run with larger file
./run-tests.sh --file-size 100
# Run with network chaos (requires pumba)
./run-tests.sh --chaos
# Clean up
./run-tests.sh --clean
```
## Manual Docker Commands
```bash
# Build the image
docker compose build
# Start services (3 leechers by default)
docker compose up -d
# Scale leechers
docker compose up -d --scale leecher=10
# View logs
docker compose logs -f
# View specific service logs
docker compose logs -f seeder
docker compose logs -f leecher
# Stop everything
docker compose down -v
```
## Configuration
Environment variables (set in shell or `.env` file):
| Variable | Default | Description |
|----------|---------|-------------|
| `LEECHER_COUNT` | 3 | Number of leecher containers |
| `FILE_SIZE_MB` | 10 | Test file size in megabytes |
| `PIECE_SIZE_KB` | 256 | Piece size in kilobytes |
## Network Chaos Testing
For realistic network simulation, install [Pumba](https://github.com/alexei-led/pumba):
```bash
# macOS
brew install pumba
# Linux
curl -L https://github.com/alexei-led/pumba/releases/download/0.9.0/pumba_linux_amd64 -o pumba
chmod +x pumba
sudo mv pumba /usr/local/bin/
```
### Chaos Scenarios
```bash
# Add 100ms latency to all leechers
pumba netem --duration 5m delay --time 100 \
$(docker ps -q -f "name=leecher")
# Add 5% packet loss
pumba netem --duration 5m loss --percent 5 \
$(docker ps -q -f "name=leecher")
# Add latency + jitter + packet loss
pumba netem --duration 5m \
delay --time 50 --jitter 20 \
loss --percent 2 \
$(docker ps -q -f "name=leecher")
# Bandwidth limit (1 Mbps)
pumba netem --duration 5m rate --rate 1mbit \
$(docker ps -q -f "name=leecher")
```
## Debug Mode
Start with a network debugging container:
```bash
# Start with debug profile
docker compose --profile debug up -d
# Access the debug container
docker exec -it p2p-monitor bash
# Inside debug container:
ping seeder
nslookup seeder
tcpdump -i eth0 port 8080
iperf3 -c seeder -p 8080
```
## Files
| File | Description |
|------|-------------|
| `Dockerfile` | Multi-stage build for Zig P2P peer |
| `docker-compose.yml` | Service definitions |
| `peer.zig` | Test peer binary source |
| `run-tests.sh` | Test runner script |
| `README.md` | This file |
## Test Scenarios
### Basic Transfer Test
```bash
./run-tests.sh --leechers 1 --file-size 10
```
### Multi-Peer Scaling Test
```bash
./run-tests.sh --leechers 20 --file-size 50
```
### Large File Test
```bash
./run-tests.sh --leechers 5 --file-size 500
```
### Stress Test with Network Issues
```bash
# Terminal 1: Start test
./run-tests.sh --leechers 10 --file-size 100
# Terminal 2: Add network chaos
pumba netem --duration 10m delay --time 100 --jitter 50 loss --percent 3 \
$(docker ps -q -f "name=leecher")
```
## Troubleshooting
### Build fails
```bash
# Check Zig version matches (0.15.2)
docker compose build --no-cache --progress=plain 2>&1 | tee build.log
```
### Containers not communicating
```bash
# Check network
docker network inspect docker-tests_p2p-network
# Check DNS resolution
docker exec -it p2p-seeder ping leecher
```
### Out of memory
```bash
# Check resource limits in docker-compose.yml
# Increase Docker memory allocation in Docker Desktop settings
```
## Performance Metrics
Expected results on a typical development machine:
| Scenario | Throughput | Notes |
|----------|------------|-------|
| 1 leecher, 10MB | 50-100 MB/s | Loopback overhead |
| 5 leechers, 100MB | 30-50 MB/s | Concurrent transfers |
| 10 leechers, 100MB | 20-40 MB/s | More contention |
| With 50ms latency | 5-15 MB/s | Network delay impact |