#!/usr/bin/env bash
# Reproducer for the bench/RESULTS.md numbers.
#
# Requirements:
#   - Docker daemon running (testcontainers boots Postgres images)
#   - Rust stable toolchain matching rust-toolchain.toml (1.94+)
#
# Usage:
#   ./bench/run.sh                  # writes bench/results-<timestamp>.log
#
# After running, copy the table from the log into bench/RESULTS.md and
# update the metadata + interpretation sections. The script keeps logs
# rather than overwriting RESULTS.md so historical runs are preserved.

set -euo pipefail

cd "$(dirname "$0")/.."

ts=$(date -u +%Y%m%dT%H%M%SZ)
log="bench/results-${ts}.log"

{
  echo "=== bench/throughput run @ ${ts} ==="
  echo
  echo "--- hardware ---"
  cat /proc/cpuinfo | grep "model name" | head -1 || true
  echo "cores: $(nproc)"
  free -h | head -2
  echo
  echo "--- os ---"
  uname -a
  echo
  echo "--- toolchain ---"
  rustc --version
  cargo --version
  echo
  echo "--- docker ---"
  docker version --format 'client {{.Client.Version}} / server {{.Server.Version}}' 2>/dev/null || docker --version
  echo
  echo "--- postgres image digest ---"
  docker image inspect postgres:16-alpine --format '{{.Id}}' 2>/dev/null \
    || docker pull postgres:16-alpine >/dev/null 2>&1 \
    && docker image inspect postgres:16-alpine --format '{{.Id}}'
  echo
  echo "=== running cargo bench --bench throughput --features worker ==="
  echo
  cargo bench --bench throughput --features worker -- 2>&1
} | tee "$log"

echo
echo "log saved to: $log"
echo "next: paste the table from the log into bench/RESULTS.md and commit."
