#!/usr/bin/env bash # Benchmark results over time (issue gabla108/louise#101), for a dedicated, quiet machine # (.toboggan/workflows/bench.yml). Runs scripts/bench.sh and # scripts/bench-compile.sh, counts each benchmark's user-space # instructions with `perf stat` on one scheduler thread (the same to a few # parts in 10^8 from run to run, where wall time varies by percents; # skipped without perf), and writes the results as one CSV per commit: # # commit,benchmark,metric,value # metric: ms, compile_ms, instructions # # It then compares them with the previous run's and lists the # regressions beyond a threshold, in REPORT (and on stdout). Regressions # do not fail the script: timings are for people to judge. A benchmark # whose output is wrong does (bench.sh checks it). # # scripts/bench-track.sh # # Environment: LOUISE_BENCH_DIR, where results are kept (default # ~/louise-bench): .csv, .report, and `index`, the commits # in the order they ran. BENCH_THRESHOLD_MS (default 10) and # BENCH_THRESHOLD_INSTRUCTIONS (default 1), in percent. BENCH_RUNS, as # for bench.sh (instructions are counted once). set -euo pipefail cd "$(dirname "$0")/.." dir="${LOUISE_BENCH_DIR:-$HOME/louise-bench}" commit="$(git rev-parse HEAD)" mkdir -p "$dir" work="$(mktemp -d)" trap 'rm -rf "$work"' EXIT # The toolchain as released (issue gabla108/louise#120). Results before this commit came # from the `release` profile: the series steps there. export LOUISE_PROFILE=dist BENCH_BUILD_DIR="$work/programs" scripts/bench.sh --save "$work/run.txt" scripts/bench-compile.sh --save "$work/compile.txt" csv="$work/results.csv" echo "commit,benchmark,metric,value" > "$csv" while read -r name ms; do echo "$commit,$name,ms,$ms"; done < "$work/run.txt" >> "$csv" while read -r name ms; do echo "$commit,$name,compile_ms,$ms"; done < "$work/compile.txt" >> "$csv" # User-space instructions a program runs, or nothing if perf cannot count # them (not installed, no counters, or perf_event_paranoid above 2). # - One scheduler thread: on 22, threads that steal or spin vary the count # by 0.3-1.5%; on one, it is the same to a few parts in 10^8. # - On a hybrid CPU perf reports one row per core type # (`cpu_core/instructions/u`, `cpu_atom/instructions/u`), each counting # while the program runs on that type: their sum is the total. # `--no-scale`: by default perf scales each row up as if its counter had # been multiplexed, which counted up to 30% too many, varying with the # time spent on each core type. instructions() { command -v perf >/dev/null || return 0 LOUISE_THREADS=1 perf stat --no-scale -x, -e instructions:u -o "$work/perf.txt" "$@" >/dev/null || return 1 awk -F, '$3 ~ /instructions/ && $1 ~ /^[0-9]+$/ { n += $1; seen = 1 } END { if (seen) print n }' "$work/perf.txt" } if [ -n "$(instructions true)" ]; then for program in tests/bench/*.lse; do name="$(basename "$program" .lse)" echo "$commit,$name,instructions,$(instructions "$work/programs/$name")" >> "$csv" done else echo "bench-track: perf cannot count instructions here, so they are not recorded" fi cp "$csv" "$dir/$commit.csv" # The previous run: the last commit in the index other than this one. previous="" if [ -f "$dir/index" ]; then previous="$(grep -vx "$commit" "$dir/index" | tail -1 || true)" fi report="$dir/$commit.report" if [ -z "$previous" ] || [ ! -f "$dir/$previous.csv" ]; then echo "bench-track: no previous results to compare with" | tee "$report" else awk -F, -v ms="${BENCH_THRESHOLD_MS:-10}" -v ins="${BENCH_THRESHOLD_INSTRUCTIONS:-1}" \ -v before="${previous:0:10}" -v after="${commit:0:10}" ' FNR == 1 { next } NR == FNR { old[$2 "," $3] = $4; next } ($2 "," $3) in old { o = old[$2 "," $3] if (o <= 0) next change = ($4 - o) * 100 / o limit = ($3 == "instructions") ? ins : ms if (change > limit) { rows = rows sprintf("| `%s` | %s | %s | %s | +%.1f%% |\n", $2, $3, o, $4, change) } } END { if (rows == "") { printf "No regressions from %s to %s.\n", before, after; exit } printf "Benchmark regressions from %s to %s (thresholds: %s%% for times, %s%% for instructions):\n\n", before, after, ms, ins print "| Benchmark | Metric | Before | After | Change |" print "|---|---|---|---|---|" printf "%s", rows }' "$dir/$previous.csv" "$csv" | tee "$report" fi grep -qx "$commit" "$dir/index" 2>/dev/null || echo "$commit" >> "$dir/index"