#!/usr/bin/env bash
# Runs the server benchmark's streaming scenario once and keeps the server trace.
set -u
wt="${1:?path to a t3code checkout}"
out="$PWD/run-$(date +%Y%m%d-%H%M%S)"
mkdir -p "$out/tmp"
(
  while true; do
    f=$(find "$out/tmp" -name server.trace.ndjson -path '*streaming-*' 2>/dev/null | head -1)
    if [ -n "$f" ]; then cp "$f" "$out/server.trace.ndjson.partial" 2>/dev/null && mv "$out/server.trace.ndjson.partial" "$out/server.trace.ndjson"; fi
    sleep 1
  done
) &
copier=$!
cd "$wt"
TMPDIR="$out/tmp" node scripts/metrics/server/server-metrics.ts run --only streaming --checkout . --out "$out/streaming.json" > "$out/stdout.log" 2> "$out/stderr.log"
echo "exit=$?" >> "$out/stdout.log"
kill "$copier"
echo "$out"
