# 1. THE MOST CRITICAL — process fd limit is only 256! sudo launchctl limit maxfiles 65536 200000 # Then restart your terminal or run: ulimit -n 65536 # Verify ulimit -n # must show 65536 # 2. TCP listen backlog sudo sysctl -w kern.ipc.somaxconn=8192 # 3. Reduce TIME_WAIT holddown sudo sysctl -w net.inet.tcp.msl=1000 # 4. Widen ephemeral port range for load tester sudo sysctl -w net.inet.ip.portrange.first=1024 # Verify all echo "somaxconn: $(sysctl -n kern.ipc.somaxconn)" echo "msl: $(sysctl -n net.inet.tcp.msl)" echo "ports: $(sysctl -n net.inet.ip.portrange.first)-$(sysctl -n net.inet.ip.portrange.last)" echo "fd limit: $(ulimit -n)" # In a second terminal while the test runs watch -n1 'netstat -an | grep 8080 | grep -c ESTABLISHED' # Option 1: install it brew install watch watch -n1 'netstat -an | grep 8080 | grep -c ESTABLISHED' # Option 2: pure shell loop (no install needed) while true; do clear echo "=== $(date) ===" echo "ESTABLISHED: $(netstat -an | grep 8080 | grep -c ESTABLISHED)" echo "TIME_WAIT: $(netstat -an | grep 8080 | grep -c TIME_WAIT)" echo "CLOSE_WAIT: $(netstat -an | grep 8080 | grep -c CLOSE_WAIT)" echo "SYN_RCVD: $(netstat -an | grep 8080 | grep -c SYN_RCVD)" echo "Total fds: $(lsof -p $(pgrep broadcast_server) 2>/dev/null | wc -l)" sleep 1 done SERVER_PID=$(pgrep broadcast_server) while true; do clear echo "=== $(date) ===" echo "--- Network ---" echo "ESTABLISHED: $(netstat -an | grep 8080 | grep -c ESTABLISHED)" echo "TIME_WAIT: $(netstat -an | grep 8080 | grep -c TIME_WAIT)" echo "CLOSE_WAIT: $(netstat -an | grep 8080 | grep -c CLOSE_WAIT)" echo "SYN_RCVD: $(netstat -an | grep 8080 | grep -c SYN_RCVD)" echo "--- Process (PID: $SERVER_PID) ---" FD_COUNT=$(lsof -p $SERVER_PID 2>/dev/null | wc -l) echo "Open fds: $FD_COUNT / 65536" echo "--- System ---" echo "Ports in use: $(netstat -an | grep -c tcp)" sleep 1 done # CPU profiling with flamegraph cargo install flamegraph cargo flamegraph --bin broadcast_server # Memory profiling cargo install cargo-instruments cargo instruments -t Allocations --features b_server --bin broadcast_server # Time-based profiling on macOS cargo instruments -t "Time Profiler" --features b_server --bin broadcast_server