#!/bin/bash
# Run all benchmarks on localhost and compare results.
#
# Usage:
# ./benchmarks/run-all.sh # run all benchmarks
# ./benchmarks/run-all.sh raw-udx # run just raw-udx
# ./benchmarks/run-all.sh hyperswarm # run just hyperswarm
# ./benchmarks/run-all.sh protomux # run just protomux
set -e
cd "$(dirname "$0")/.."
RESULTS_DIR="benchmarks/results"
mkdir -p "$RESULTS_DIR"
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
run_bench() {
local name=$1
local file=$2
echo ""
echo "================================================================"
echo " Running: $name"
echo "================================================================"
# Capture output, extract JSON at the end
local output
output=$(node "$file" 2>&1) || true
echo "$output" | grep -v "^--- JSON ---$" | grep -v "^{" | grep -v "^}" | grep -v '^\s*"'
# Extract JSON result
local json
json=$(echo "$output" | sed -n '/^--- JSON ---$/,$ p' | tail -n +2)
if [ -n "$json" ]; then
echo "$json" > "$RESULTS_DIR/${name}-${TIMESTAMP}.json"
echo " → Saved to $RESULTS_DIR/${name}-${TIMESTAMP}.json"
fi
}
FILTER="${1:-all}"
if [ "$FILTER" = "all" ] || [ "$FILTER" = "raw-udx" ]; then
run_bench "raw-udx" "benchmarks/bench-raw-udx.cjs"
fi
if [ "$FILTER" = "all" ] || [ "$FILTER" = "hyperswarm" ]; then
run_bench "hyperswarm" "benchmarks/bench-hyperswarm.cjs"
fi
if [ "$FILTER" = "all" ] || [ "$FILTER" = "protomux" ]; then
run_bench "protomux" "benchmarks/bench-protomux.cjs"
fi
echo ""
echo "================================================================"
echo " All benchmarks complete. Results in $RESULTS_DIR/"
echo "================================================================"
# Print comparison table if all three ran
if [ "$FILTER" = "all" ]; then
echo ""
echo "=== COMPARISON TABLE ==="
echo ""
printf "%-12s | %-10s | %-10s | %-10s | %-10s | %-12s | %-10s\n" \
"Transport" "PktSize" "Avg RTT" "P95 RTT" "Jitter" "Throughput" "Pkts/sec"
printf "%-12s-+-%-10s-+-%-10s-+-%-10s-+-%-10s-+-%-12s-+-%-10s\n" \
"------------" "----------" "----------" "----------" "----------" "------------" "----------"
for f in "$RESULTS_DIR"/*-${TIMESTAMP}.json; do
[ -f "$f" ] || continue
transport=$(node -e "const d=require('./$f'); console.log(d.transport)")
node -e "
const d = require('./$f');
for (const r of d.results) {
const t = d.transport.padEnd(12);
const s = (r.packetSize + 'B').padEnd(10);
const a = (r.avgRTT + 'ms').padEnd(10);
const p = (r.p95RTT + 'ms').padEnd(10);
const j = (r.jitter + 'ms').padEnd(10);
const tp = (r.throughputMBs + 'MB/s').padEnd(12);
const pp = String(r.packetsSec).padEnd(10);
console.log(t + ' | ' + s + ' | ' + a + ' | ' + p + ' | ' + j + ' | ' + tp + ' | ' + pp);
}
" 2>/dev/null || true
done
fi