|
| 1 | +#!/bin/bash |
| 2 | +# Bench relay vs direct steady-state throughput. |
| 3 | +# |
| 4 | +# Uses `pilotctl bench` which opens ONE long-lived connection to the |
| 5 | +# echo port and pumps data through it — that gives clean steady-state |
| 6 | +# numbers (the per-file send-file path was hitting retransmit stalls |
| 7 | +# from connection-setup churn between iterations; see P1-010). |
| 8 | + |
| 9 | +set -u |
| 10 | +export COMPOSE_PROJECT_NAME="pilot-bench" |
| 11 | +export PILOT_SUBNET_PREFIX="172.29.99" |
| 12 | + |
| 13 | +DC="docker compose -f docker-compose.multi.yml -f docker-compose.multi.bench.yml" |
| 14 | + |
| 15 | +cd "$(dirname "$0")" || exit 1 |
| 16 | +# shellcheck source=chaos_helpers.sh |
| 17 | +source ./chaos_helpers.sh |
| 18 | + |
| 19 | +cleanup() { |
| 20 | + [ -n "${IP_A:-}" ] && [ -n "${IP_B:-}" ] && { |
| 21 | + heal_partition agent-a "$IP_B" >/dev/null 2>&1 || true |
| 22 | + heal_partition agent-b "$IP_A" >/dev/null 2>&1 || true |
| 23 | + } |
| 24 | + $DC down -v >/dev/null 2>&1 |
| 25 | +} |
| 26 | +trap cleanup EXIT |
| 27 | + |
| 28 | +SIZES_MB="${BENCH_SIZES_MB:-1 5 10 25 50}" |
| 29 | +ITER="${BENCH_ITER:-3}" |
| 30 | + |
| 31 | +RESULTS="" |
| 32 | +record() { RESULTS="${RESULTS}$1 $2 $3 $4 $5 |
| 33 | +"; } |
| 34 | + |
| 35 | +# Inner: run pilotctl bench, parse send-throughput from --json output. |
| 36 | +# Returns "rc dur_ms send_MBps total_MBps". |
| 37 | +time_bench() { |
| 38 | + local mode="$1" size_mb="$2" |
| 39 | + local out rc |
| 40 | + out=$($DC exec -T agent-a pilotctl --json bench agent-b "$size_mb" --timeout 600s 2>&1) |
| 41 | + rc=$? |
| 42 | + |
| 43 | + local sent_bytes recv_bytes send_dur total_dur send_mbps total_mbps total_dur_ms |
| 44 | + if [ "$rc" -eq 0 ]; then |
| 45 | + sent_bytes=$(echo "$out" | jq -r '.data.sent_bytes // empty') |
| 46 | + recv_bytes=$(echo "$out" | jq -r '.data.recv_bytes // empty') |
| 47 | + send_dur=$(echo "$out" | jq -r '.data.send_duration_ms // empty') |
| 48 | + total_dur=$(echo "$out" | jq -r '.data.total_duration_ms // empty') |
| 49 | + send_mbps=$(echo "$out" | jq -r '.data.send_throughput_mbps // empty') |
| 50 | + total_mbps=$(echo "$out" | jq -r '.data.total_throughput_mbps // empty') |
| 51 | + if [ -z "$total_dur" ] || [ "$total_dur" = "null" ]; then |
| 52 | + # Older bench output: only seconds-level fields. Compute from raw. |
| 53 | + send_dur=$(echo "$out" | jq -r '.data.send_duration_seconds // 0') |
| 54 | + total_dur=$(echo "$out" | jq -r '.data.total_duration_seconds // 0') |
| 55 | + total_dur_ms=$(awk -v s="$total_dur" 'BEGIN{printf "%d", s*1000}') |
| 56 | + send_mbps=$(echo "$out" | jq -r '.data.send_throughput_mbps // 0') |
| 57 | + total_mbps=$(echo "$out" | jq -r '.data.total_throughput_mbps // 0') |
| 58 | + else |
| 59 | + total_dur_ms="$total_dur" |
| 60 | + fi |
| 61 | + # Sanity: recv_bytes must equal sent_bytes (otherwise echo path lost data) |
| 62 | + if [ -n "$recv_bytes" ] && [ -n "$sent_bytes" ] && [ "$recv_bytes" != "$sent_bytes" ]; then |
| 63 | + echo "1 0 0 0" |
| 64 | + return |
| 65 | + fi |
| 66 | + echo "0 ${total_dur_ms:-0} ${send_mbps:-0} ${total_mbps:-0}" |
| 67 | + return |
| 68 | + fi |
| 69 | + echo "1 0 0 0" |
| 70 | +} |
| 71 | + |
| 72 | +run_mode() { |
| 73 | + local mode="$1" |
| 74 | + echo "==> $mode-path measurements (pilotctl bench)" |
| 75 | + for s in $SIZES_MB; do |
| 76 | + for i in $(seq 1 "$ITER"); do |
| 77 | + printf " %-6s %3d MB iter %d ... " "$mode" "$s" "$i" |
| 78 | + read rc dur_ms send_mbps total_mbps < <(time_bench "$mode" "$s") |
| 79 | + if [ "$rc" -eq 0 ]; then |
| 80 | + printf "%6d ms send=%5s MB/s rt=%5s MB/s\n" "$dur_ms" "$send_mbps" "$total_mbps" |
| 81 | + record "$mode" "$s" "$i" "$dur_ms" "$total_mbps" |
| 82 | + else |
| 83 | + printf "FAIL\n" |
| 84 | + record "$mode" "$s" "$i" 0 0 |
| 85 | + fi |
| 86 | + done |
| 87 | + done |
| 88 | +} |
| 89 | + |
| 90 | +# ---------- Boot ---------- |
| 91 | +echo "==> bench: starting fresh stack on $PILOT_SUBNET_PREFIX/24" |
| 92 | +$DC down -v >/dev/null 2>&1 |
| 93 | +$DC up -d rendezvous agent-a agent-b >/dev/null 2>&1 |
| 94 | +for _ in $(seq 1 60); do |
| 95 | + COUNT=$($DC exec -T rendezvous curl -fsS http://127.0.0.1:8080/api/stats 2>/dev/null | jq -r '.total_nodes // 0') |
| 96 | + [ "${COUNT:-0}" -ge 2 ] && break |
| 97 | + sleep 1 |
| 98 | +done |
| 99 | +[ "${COUNT:-0}" -ge 2 ] || { echo "agents did not register" >&2; exit 1; } |
| 100 | + |
| 101 | +IP_A=$(resolve_service_ip agent-a) |
| 102 | +IP_B=$(resolve_service_ip agent-b) |
| 103 | +[ -n "$IP_A" ] && [ -n "$IP_B" ] || { echo "could not resolve agent IPs" >&2; exit 1; } |
| 104 | + |
| 105 | +# Warm tunnel + a short bench round so cwnd is primed. |
| 106 | +$DC exec -T agent-a pilotctl ping agent-b --count 3 --timeout 5s >/dev/null 2>&1 || true |
| 107 | +$DC exec -T agent-a pilotctl bench agent-b 1 --timeout 30s >/dev/null 2>&1 || true |
| 108 | + |
| 109 | +# ---------- Direct ---------- |
| 110 | +run_mode direct |
| 111 | + |
| 112 | +# ---------- Apply partition for relay ---------- |
| 113 | +echo "==> partitioning a<->b for relay measurements" |
| 114 | +apply_partition agent-a "$IP_B" |
| 115 | +apply_partition agent-b "$IP_A" |
| 116 | +sleep 12 |
| 117 | +$DC exec -T agent-a pilotctl bench agent-b 1 --timeout 30s >/dev/null 2>&1 || true |
| 118 | + |
| 119 | +run_mode relay |
| 120 | + |
| 121 | +# ---------- Summary ---------- |
| 122 | +echo |
| 123 | +echo "==> raw results (mode size_MB iter dur_ms total_MB/s)" |
| 124 | +printf '%s' "$RESULTS" |
| 125 | + |
| 126 | +echo |
| 127 | +echo "==> per-(mode,size) statistics across ${ITER} iterations" |
| 128 | +printf "%-7s %-8s %-9s %-9s %-9s %-9s\n" "mode" "size_MB" "min_ms" "median" "max_ms" "med_MB/s" |
| 129 | +echo "$RESULTS" | awk -F'\t' ' |
| 130 | +NF>=5 && $4>0 { |
| 131 | + key=$1"\t"$2; n[key]++; d[key,n[key]]=$4; m[key,n[key]]=$5 |
| 132 | +} |
| 133 | +END { |
| 134 | + for (k in n) { |
| 135 | + cnt = n[k] |
| 136 | + for (i=1;i<=cnt;i++) ms[i]=d[k,i]+0 |
| 137 | + for (i=1;i<=cnt;i++) for (j=i+1;j<=cnt;j++) |
| 138 | + if (ms[j]<ms[i]) {t=ms[i];ms[i]=ms[j];ms[j]=t} |
| 139 | + med_ms = (cnt % 2 == 1) ? ms[(cnt+1)/2] : (ms[cnt/2]+ms[cnt/2+1])/2 |
| 140 | + for (i=1;i<=cnt;i++) mb[i]=m[k,i]+0 |
| 141 | + for (i=1;i<=cnt;i++) for (j=i+1;j<=cnt;j++) |
| 142 | + if (mb[j]<mb[i]) {t=mb[i];mb[i]=mb[j];mb[j]=t} |
| 143 | + med_mbps = (cnt % 2 == 1) ? mb[(cnt+1)/2] : (mb[cnt/2]+mb[cnt/2+1])/2 |
| 144 | + split(k, kk, "\t") |
| 145 | + printf "%-7s %-8s %-9s %-9s %-9s %-9s\n", kk[1], kk[2], ms[1], med_ms, ms[cnt], med_mbps |
| 146 | + } |
| 147 | +}' | sort -k1,1 -k2,2n |
0 commit comments