opencode-bench/scripts/monitor.sh
KaKi87 7856b8e6c3 Add a tmux monitor script that keeps a run going and logs its progress
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-25 11:00:45 +01:00

51 lines
2.3 KiB
Bash
Executable file

#!/usr/bin/env bash
# Keeps a benchmark run going inside a tmux session and logs its progress.
#
# scripts/monitor.sh <run-id> [concurrency] [interval-seconds]
#
# Every interval it logs the run's progress, the running containers and the free disk space.
# If the run's tmux session has died while tasks are still pending, it starts the session again.
# A cancelled or finished run is left alone. Run this script itself in tmux:
# tmux new -d -s bench-monitor 'scripts/monitor.sh 2 8'
set -u
RUN="${1:?usage: monitor.sh <run-id> [concurrency] [interval-seconds]}"
CONC="${2:-8}"
EVERY="${3:-60}"
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
SESSION="bench-run-$RUN"
LOG="$ROOT/data/monitor-$RUN.log"
MIN_FREE_GB=3
# A run that ended with tasks left (a usage limit, for example) is not restarted more often than this.
RESTART_GAP=900
last_start=0
cd "$ROOT" || exit 1
log() { printf '%s %s\n' "$(date '+%F %T')" "$*" | tee -a "$LOG"; }
count() { sqlite3 -cmd ".timeout 5000" data/bench.sqlite "$1" 2>/dev/null; }
start() {
last_start=$(date +%s)
tmux new-session -d -s "$SESSION" "cd '$ROOT' && bun src/cli.ts resume $RUN --concurrency $CONC 2>&1 | tee -a data/run-$RUN.log"
log "started tmux session $SESSION"
}
log "monitoring run $RUN every ${EVERY}s (log: $LOG)"
while true; do
status=$(count "SELECT status FROM runs WHERE id=$RUN")
total=$(count "SELECT COUNT(*) FROM results WHERE run_id=$RUN")
done_=$(count "SELECT COUNT(*) FROM results WHERE run_id=$RUN AND status IN ('done','error','timeout')")
pending=$(count "SELECT COUNT(*) FROM results WHERE run_id=$RUN AND status IN ('pending','running')")
containers=$(docker ps -q --filter "name=ocb-$RUN-" | wc -l)
free=$(df --output=avail -BG / | tail -1 | tr -dc '0-9')
alive=no; tmux has-session -t "$SESSION" 2>/dev/null && alive=yes
log "run=$RUN status=$status $done_/$total pending=$pending containers=$containers session=$alive free=${free}G"
[ "$free" -lt "$MIN_FREE_GB" ] && log "WARNING: only ${free}G free on /"
if [ "$status" = cancelled ] || [ "${pending:-0}" -eq 0 ]; then
log "run $RUN is $status with $pending tasks left, nothing to do"
[ "$alive" = no ] && break
elif [ "$alive" = no ] && [ $(( $(date +%s) - last_start )) -ge "$RESTART_GAP" ]; then
log "session $SESSION is gone with $pending tasks left, restarting"
start
fi
sleep "$EVERY"
done