|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +OUT_DIR=bench_results/$(date +%Y%m%d_%H%M%S) |
| 5 | +mkdir -p "$OUT_DIR" |
| 6 | + |
| 7 | +printf "Building examples...\r\n" >&2 |
| 8 | +cargo build --examples --release > "$OUT_DIR/build.log" 2>&1 |
| 9 | + |
| 10 | +# iperf3 server will listen on 127.0.0.1:5201 |
| 11 | +printf "Starting iperf3 server... (logs -> $OUT_DIR/iperf3_server.log)\r\n" >&2 |
| 12 | +iperf3 -s -p 5201 >"$OUT_DIR/iperf3_server.log" 2>&1 & |
| 13 | +IPERF3_SRV_PID=$! |
| 14 | + |
| 15 | +# start the tun example (requires root). We will try to launch it via sudo; if that |
| 16 | +# fails (password prompt/backgrounding issues), prompt the user to start it manually |
| 17 | +# in another terminal and press ENTER to continue. |
| 18 | +EXAMPLE_BIN=target/release/examples/tun |
| 19 | +if [ ! -x "$EXAMPLE_BIN" ]; then |
| 20 | + printf "Example binary not found: $EXAMPLE_BIN\r\n" >&2 |
| 21 | + kill $IPERF3_SRV_PID || true |
| 22 | + exit 1 |
| 23 | +fi |
| 24 | + |
| 25 | +printf "Attempting to start the TUN example via sudo (may prompt for password)...\r\n" >&2 |
| 26 | +if command -v sudo >/dev/null 2>&1; then |
| 27 | + # Ask for sudo credential upfront so backgrounded sudo won't hang |
| 28 | + sudo -v || true |
| 29 | + sudo "$EXAMPLE_BIN" --server-addr 127.0.0.1:5201 < /dev/null >"$OUT_DIR/tun_example.log" 2>&1 & |
| 30 | + TUN_PID=$! |
| 31 | + # Reset terminal in case sudo changed it |
| 32 | + stty sane || true |
| 33 | + sleep 1 |
| 34 | + if kill -0 "$TUN_PID" >/dev/null 2>&1; then |
| 35 | + printf "TUN example started (pid $TUN_PID), logs -> $OUT_DIR/tun_example.log\r\n" >&2 |
| 36 | + else |
| 37 | + echo "Automatic sudo start failed or process exited. Please run the following command in another terminal as root:" >&2 |
| 38 | + echo " sudo $EXAMPLE_BIN --server-addr 127.0.0.1:5201 >$OUT_DIR/tun_example.log 2>&1 &" >&2 |
| 39 | + echo >&2 "Press ENTER after you've started the TUN example manually (or Ctrl-C to abort)" |
| 40 | + read -r _ |
| 41 | + TUN_PID=0 |
| 42 | + fi |
| 43 | +else |
| 44 | + echo "sudo not found; please start the TUN example manually in another terminal as root:" >&2 |
| 45 | + echo " sudo $EXAMPLE_BIN --server-addr 127.0.0.1:5201 >$OUT_DIR/tun_example.log 2>&1 &" >&2 |
| 46 | + echo >&2 "Press ENTER after you've started the TUN example manually (or Ctrl-C to abort)" |
| 47 | + read -r _ |
| 48 | + TUN_PID=0 |
| 49 | +fi |
| 50 | + |
| 51 | +# wait a moment for tun and ip stack to initialize |
| 52 | +sleep 3 |
| 53 | + |
| 54 | +# run iperf3 tests (JSON output saved) |
| 55 | +printf "Running client -> server test...\r\n" >&2 |
| 56 | +iperf3 -c 10.3.0.1 -J > "$OUT_DIR/iperf_client_to_server.json" 2> "$OUT_DIR/iperf_client_to_server.err" || true |
| 57 | + |
| 58 | +printf "Running server -> client (reverse) test...\r\n" >&2 |
| 59 | +iperf3 -c 10.3.0.1 -R -J > "$OUT_DIR/iperf_server_to_client.json" 2> "$OUT_DIR/iperf_server_to_client.err" || true |
| 60 | + |
| 61 | +# give a moment to flush logs |
| 62 | +sleep 3 |
| 63 | + |
| 64 | +# stop background processes |
| 65 | +sudo kill $TUN_PID || true |
| 66 | +kill $IPERF3_SRV_PID || true |
| 67 | + |
| 68 | +printf "Benchmark finished. Results saved to: $OUT_DIR\r\n" >&2 |
| 69 | + |
| 70 | +printf "To inspect results:\r\n" >&2 |
| 71 | +printf " ls -l $OUT_DIR\r\n" >&2 |
| 72 | +printf " jq . < $OUT_DIR/iperf_client_to_server.json\r\n" >&2 |
| 73 | + |
| 74 | +printf "If you don't want the script to require root, instead run the tun example manually as root, then run the two iperf3 commands:\r\n" >&2 |
| 75 | + |
| 76 | +printf " # start iperf3 server (local): iperf3 -s -p 5201 &\r\n" >&2 |
| 77 | +printf " # run client->server: iperf3 -c 10.3.0.1 -J > client.json\r\n" >&2 |
| 78 | +printf " # run reverse: iperf3 -c 10.3.0.1 -R -J > reverse.json\r\n" >&2 |
| 79 | + |
| 80 | +# Reset terminal |
| 81 | +stty sane || true |
| 82 | + |
| 83 | +printf "\r\n" >&2 |
0 commit comments