#!/usr/bin/env bash
# tests/run-ipc-benchmark: IPC round-trip latency benchmark (#342, D-020)
#
# Runs server/tests/ipc_bench.rs via `cargo test --release --test ipc_bench`.
# Parses IPC_BENCH_RESULT:{json} from output and outputs the result JSON.
#
# Latency budget: p99 <= 5ms (D-020: "~1-5ms serialization latency per tick").
#
# NOTE (#342): Handshake step is stubbed in ipc_bench.rs pending #555 (server
# protocol handshake) and #556 (client handshake). Full clean timing requires
# a working handshake before the measurement loop starts.
#
# Exit: 0 = benchmark passed (p99 within threshold), non-zero = failure
# Stdout: {"p50_ms":N,"p95_ms":N,"p99_ms":N,"threshold_ms":5,"passed":true,"rounds":100}
set -euo pipefail

THRESHOLD_MS=5

while [[ $# -gt 0 ]]; do
    case "$1" in
        --filter) shift 2 ;;        # ignored — benchmark has no test filter
        --filter=*) shift ;;
        *) echo "Unknown argument: $1" >&2; exit 2 ;;
    esac
done

REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"

START_MS=$(date +%s%3N)
TMPOUT=$(mktemp)

set +e
cd "$REPO_ROOT/server" && \
    cargo test --release --test ipc_bench -- --ignored --nocapture 2>&1 | tee "$TMPOUT" >&2
EXIT_CODE=${PIPESTATUS[0]}
set -e

END_MS=$(date +%s%3N)
DURATION_MS=$((END_MS - START_MS))

# Extract IPC_BENCH_RESULT:{json} line from output
RESULT_LINE=$(grep "^IPC_BENCH_RESULT:" "$TMPOUT" | tail -1 || true)
rm -f "$TMPOUT"

if [[ -n "$RESULT_LINE" ]]; then
    # Strip the prefix and output the JSON
    echo "${RESULT_LINE#IPC_BENCH_RESULT:}"
else
    # No result line — test failed to produce output
    printf '{"p50_ms":0,"p95_ms":0,"p99_ms":0,"threshold_ms":%d,"passed":false,"error":"no benchmark output — server binary may not be built (run make build-server)"}\n' \
        "$THRESHOLD_MS"
    EXIT_CODE=1
fi

exit $EXIT_CODE
