Skip to content
suit Artifact [a86bb57527]
Login

Artifact [a86bb57527]

Artifact a86bb57527bb25a96985f79740dc181477a5bd502ec0a85e92708980a625a116:


#!/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 real-sized wardrobes: the fixture's store
# plus 50 more files in it, and a second wardrobe with 50 files, all watched, with the links placed in a scratch HOME.
export HOME="$T/hm" SUIT_HOME="$T/home"
mkdir -p "$HOME" "$SUIT_HOME/store"
cp -R test/fixtures/wear "$SUIT_HOME/store/mine"
mkdir -p "$SUIT_HOME/store/mine/bulk"
i=0
while [ $i -lt 50 ]; do
    echo "file $i" > "$SUIT_HOME/store/mine/bulk/f$i"
    i=$((i + 1))
done
# a second wardrobe, as a user of the public and private pair has: 50 files in each, 100 in all
mkdir -p "$SUIT_HOME/store/public/bulk"
printf '[outfit extra]\n' > "$SUIT_HOME/store/public/suit.conf"
i=0
while [ $i -lt 50 ]; do
    echo "file $i" > "$SUIT_HOME/store/public/bulk/f$i"
    i=$((i + 1))
done
"$SUIT" apply >/dev/null || { echo "budget: apply of the two scratch wardrobes failed"; exit 1; }
echo "budget: the stamp watches $(wc -l < "$SUIT_HOME/stamp" | tr -d ' ') paths"
"$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

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 ]