#!/usr/bin/env bash # A/B the benchmark: a base revision against the working tree, alternately, on one # core, best of ROUNDS each, under each code layout. # # Why: the machine is shared, and separate runs move by tens of percent with other # people's load. Two builds timed alternately on one core see the same load, and # interference only ever slows a sample, so best-of recovers the undisturbed rate. # The ratio is the result (the method of z80-rust-fast, m68000-rust-fast and # ym2612-rust-fast). Both sides run the working tree's md-bench, so only the board # differs. # # Usage: scripts/ab.sh [BASE (default HEAD)] # env: CPU (default 31), ROUNDS (default 5), LAYOUTS (default "default nf5"), # FRAMES (per workload, default 600), TOOLCHAIN (default stable), # BIN_A and BIN_B (two md-bench binaries to compare instead of building) set -euo pipefail ROOT=$(cd "$(dirname "$0")/.." && pwd) BASE=${1:-HEAD} CPU=${CPU:-31} ROUNDS=${ROUNDS:-5} LAYOUTS=${LAYOUTS:-"default nf5"} FRAMES=${FRAMES:-600} TOOLCHAIN=${TOOLCHAIN:-stable} # the system cargo is another version declare -A FLAGS=( [default]="" [nf5]="-C llvm-args=-align-all-nofallthru-blocks=5" ) BASE_TREE="$ROOT/target/ab-base" rev=$(git -C "$ROOT" rev-parse --short "$BASE") [ -z "${BIN_A:-}" ] || { rev=$BIN_A; LAYOUTS=given; } if [ -n "${BIN_A:-}" ]; then :; elif [ ! -d "$BASE_TREE" ]; then git -C "$ROOT" worktree add -q --detach "$BASE_TREE" "$rev" fi if [ -z "${BIN_A:-}" ]; then git -C "$BASE_TREE" checkout -q -f --detach "$rev" cp "$ROOT/src/bin/md-bench.rs" "$BASE_TREE/src/bin/" fi build() { # tree layout -> binary path local tree=$1 layout=$2 target target="$tree/target/ab-$layout" RUSTFLAGS="${FLAGS[$layout]}" CARGO_TARGET_DIR="$target" \ rustup run "$TOOLCHAIN" cargo build --release -q --manifest-path "$tree/Cargo.toml" --bin md-bench >&2 || return 1 echo "$target/release/md-bench" } WORKLOADS="sonic sor2 gunstar od2" echo "base $rev vs ${BIN_B:-working tree}, cpu $CPU, $ROUNDS rounds, $FRAMES frames, load $(cut -d' ' -f1 /proc/loadavg)" best_base_all=""; best_new_all=""; checks="" for layout in $LAYOUTS; do # A failed build must stop the run: timing a stale binary is worse than no number. if [ -n "${BIN_A:-}" ]; then a=$BIN_A; b=$BIN_B else a=$(build "$BASE_TREE" "$layout") || { echo "base build failed" >&2; exit 1; } b=$(build "$ROOT" "$layout") || { echo "working-tree build failed" >&2; exit 1; } fi # Alternate per workload, keeping each workload's best on each side: the two # sides of one comparison are seconds apart, under the same load. declare -A best=() for _ in $(seq "$ROUNDS"); do for w in $WORKLOADS; do for side in a b; do bin=$a; [ $side = b ] && bin=$b out=$(BENCH_BIN=$bin taskset -c "$CPU" "$ROOT/scripts/bench.sh" --frames "$FRAMES" --only "$w") t=$(sed -nE 's/^total ([0-9.]+) s$/\1/p' <<<"$out") checks+="$side $w $(grep -o 'check [0-9a-f]*' <<<"$out")"$'\n' key="$side $w" if [ -z "${best[$key]:-}" ] || awk "BEGIN{exit !($t < ${best[$key]})}"; then best[$key]=$t; fi done done done best_a=0; best_b=0 for w in $WORKLOADS; do best_a=$(awk "BEGIN{print $best_a + ${best[a $w]}}") best_b=$(awk "BEGIN{print $best_b + ${best[b $w]}}") echo " $w: base ${best[a $w]} s, new ${best[b $w]} s, x$(awk "BEGIN{printf \"%.3f\", ${best[a $w]}/${best[b $w]}}")" done unset best echo "$layout: base $best_a s, new $best_b s, x$(awk "BEGIN{printf \"%.3f\", $best_a/$best_b}")" [ -z "$best_base_all" ] || awk "BEGIN{exit !($best_a < $best_base_all)}" && best_base_all=$best_a [ -z "$best_new_all" ] || awk "BEGIN{exit !($best_b < $best_new_all)}" && best_new_all=$best_b done # Both sides must make the same sound and picture. if [ "$(cut -d' ' -f2- <<<"$checks" | sort -u | grep -c .)" != "$(wc -w <<<"$WORKLOADS")" ]; then echo "CHECKSUMS DIFFER:"; sort -u <<<"$checks"; exit 1 fi echo "min over layouts: base $best_base_all s, new $best_new_all s, x$(awk "BEGIN{printf \"%.3f\", $best_base_all/$best_new_all}")"