#!/bin/bash
# portal-benchmark.sh — measure portal save/load across:
#   1. Same-process save + load per impl + format
#   2. Cross-process (proc A saves, proc B loads) same server, same binary
#   3. Mismatch cases (wrong format, corrupt file, truncated input)
#
# Usage: bash tests/portal-benchmark.sh
#
# Output: one table. Times are wall time averaged over N iterations.

set -u
cd "$(dirname "$0")/.."

PY="python3 lumbda.py --fast"
C="./c/lumbda"
ASM="./asm/lumbda"
N=50

pad() { printf "  %-42s " "$1"; }

time_n() {
    local n=$1 cmd=$2 t0 t1
    t0=$(date +%s.%N)
    for _ in $(seq 1 "$n"); do eval "$cmd" >/dev/null 2>&1; done
    t1=$(date +%s.%N)
    python3 -c "print(f'{(float(\"$t1\")-float(\"$t0\"))*1000/$n:8.3f} ms/iter')"
}

PROLOGUE='(define my-int 42)
(define my-list (list 1 2 3 4 5 6 7 8 9 10))
(define my-str "hello world")
(define (fib n) (let loop ((a 0) (b 1) (i 0)) (if (= i n) a (loop b (+ a b) (+ i 1)))))
(define my-result (fib 30))'

# Save programs that write the same state in sexp / binary / JSON
cat > /tmp/pb-sexp-save.lsp <<EOF
$PROLOGUE
(define p (open-output-file "/tmp/pb-state.sexp"))
(display "(define my-int " p) (write my-int p) (display ")\n" p)
(display "(define my-list '" p) (write my-list p) (display ")\n" p)
(display "(define my-str " p) (write my-str p) (display ")\n" p)
(display "(define my-result " p) (write my-result p) (display ")\n" p)
(close-port p)
EOF
cat > /tmp/pb-bin-save.lsp <<EOF
$PROLOGUE
(portal-save "/tmp/pb-state.binary")
EOF
cat > /tmp/pb-json-save.lsp <<EOF
$PROLOGUE
(portal-save "/tmp/pb-state.json")
EOF
cat > /tmp/pb-noop.lsp <<EOF
$PROLOGUE
EOF
cat > /tmp/pb-sexp-load.lsp <<'EOF'
(load "/tmp/pb-state.sexp")
EOF
cat > /tmp/pb-bin-load.lsp <<'EOF'
(portal-resume "/tmp/pb-state.binary")
EOF

# Pre-materialize files so "load" cases have something to load.
$PY /tmp/pb-sexp-save.lsp >/dev/null 2>&1
$ASM < /tmp/pb-bin-save.lsp >/dev/null 2>&1
$PY /tmp/pb-json-save.lsp >/dev/null 2>&1

echo "═══════════════════════════════════════════════════════════════"
echo "Portal benchmark — $N iterations per case"
echo "   (each iter = one full process: startup + workload + task)"
echo "═══════════════════════════════════════════════════════════════"
echo

echo "PART 1 — full lifetime: startup + build workload + SAVE"
pad "baseline (workload only, no save)"           ; time_n $N "$PY /tmp/pb-noop.lsp"
pad "Python  S-exp save (via ports)"              ; time_n $N "$PY /tmp/pb-sexp-save.lsp"
pad "C       S-exp save (via ports)"              ; time_n $N "$C /tmp/pb-sexp-save.lsp"
pad "Asm     S-exp save (via ports)"              ; time_n $N "$ASM < /tmp/pb-sexp-save.lsp"
pad "Asm     binary portal-save (heap dump)"      ; time_n $N "$ASM < /tmp/pb-bin-save.lsp"
pad "Python  JSON portal-save (graph-aware)"      ; time_n $N "$PY /tmp/pb-json-save.lsp"
echo

echo "PART 2 — full lifetime: startup + LOAD pre-written state"
pad "Python  (load sexp)"                         ; time_n $N "$PY /tmp/pb-sexp-load.lsp"
pad "C       (load sexp)"                         ; time_n $N "$C /tmp/pb-sexp-load.lsp"
pad "Asm     (load sexp)"                         ; time_n $N "$ASM < /tmp/pb-sexp-load.lsp"
pad "Asm     (portal-resume binary)"              ; time_n $N "$ASM < /tmp/pb-bin-load.lsp"
echo

