#!/usr/bin/env bash
# Benchmark: nginx (:8080, already running) vs veld (:8081)
# Reports the PEAK Requests/sec over N repetitions per case to suppress the
# noise inherent in an oversubscribed (wrk + workers) shared machine.
# Usage: bench.sh [duration] [label] [reps]
set -u
DUR="${1:-8}"
LABEL="${2:-run}"
REPS="${3:-3}"
BENCH=/tmp/bench
RN_BIN="${RN_BIN:-$HOME/veld/target/release/veld}"
RESULTS="$BENCH/results-$LABEL.txt"
start_rust() {
pkill -f "veld" 2>/dev/null
sleep 1
"$RN_BIN" -c "$BENCH/veld.conf" --prefix "$BENCH" start >"$BENCH/logs/rn-stdout.log" 2>&1 &
sleep 2
}
# One wrk run -> "rps|lat|xfer"
one() {
wrk -t4 -c"$2" -d"${DUR}s" "$1" 2>/dev/null | awk '
/Requests\/sec/{rps=$2}
/Latency/ && !x {lat=$2; x=1}
/Transfer\/sec/{xfer=$2}
END{printf "%s|%s|%s", rps, lat, xfer}'
}
# Interleave nginx and rust per repetition so both see identical machine
# load; report the peak rps for each.
run_case() {
local name="$1" conns="$2" path="$3"
local nb=0 rb=0 nl="" rl=""
for _ in $(seq 1 "$REPS"); do
IFS='|' read -r nr nlat nx <<<"$(one "http://127.0.0.1:8080${path}" "$conns")"
IFS='|' read -r rr rlat rx <<<"$(one "http://127.0.0.1:8081${path}" "$conns")"
[ -z "$nr" ] && nr=0; [ -z "$rr" ] && rr=0
awk "BEGIN{exit !($nr>$nb)}" && { nb=$nr; nl="rps=$nr lat=$nlat xfer=$nx"; }
awk "BEGIN{exit !($rr>$rb)}" && { rb=$rr; rl="rps=$rr lat=$rlat xfer=$rx"; }
done
local verdict
verdict=$(awk "BEGIN{printf \"%+.0f%%\", ($rb/$nb-1)*100}")
printf "%-16s c=%-4s | nginx: %-30s | rust: %-30s | rust %s\n" "$name" "$conns" "$nl" "$rl" "$verdict" | tee -a "$RESULTS"
}
echo "=== Starting veld ==="
start_rust
echo "=== Sanity check ==="
for p in index.html 1k.txt 10k.txt 100k.txt 1m.txt; do
n=$(curl -s -o /dev/null -w '%{http_code}/%{size_download}' http://127.0.0.1:8080/$p)
r=$(curl -s -o /dev/null -w '%{http_code}/%{size_download}' http://127.0.0.1:8081/$p)
printf " %-12s nginx=%-14s rust=%s\n" "$p" "$n" "$r"
done
: > "$RESULTS"
echo "=== Benchmark peak-of-${REPS} (dur=${DUR}s) label=$LABEL ===" | tee -a "$RESULTS"
run_case "small/index" 10 /index.html
run_case "small/index" 100 /index.html
run_case "small/index" 500 /index.html
run_case "small/1k" 100 /1k.txt
run_case "mid/10k" 100 /10k.txt
run_case "large/100k" 100 /100k.txt
run_case "huge/1m" 50 /1m.txt
run_case "huge/1m" 100 /1m.txt
echo "=== Done ($LABEL) ==="
pkill -f "veld" 2>/dev/null