#!/bin/sh
# The speed test: suit-run, the program a timer starts every minute, may cost at
# most 1 ms and 1 MiB more than an empty program built the same way (build/empty,
# next to the binary). The hook dispatcher on an event with no slot
# (`suit-run hook NOSUCHEVENT`) has the same 1 ms limit. The bench runs once on a quiet machine and again under
# CPU load from loadgen, whose workers carry their own deadline.
#
# Each of the two measurements is taken three times, all three are printed, and
# it passes if the best of the three is within budget. A machine kept busy by
# other work (a load average above about 100) adds noise to a 1 ms difference
# that the test cannot tell from suit's own cost; the budget is not loosened.
# SUIT names the binary (default build/suit); suit-run is beside it.

SUIT=${SUIT:-build/suit}
case $SUIT in /*) ;; *) SUIT=$PWD/$SUIT ;; esac
LOADGEN=${LOADGEN:-$HOME/.claude/bin/loadgen}
RUNS=3
ROUNDS=600
LOAD_WORKERS=8
# Three loaded runs of about 20 seconds each, with room to spare.
LOAD_SECONDS=100

mkdir -p build/tmp
T=$(mktemp -d "$PWD/build/tmp/budget.XXXXXX") || exit 1
trap 'rm -rf "$T"' EXIT INT TERM

# The stamp `suit apply` writes over two wardrobes of a real size: 300 skills of
# 5 files each (one with a subfolder), 40 agents, 20 commands, a joined
# instructions file for each wardrobe's harness, slots and files, all placed by
# a real apply into a scratch HOME on a machine named bench.
export HOME="$T/hm" SUIT_HOME="$T/home"
mkdir -p "$HOME" "$SUIT_HOME/store"
# wardrobe W HARNESS: 150 skills, 20 agents, 10 commands, two instruction sources, a slot and a file
wardrobe() {
    w=$SUIT_HOME/store/$1
    mkdir -p "$w/skills" "$w/agents" "$w/commands" "$w/instructions" "$w/files"
    i=0
    while [ $i -lt 150 ]; do
        s=$w/skills/$1-skill-$i
        mkdir "$s"
        for f in SKILL.md one.md two.md three.md four.md; do echo "$1 $i $f" > "$s/$f"; done
        i=$((i + 1))
    done
    mkdir "$w/skills/$1-skill-0/sub"
    mv "$w/skills/$1-skill-0/four.md" "$w/skills/$1-skill-0/sub/four.md"
    i=0; while [ $i -lt 20 ]; do echo "agent $i" > "$w/agents/$1-agent-$i.md"; i=$((i + 1)); done
    i=0; while [ $i -lt 10 ]; do echo "command $i" > "$w/commands/$1-command-$i.md"; i=$((i + 1)); done
    echo "$1 base" > "$w/instructions/base.md"
    echo "$1 bench" > "$w/instructions/bench.md"
    printf '#!/bin/sh\n' > "$w/files/slot.sh"; chmod +x "$w/files/slot.sh"
    echo '{}' > "$w/files/extra.json"
    cat > "$w/suit.conf" <<CONF
[harness $2]
skills.user       = ~/.$2/skills
agents.user       = ~/.$2/agents
commands.user     = ~/.$2/commands
instructions.user = ~/.$2/INSTRUCTIONS.md

[outfit $1-all]
only         = $2
skills       = *
agents       = *
commands     = *
instructions = instructions/base.md
slot $1/status = files/slot.sh
file ~/.$2/extra.json = files/extra.json

[outfit $1-bench]
only         = $2
instructions = instructions/bench.md
CONF
}
wardrobe mine claude
wardrobe public pi
printf '\n[wear user]\noutfits = mine-all public-all\n' >> "$SUIT_HOME/store/mine/suit.conf"
printf '\n[wear user machine=bench]\noutfits = mine-bench public-bench\n' >> "$SUIT_HOME/store/public/suit.conf"
"$SUIT" machine bench || { echo "budget: cannot name the scratch machine"; exit 1; }
"$SUIT" apply >/dev/null || { echo "budget: apply of the two scratch wardrobes failed"; exit 1; }
[ -f "$SUIT_HOME/joined/claude" ] && [ -f "$SUIT_HOME/joined/pi" ] || { echo "budget: the instructions were not joined"; exit 1; }
# The stamp lists 6 paths of SUIT_HOME, 5 for each wardrobe (its folder, its
# suit.conf and its skills, agents and commands folders) and each path the
# configs name: 24 here, whatever the 1500 files in the skills hold. The old
# stamp listed every file and folder of the stores, about 1900 here.
WATCH_LIMIT=64
watched=$(grep -vc '^suit stamp ' "$SUIT_HOME/stamp")
echo "budget: the stamp watches $watched paths (limit $WATCH_LIMIT)"
"$SUIT" compare || { echo "budget: scratch stamp does not read back as unchanged"; exit 1; }

# best_of LABEL: run the bench RUNS times, print every run, succeed if one passed.
best_of() {
    ok=1
    n=1
    while [ $n -le $RUNS ]; do
        echo "-- $1, run $n of $RUNS"
        if "$SUIT" bench -n "$ROUNDS"; then ok=0; fi
        n=$((n + 1))
    done
    return $ok
}

failed=0
[ "$watched" -lt "$WATCH_LIMIT" ] || { echo "budget: the stamp watches $watched paths, not fewer than $WATCH_LIMIT"; failed=1; }

echo "== quiet (load average: $(uptime | sed 's/.*load averages*: //'))"
best_of quiet || { echo "budget: no quiet run was within budget"; failed=1; }

[ -x "$LOADGEN" ] || { echo "budget: $LOADGEN not found"; exit 1; }
echo "== under load: loadgen $LOAD_SECONDS $LOAD_WORKERS"
"$LOADGEN" "$LOAD_SECONDS" "$LOAD_WORKERS" &
load=$!
sleep 2
echo "load average: $(uptime | sed 's/.*load averages*: //')"
best_of loaded || { echo "budget: no loaded run was within budget"; failed=1; }
wait "$load"

if [ "$failed" -eq 0 ]; then echo "budget: pass"; else echo "budget: FAIL"; fi
[ "$failed" -eq 0 ]
