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))