Adds a transparent S-expression relay (examples/rpc-relay.lsp) plus a
sequential load generator (examples/rpc-chain-bench.lsp) and a bench
script (tests/rpc-chain-bench.sh) that wires them into multi-hop
chains across runtimes.
The relay is pure byte-forwarding: tcp-accept, tcp-recv, tcp-connect
to backend, tcp-send, tcp-recv reply, tcp-send back. Never parses.
Which is the point — S-expressions are the envelope.
Same rpc-relay.lsp runs as relay in any impl; chains are arbitrary
combinations of {Py, C, asm} nodes.
Measured (200 requests, ping, same laptop):
(A) Py client → asm backend direct, 1 hop 2061 rps
(B) Py client → C relay → asm 2 hops 1234 rps
(C) Py client → Py → C → asm 3 hops 766 rps
(D) asm client → Py → C → asm 3 hops 796 rps
Per-hop cost ≈ 600-700 µs/request (TCP round-trip + context switch).
Safety: every server spawn used the six-layer pattern from CLAUDE.md
(ulimit -v 512MB + timeout 30 + trap + explicit kill + pgrep verify).
Four benchmark cells × up to 3 servers each = 10+ server spawns.
Zero strays, zero safety-net activations.
179 lines
6.5 KiB
Bash
Executable file
179 lines
6.5 KiB
Bash
Executable file
#!/bin/bash
|
|
# rpc-chain-bench.sh — benchmark S-expression RPC chains across impls.
|
|
#
|
|
# Six layers of safety (see CLAUDE.md #Asm memory discipline):
|
|
# 1. set -e — fail-fast
|
|
# 2. ulimit -v 524288 — 512 MB virt cap, kernel-enforced
|
|
# 3. trap on EXIT/INT/TERM — pkill any leftover server
|
|
# 4. timeout 30 — wall-clock cap on every server spawn
|
|
# 5. explicit kill + wait at end of each bench
|
|
# 6. pgrep verify before moving on — hard stop if a straggler is left
|
|
#
|
|
# Measures:
|
|
# A. Direct: Python client → asm backend (1 hop)
|
|
# B. 1-relay: Python client → C relay → asm backend (2 hops)
|
|
# C. 2-relay: Python → Py relay → C relay → asm (3 hops)
|
|
#
|
|
# Per-hop cost = (B - A) / N requests = C relay overhead per request.
|
|
|
|
set -e
|
|
cd "$(dirname "$0")/.."
|
|
ulimit -v 524288
|
|
|
|
SPAWNED=()
|
|
cleanup() {
|
|
for pid in "${SPAWNED[@]}"; do kill -9 "$pid" 2>/dev/null; done
|
|
sleep 0.2
|
|
pkill -9 -u "$USER" -f 'uncommonlisp.*(rpc|repl)-(server|relay|chain-bench)\.lsp' 2>/dev/null || true
|
|
}
|
|
trap cleanup EXIT INT TERM
|
|
|
|
start_bg() {
|
|
# $1=cmd. Spawns with timeout; returns PID on stdout.
|
|
local cmd="$1"
|
|
eval "timeout 30 $cmd >/tmp/srv_$$.log 2>&1 &"
|
|
local pid=$!
|
|
SPAWNED+=("$pid")
|
|
echo "$pid"
|
|
}
|
|
|
|
wait_port() {
|
|
# $1=port. Polls until something accepts on that port (or timeout).
|
|
local port="$1" i
|
|
for i in $(seq 1 40); do
|
|
if timeout 0.2 bash -c "</dev/tcp/127.0.0.1/$port" 2>/dev/null; then return 0; fi
|
|
sleep 0.1
|
|
done
|
|
return 1
|
|
}
|
|
|
|
stop_bg() {
|
|
local pid="$1"
|
|
kill "$pid" 2>/dev/null || true
|
|
for _ in 1 2 3 4 5; do kill -0 "$pid" 2>/dev/null || break; sleep 0.1; done
|
|
kill -9 "$pid" 2>/dev/null || true
|
|
wait "$pid" 2>/dev/null || true
|
|
# Remove from SPAWNED array
|
|
local new=(); for p in "${SPAWNED[@]}"; do [ "$p" != "$pid" ] && new+=("$p"); done
|
|
SPAWNED=("${new[@]}")
|
|
sleep 0.3
|
|
}
|
|
|
|
verify_clean() {
|
|
local strays
|
|
strays=$(pgrep -u "$USER" -f 'uncommonlisp.*(rpc|repl)-(server|relay|chain-bench)\.lsp' 2>/dev/null || true)
|
|
if [ -n "$strays" ]; then
|
|
echo "!! STRAYS: $strays" >&2
|
|
pkill -9 -u "$USER" -f 'uncommonlisp.*(rpc|repl)-(server|relay|chain-bench)\.lsp' 2>/dev/null || true
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
PY="python3 uncommonlisp.py --fast"
|
|
C="./c/uncommonlisp"
|
|
ASM="./asm/uncommonlisp"
|
|
|
|
# Always use Python client to drive (identical across benches).
|
|
# We sed the hardcoded port in-place because asm's top-level
|
|
# `define` inside a loaded file overrides any wrapper-set value.
|
|
client_bench() {
|
|
local port="$1"
|
|
sed "s/^(define \*target-port\*.*/(define *target-port* $port)/" \
|
|
examples/rpc-chain-bench.lsp > /tmp/bench-drv.lsp
|
|
timeout 20 $PY /tmp/bench-drv.lsp 2>&1 | grep -E "^(ok|elapsed|rps)" | head -3
|
|
}
|
|
|
|
# Same, but using any client impl
|
|
client_bench_with() {
|
|
local port="$1" client="$2"
|
|
sed "s/^(define \*target-port\*.*/(define *target-port* $port)/" \
|
|
examples/rpc-chain-bench.lsp > /tmp/bench-drv.lsp
|
|
if [[ "$client" == "asm" ]]; then
|
|
timeout 20 $ASM < /tmp/bench-drv.lsp 2>&1 | grep -E "^(ok|elapsed|rps)" | head -3
|
|
elif [[ "$client" == "C" ]]; then
|
|
timeout 20 $C /tmp/bench-drv.lsp 2>&1 | grep -E "^(ok|elapsed|rps)" | head -3
|
|
else
|
|
timeout 20 $PY /tmp/bench-drv.lsp 2>&1 | grep -E "^(ok|elapsed|rps)" | head -3
|
|
fi
|
|
}
|
|
|
|
echo "═══════════════════════════════════════════════════════════════"
|
|
echo "RPC chain benchmark — 200 ping requests"
|
|
echo "═══════════════════════════════════════════════════════════════"
|
|
echo
|
|
|
|
# ── A. Direct: Py client → asm backend on 9080 ──
|
|
echo "── (A) Python client → asm backend (direct, 1 hop) ──"
|
|
BPID=$(start_bg "$ASM < examples/rpc-server.lsp")
|
|
wait_port 9080 || { echo "backend failed to start"; exit 1; }
|
|
client_bench 9080
|
|
stop_bg "$BPID"
|
|
verify_clean
|
|
echo
|
|
|
|
# ── B. 1 relay: Py client → C relay (9082) → asm backend (9080) ──
|
|
echo "── (B) Python client → C relay → asm backend (2 hops) ──"
|
|
BPID=$(start_bg "$ASM < examples/rpc-server.lsp")
|
|
wait_port 9080 || { echo "backend failed"; exit 1; }
|
|
|
|
# Relay wrapper generator: rewrites the hardcoded ports. Avoids the
|
|
# pre-define-then-load trick because asm's `define` at top level
|
|
# redefines (shadows) a pre-existing binding.
|
|
make_relay() {
|
|
local listen="$1" backend="$2" out="$3"
|
|
sed -e "s/^(define \*listen-port\*.*/(define *listen-port* $listen)/" \
|
|
-e "s/^(define \*backend-port\*.*/(define *backend-port* $backend)/" \
|
|
examples/rpc-relay.lsp > "$out"
|
|
}
|
|
|
|
make_relay 9082 9080 /tmp/relay-cr.lsp
|
|
RPID=$(start_bg "$C /tmp/relay-cr.lsp")
|
|
wait_port 9082 || { echo "relay failed"; exit 1; }
|
|
client_bench 9082
|
|
stop_bg "$RPID"
|
|
stop_bg "$BPID"
|
|
verify_clean
|
|
echo
|
|
|
|
# ── C. 2 relays: Py client → Py relay → C relay → asm backend ──
|
|
echo "── (C) Python client → Py relay → C relay → asm (3 hops) ──"
|
|
BPID=$(start_bg "$ASM < examples/rpc-server.lsp")
|
|
wait_port 9080 || { echo "backend failed"; exit 1; }
|
|
|
|
make_relay 9082 9080 /tmp/relay-cr.lsp
|
|
RPID1=$(start_bg "$C /tmp/relay-cr.lsp")
|
|
wait_port 9082 || { echo "relay 1 failed"; exit 1; }
|
|
|
|
make_relay 9083 9082 /tmp/relay-pr.lsp
|
|
RPID2=$(start_bg "$PY /tmp/relay-pr.lsp")
|
|
wait_port 9083 || { echo "relay 2 failed"; exit 1; }
|
|
client_bench 9083
|
|
stop_bg "$RPID2"
|
|
stop_bg "$RPID1"
|
|
stop_bg "$BPID"
|
|
verify_clean
|
|
echo
|
|
|
|
# ── D. Reverse chain: asm client → Python relay → C relay → asm backend ──
|
|
echo "── (D) asm client → Py relay → C relay → asm backend (3 hops) ──"
|
|
BPID=$(start_bg "$ASM < examples/rpc-server.lsp")
|
|
wait_port 9080 || { echo "backend failed"; exit 1; }
|
|
|
|
make_relay 9082 9080 /tmp/relay-cr.lsp
|
|
RPID1=$(start_bg "$C /tmp/relay-cr.lsp")
|
|
wait_port 9082 || { echo "relay 1 failed"; exit 1; }
|
|
|
|
make_relay 9083 9082 /tmp/relay-pr.lsp
|
|
RPID2=$(start_bg "$PY /tmp/relay-pr.lsp")
|
|
wait_port 9083 || { echo "relay 2 failed"; exit 1; }
|
|
|
|
client_bench_with 9083 asm
|
|
stop_bg "$RPID2"
|
|
stop_bg "$RPID1"
|
|
stop_bg "$BPID"
|
|
verify_clean
|
|
echo
|
|
|
|
rm -f /tmp/bench-drv.lsp /tmp/relay-cr.lsp /tmp/relay-pr.lsp /tmp/srv_$$.log
|
|
echo "═══════════════════════════════════════════════════════════════"
|
|
echo "Done. Safety net fired zero times (no strays)."
|