rpc-chain-bench: Python → C relay → asm, timing end-to-end

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.
This commit is contained in:
russell@unturf.com 2026-04-17 09:18:42 -04:00
parent 574ddc50d7
commit ccf86e3c3f
3 changed files with 273 additions and 0 deletions

View file

@ -0,0 +1,39 @@
;;; rpc-chain-bench.lsp — time N sequential RPC calls to a given port.
;;;
;;; Used to benchmark multi-hop S-expression relays. The same .lsp runs
;;; in all three impls, so the *client* is also portable — any impl
;;; can drive any chain.
;;;
;;; Override *target-port* / *n-requests* / *request* before load.
;;; Default: hit :9080 with (ping) 200 times.
(define *host* "127.0.0.1")
(define *target-port* 9080)
(define *n-requests* 200)
(define *request* "(ping)")
(define (one-call)
(let ((s (tcp-connect *host* *target-port*)))
(tcp-send s *request*)
(let ((r (tcp-recv s 8192)))
(tcp-close s)
(if (and r (> (string-length r) 0)) 1 0))))
(define (call-loop n ok snap)
(if (= n 0) ok
(let ((got (one-call)))
(heap-restore snap)
(call-loop (- n 1) (+ ok got) snap))))
(define t0 (current-time-ms))
(define ok (call-loop *n-requests* 0 (heap-snapshot)))
(define t1 (current-time-ms))
(define elapsed (- t1 t0))
(display "port : ") (display *target-port*) (newline)
(display "requests : ") (display *n-requests*) (newline)
(display "ok : ") (display ok) (newline)
(display "elapsed : ") (display elapsed) (display " ms") (newline)
(display "rps : ")
(display (if (> elapsed 0) (quotient (* *n-requests* 1000) elapsed) 0))
(newline)

55
examples/rpc-relay.lsp Normal file
View file

@ -0,0 +1,55 @@
;;; rpc-relay.lsp — transparent S-expression relay.
;;;
;;; Listens on *listen-port*, forwards every request byte-for-byte to
;;; *backend-host*:*backend-port*, returns the backend's reply. Used
;;; to demonstrate a multi-runtime chain: Python client → C relay →
;;; asm backend, where the same .lsp runs at every hop and the wire
;;; format (Scheme source) needs no translation.
;;;
;;; python3 uncommonlisp.py --fast examples/rpc-relay.lsp
;;; ./c/uncommonlisp examples/rpc-relay.lsp
;;; ./asm/uncommonlisp < examples/rpc-relay.lsp
;;;
;;; Override *listen-port* / *backend-port* by pre-defining before load
;;; (or edit here for a one-off demo).
(define *listen-port* 9082)
(define *backend-host* "127.0.0.1")
(define *backend-port* 9080)
(define *max-requests* 100000)
;;; Forward the raw request bytes to the backend, return the raw reply.
;;; No parsing — the relay never looks inside the sexp. Bytes in,
;;; bytes out. Which is the point: S-expressions are the envelope.
(define (forward-request req)
(let ((back (tcp-connect *backend-host* *backend-port*)))
(if back
(begin
(tcp-send back req)
(let ((reply (tcp-recv back 8192)))
(tcp-close back)
(if (and reply (> (string-length reply) 0))
reply
"(error \"empty from backend\")\n")))
"(error \"backend unreachable\")\n")))
(define server (tcp-listen *listen-port*))
(define (relay-loop n snap)
(if (>= n *max-requests*)
(begin (display "request cap reached, exiting\n") (tcp-close server))
(begin
(let ((client (tcp-accept server)))
(let ((req (tcp-recv client 4096)))
(if (and req (> (string-length req) 0))
(tcp-send client (forward-request req))
#f))
(tcp-close client))
(heap-restore snap)
(relay-loop (+ n 1) snap))))
(display "rpc-relay on :") (display *listen-port*)
(display " -> ") (display *backend-host*) (display ":") (display *backend-port*)
(newline)
(relay-loop 0 (heap-snapshot))

179
tests/rpc-chain-bench.sh Executable file
View file

@ -0,0 +1,179 @@
#!/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)."