echo "PART 3 — cross-process: proc A saves → proc B loads"
echo "   (wall-time for both procs end-to-end; pipes via file)"
pad "Python → Python  (sexp)"     ; time_n $N "$PY /tmp/pb-sexp-save.lsp && $PY /tmp/pb-sexp-load.lsp"
pad "C      → C       (sexp)"     ; time_n $N "$C /tmp/pb-sexp-save.lsp && $C /tmp/pb-sexp-load.lsp"
pad "Asm    → Asm     (sexp)"     ; time_n $N "$ASM < /tmp/pb-sexp-save.lsp && $ASM < /tmp/pb-sexp-load.lsp"
pad "Asm    → Asm     (binary portal)" ; time_n $N "$ASM < /tmp/pb-bin-save.lsp && $ASM < /tmp/pb-bin-load.lsp"
pad "Python → C       (sexp)"     ; time_n $N "$PY /tmp/pb-sexp-save.lsp && $C /tmp/pb-sexp-load.lsp"
pad "Python → Asm     (sexp)"     ; time_n $N "$PY /tmp/pb-sexp-save.lsp && $ASM < /tmp/pb-sexp-load.lsp"
pad "C      → Python  (sexp)"     ; time_n $N "$C /tmp/pb-sexp-save.lsp && $PY /tmp/pb-sexp-load.lsp"
pad "C      → Asm     (sexp)"     ; time_n $N "$C /tmp/pb-sexp-save.lsp && $ASM < /tmp/pb-sexp-load.lsp"
pad "Asm    → Python  (sexp)"     ; time_n $N "$ASM < /tmp/pb-sexp-save.lsp && $PY /tmp/pb-sexp-load.lsp"
pad "Asm    → C       (sexp)"     ; time_n $N "$ASM < /tmp/pb-sexp-save.lsp && $C /tmp/pb-sexp-load.lsp"
echo

echo "PART 4 — mismatch cases (classify output, errors are expected)"

# Helper: run, capture output, classify via keyword
run_out() { eval "$1" 2>&1 | head -4 | tr '\n' ' ' | cut -c1-180; }

probe() {
    local label="$1" cmd="$2"
    pad "$label"
    local out
    out=$(run_out "$cmd")
    if [ -z "$out" ]; then
        echo "(no output / silent)"
    else
        echo "$out"
    fi
}

# Pre-materialize canonical files for the probes
$ASM < /tmp/pb-bin-save.lsp >/dev/null 2>&1
$PY /tmp/pb-sexp-save.lsp >/dev/null 2>&1

# 4a. Load a binary portal as if it were sexp → reader errors
cat > /tmp/pb-load-binary-as-sexp.lsp <<'EOF'
(load "/tmp/pb-state.binary") (display "reached end") (newline)
EOF
probe "Python loads asm BINARY as sexp" "$PY /tmp/pb-load-binary-as-sexp.lsp"
probe "C      loads asm BINARY as sexp" "$C /tmp/pb-load-binary-as-sexp.lsp"
probe "Asm    loads asm BINARY as sexp" "$ASM < /tmp/pb-load-binary-as-sexp.lsp"

# 4b. Portal-resume on a sexp file (wrong magic) → #f
cat > /tmp/pb-resume-sexp.lsp <<'EOF'
(display (portal-resume "/tmp/pb-state.sexp")) (newline)
EOF
probe "Asm portal-resume on SEXP file" "$ASM < /tmp/pb-resume-sexp.lsp"

# 4c. Load an empty file
: > /tmp/pb-empty.lsp
cat > /tmp/pb-empty-test.lsp <<'EOF'
(load "/tmp/pb-empty.lsp") (display "continued") (newline)
EOF
probe "Python load EMPTY file"  "$PY /tmp/pb-empty-test.lsp"
probe "C      load EMPTY file"  "$C /tmp/pb-empty-test.lsp"
probe "Asm    load EMPTY file"  "$ASM < /tmp/pb-empty-test.lsp"

# 4d. Load a truncated sexp (unclosed paren, no value)
echo -n "(define truncated" > /tmp/pb-trunc.sexp
cat > /tmp/pb-trunc-test.lsp <<'EOF'
(load "/tmp/pb-trunc.sexp") (display "continued") (newline)
EOF
probe "Python load TRUNCATED sexp" "$PY /tmp/pb-trunc-test.lsp"
probe "C      load TRUNCATED sexp" "$C /tmp/pb-trunc-test.lsp"
probe "Asm    load TRUNCATED sexp" "$ASM < /tmp/pb-trunc-test.lsp"

# 4e. Load a missing file
rm -f /tmp/pb-nope.xyz
cat > /tmp/pb-missing-test.lsp <<'EOF'
(display (load "/tmp/pb-nope.xyz")) (newline)
(display "continued") (newline)
EOF
probe "Python load MISSING file"  "$PY /tmp/pb-missing-test.lsp"
probe "C      load MISSING file"  "$C /tmp/pb-missing-test.lsp"
probe "Asm    load MISSING file"  "$ASM < /tmp/pb-missing-test.lsp"

# 4f. Asm portal-resume on truncated binary (corrupted heap dump)
head -c 40 /tmp/pb-state.binary > /tmp/pb-corrupt.binary
cat > /tmp/pb-corrupt-test.lsp <<'EOF'
(display (portal-resume "/tmp/pb-corrupt.binary")) (newline)
EOF
probe "Asm portal-resume on CORRUPT binary" "$ASM < /tmp/pb-corrupt-test.lsp"

echo
echo "═══════════════════════════════════════════════════════════════"
echo "Done."

rm -f /tmp/pb-*.lsp /tmp/pb-*.sexp /tmp/pb-*.binary /tmp/pb-*.json \
      /tmp/pb-state.* /tmp/pb-corrupt.binary /tmp/pb-nope.xyz 2>/dev/null
