joeylib2/scripts/bench-dos.sh

85 lines
2.8 KiB
Bash
Executable file

#!/usr/bin/env bash
# Part of JoeyLib, Copyright (c) 2026 Scott Duensing, under the MIT License.
# See LICENSE.md for license information.
# SPDX-License-Identifier: MIT
#
# Headless UBER perf capture for DOS (DOSBox, 386SX-16 timing).
#
# Mounts build/dos/bin as C: in a headless DOSBox (Xvfb), runs UBER.EXE
# (which writes JOEYLOG.TXT to its CWD == C: == the host bin dir), polls
# that log for UBER's "press any key to exit" completion marker, then
# kills DOSBox. build/tools/uberPerfTable reads build/dos/bin/JOEYLOG.TXT.
#
# Uses the same dosbox-386sx16.conf as joey-run so the cycle budget
# matches the claudeDocs/PERF.md reference machine.
#
# scripts/bench-dos.sh # default 90s timeout
# TIMEOUT=120 scripts/bench-dos.sh # override
set -euo pipefail
repo=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
source "$repo/scripts/lib/joeyEnv.sh"
joeyNeed DOSBOX dosbox
bin_dir=$repo/build/dos/bin
conf=$repo/scripts/dosbox-386sx16.conf
log=$bin_dir/JOEYLOG.TXT
timeout_s=${TIMEOUT:-90}
if [[ ! -f $bin_dir/UBER.EXE ]]; then
echo "UBER.EXE not built. Run 'make dos' first." >&2
exit 1
fi
rm -f "$log" "$bin_dir/joeylog.txt"
work=$(mktemp -d -t joeylib-bench-dos.XXXXXX)
display=:$((90 + RANDOM % 9))
Xvfb "$display" -screen 0 800x600x24 -nolisten tcp >/dev/null 2>&1 &
xvfb_pid=$!
sleep 0.4
cleanup() {
joeyStop "$dosbox_pid"
kill "$xvfb_pid" 2>/dev/null || true
wait 2>/dev/null || true
rm -rf "$work"
}
trap cleanup EXIT
echo "bench-dos: running UBER headless (timeout ${timeout_s}s)..." >&2
# Mount with an explicit -c, NOT the positional path: DOSBox runs every
# -c line BEFORE it auto-mounts the trailing directory, so "C:" and
# "UBER.EXE" ran against a drive that did not exist yet ("Drive C does
# not exist!" / "Illegal command") and the run sat at Z:\> until the
# timeout with no JOEYLOG.TXT.
XDG_CONFIG_HOME="$work/xdg" DISPLAY=$display "$DOSBOX" "${JOEY_DOSBOX_ISOLATE[@]}" \
--conf "$conf" \
-c "MOUNT C \"$bin_dir\"" \
-c "C:" \
-c "UBER.EXE" \
>"$work/dosbox.log" 2>&1 &
dosbox_pid=$!
waited=0
while (( waited < timeout_s )); do
if [[ -f $log ]] && grep -q "press any key to exit" "$log" 2>/dev/null; then
break
fi
sleep 1
(( waited++ )) || true
done
if [[ ! -f $log ]]; then
echo "bench-dos: FAIL -- no JOEYLOG.TXT produced" >&2
exit 1
fi
# uberPerfTable reads build/dos/bin/JOEYLOG.TXT directly (uppercase).
ops=$(grep -c "ops/sec" "$log" || true)
done_marker=$(grep -c "press any key to exit" "$log" || true)
if (( done_marker == 0 )); then
echo "bench-dos: WARNING -- timed out before completion marker (partial capture)" >&2
fi
echo "bench-dos: captured $ops op lines (complete=$done_marker) in ${waited}s" >&2
echo " -> $log" >&2
grep "UBER:" "$log" | sed 's/ | hash=.*//' || true