perf-gate.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #!/usr/bin/env bash
  2. # perf-gate.sh: the big-project performance gate (round perfgate190id, community slice B12; docs/RELEASE.md step 2b).
  3. #
  4. # Measures, headless, on heavy projects: the audio engine's CPU per block size (64 / 128 / 256, one thread; also the
  5. # instructions retired where the CPU counters can be read), the project's import / first build / rebuild (an edit's
  6. # new snapshot) and the peak memory (tools/perfbench); with the app: .lynx save and load times (--perfgen), the open
  7. # time and memory and the paint cost per frame of every main view (--paintbench, off screen), and optionally the full
  8. # start-up with the project (--opentime, a window: Xvfb). Then compares with a stored baseline (tests/perf/) and fails
  9. # on any regression past its threshold (scripts/perf_gate.py; cells that fail are measured once more first, and the
  10. # best run counts).
  11. #
  12. # Projects: the synthetic ones of core/PerfProjects.h (synth100, inserts50, stream, big200, automation; generated, the
  13. # same bytes every time) and, with FL=1, FL Studio's demo songs Thief and Exoplanet plus Live's Patience from the
  14. # local installs (never committed). CI runs the synthetic ones (.github/workflows/tests.yml, profile ci-ubuntu).
  15. #
  16. # usage: scripts/perf-gate.sh [compare|update|measure|ab] [results dir]
  17. # compare (default) measure, then compare with tests/perf/baseline-$PROFILE.json; exit 1 on a regression
  18. # update measure twice and write the baseline (after a reviewed, intended change: commit it with a reason)
  19. # measure measure only (results.json)
  20. # ab (rollup 0.1.31 step 5, round 190ok's proposal) the bench cells of main's perfbench (BASE_PERFBENCH=path)
  21. # and this one, measured alternately in this session, ROUNDS (>= 5, default 5) rounds (main, new, new,
  22. # main, ...), a calibration before and after each side's run; perf_gate.py ab compares median with
  23. # median (summary-ab.md): how a wall-clock failure of compare is confirmed or dismissed
  24. # env:
  25. # PROFILE=local baseline name (local = this machine; ci-ubuntu = GitHub's ubuntu-24.04 runners)
  26. # PERFBENCH=path tools/perfbench build (default: build-linux/perfbench, then build/perfbench)
  27. # LYNX_APP=path the app (default: build-linux/LYNX_artefacts/Release/LYNX, then build/...); APP=0: skip it
  28. # OPENTIME=1 also --opentime (opens a window: needs DISPLAY, xvfb-run, or XVFB=<Xvfb binary>)
  29. # FL=1 also FL's Thief / Exoplanet and Live's Patience (FL_ROOT, LIVE_DEMO)
  30. # BLOCKS="64 128 256" SCENARIOS="synth100 inserts50 stream big200 automation" SECONDS_AUDIO=4 REPS=3
  31. # CORES=7-11 pin the measurements (taskset); LOAD_MAX=6 / LOAD_WAIT=1800: wait for a quiet machine (local)
  32. set -u
  33. cd "$(dirname "$0")/.."
  34. ROOT=$PWD
  35. mode=${1:-compare}
  36. # (keep OUT on a local disk: the generated projects' audio is read from there, and a network or WSL-mounted Windows
  37. # drive turns the open times into file-system times)
  38. OUT=${2:-$ROOT/build-perfgate/$(date +%Y%m%d-%H%M%S)}
  39. PROFILE=${PROFILE:-local}
  40. BASELINE=${BASELINE:-$ROOT/tests/perf/baseline-$PROFILE.json}
  41. BLOCKS=${BLOCKS:-"64 128 256"}
  42. SCENARIOS=${SCENARIOS:-"synth100 inserts50 stream big200 automation"}
  43. SECONDS_AUDIO=${SECONDS_AUDIO:-4}
  44. REPS=${REPS:-3}
  45. LOAD_MAX=${LOAD_MAX:-}
  46. LOAD_WAIT=${LOAD_WAIT:-1800}
  47. FL_ROOT=${FL_ROOT:-"/mnt/c/Program Files/Image-Line/FL Studio 2026"}
  48. LIVE_DEMO=${LIVE_DEMO:-"/mnt/c/ProgramData/Ableton/Live 12 Trial/Resources/Core Library/Lessons/Demo Songs/Chuck Sutton - Patience (Live 12 Suite Demo).als"}
  49. first () { for f in "$@"; do [ -x "$f" ] && { echo "$f"; return; }; done; }
  50. PERFBENCH=${PERFBENCH:-$(first build-linux/perfbench build/perfbench)}
  51. [ -n "$PERFBENCH" ] && [ -x "$PERFBENCH" ] || { echo "perf-gate: no perfbench (build the 'perfbench' target, or set PERFBENCH)" >&2; exit 2; }
  52. if [ "${APP:-1}" != 0 ]; then LYNX_APP=${LYNX_APP:-$(first build-linux/LYNX_artefacts/Release/LYNX build/LYNX_artefacts/Release/LYNX)}; else LYNX_APP=; fi
  53. mkdir -p "$OUT"
  54. PIN=(); [ -n "${CORES:-}" ] && PIN=(taskset -c "$CORES")
  55. # FL demo songs (local only, never committed)
  56. declare -A FLP=([thief]="$FL_ROOT/Data/Demo projects/Demo songs/Ookay - Thief.flp" [exoplanet]="$FL_ROOT/Data/Demo projects/Demo songs/Eric Kauffmann & NrgMind - Exoplanet.flp")
  57. EXTRA=()
  58. if [ "${FL:-0}" = 1 ]; then
  59. for k in thief exoplanet; do [ -f "${FLP[$k]}" ] && EXTRA+=("$k") || echo "perf-gate: FL demo $k not found, skipped" >&2; done
  60. [ -f "$LIVE_DEMO" ] && EXTRA+=(patience) || echo "perf-gate: Live demo not found, skipped" >&2
  61. fi
  62. scenario_arg () { case $1 in thief|exoplanet) echo "flp:${FLP[$1]}";; patience) echo "als:$LIVE_DEMO";; *) echo "$1";; esac; }
  63. project_file () { case $1 in thief|exoplanet) echo "${FLP[$1]}";; patience) echo "$LIVE_DEMO";; *) echo "$OUT/projects/$1.lynx";; esac; }
  64. quiet () { # wait for a quiet machine (local runs: other jobs skew the numbers); logs the load
  65. [ -z "$LOAD_MAX" ] && return 0
  66. local t=0
  67. while awk "BEGIN{exit !($(cut -d' ' -f1 /proc/loadavg) >= $LOAD_MAX)}"; do
  68. [ $t -ge "$LOAD_WAIT" ] && { echo "perf-gate: load still $(cut -d' ' -f1 /proc/loadavg) after ${LOAD_WAIT}s, measuring anyway" >&2; return 0; }
  69. sleep 15; t=$((t + 15))
  70. done
  71. }
  72. # the app's commands: a private Xvfb when there is no display and XVFB names one; else xvfb-run
  73. XPID=
  74. APP_PROFILE=$OUT/profile
  75. app_run () { # report-producing app command (timeout 15 min), in the profile $APP_PROFILE
  76. local app_env=(env LYNX_TEST_MODE=1 LYNX_PROFILE_DIR="$APP_PROFILE" XDG_CONFIG_HOME="$OUT/xdg")
  77. if [ -n "${DISPLAY:-}" ]; then timeout 900 "${app_env[@]}" "${PIN[@]}" "$LYNX_APP" "$@"
  78. elif command -v xvfb-run > /dev/null; then timeout 900 "${app_env[@]}" xvfb-run -a "${PIN[@]}" "$LYNX_APP" "$@"
  79. else timeout 900 "${app_env[@]}" "${PIN[@]}" "$LYNX_APP" "$@"; fi
  80. }
  81. if [ -n "$LYNX_APP" ] && [ -z "${DISPLAY:-}" ] && [ -n "${XVFB:-}" ]; then
  82. d=$((90 + RANDOM % 9)); "$XVFB" ":$d" -screen 0 1920x1080x24 -nolisten tcp > "$OUT/xvfb.log" 2>&1 & XPID=$!
  83. export DISPLAY=":$d"; sleep 2
  84. fi
  85. cleanup () { [ -n "$XPID" ] && kill "$XPID" 2> /dev/null; [ -n "${GITHUB_STEP_SUMMARY:-}" ] && [ -f "$OUT/summary.md" ] && cat "$OUT/summary.md" >> "$GITHUB_STEP_SUMMARY"; return 0; }
  86. trap cleanup EXIT
  87. { echo "$(uname -sm), $(grep -m1 'model name' /proc/cpuinfo 2> /dev/null | cut -d: -f2 | sed 's/^ //'), $(nproc) threads, $(git rev-parse --short HEAD 2> /dev/null)"; } > "$OUT/machine.txt"
  88. calibrate () { quiet; "${PIN[@]}" "$PERFBENCH" calibrate > "$OUT/calib-$1.txt" 2>&1; } # (gated as the cells: the times are divided by it)
  89. bench () { # cell (scenario@block) run
  90. local s=${1%@*} b=${1#*@} extra=()
  91. quiet
  92. echo "load $(cut -d' ' -f1 /proc/loadavg)" > "$OUT/bench-$1-$2.txt"
  93. timeout 1200 "${PIN[@]}" "$PERFBENCH" "$(scenario_arg "$s")" --block "$b" --seconds "$SECONDS_AUDIO" --reps "$REPS" --gate "${extra[@]}" >> "$OUT/bench-$1-$2.txt" 2>&1 \
  94. || echo "perf-gate: perfbench $1 failed (exit $?)" | tee -a "$OUT/errors.txt" >&2
  95. }
  96. paint () { # project run
  97. quiet
  98. # (a fresh profile each time: a second open would find the first one's stream cache and measure no decoding)
  99. APP_PROFILE=$OUT/profile-paint-$1-$2 LYNX_PAINTBENCH_FRAMES=${PAINT_FRAMES:-20} app_run --paintbench "$(project_file "$1")" "$OUT/paint-$1-$2.txt" > "$OUT/paint-$1-$2.log" 2>&1
  100. grep -q '^result=OK' "$OUT/paint-$1-$2.txt" 2> /dev/null || echo "perf-gate: paintbench $1 failed" | tee -a "$OUT/errors.txt" >&2
  101. }
  102. opentime () { # project run (a window)
  103. quiet
  104. # (a fresh profile each time, as a first start: a device saved by the run before could be opened differently)
  105. APP_PROFILE=$OUT/profile-open-$1-$2 app_run --opentime "$(project_file "$1")" "$OUT/open-$1-$2.txt" 3 > "$OUT/open-$1-$2.log" 2>&1
  106. grep -q '^result=OK' "$OUT/open-$1-$2.txt" 2> /dev/null || echo "perf-gate: opentime $1 failed" | tee -a "$OUT/errors.txt" >&2
  107. }
  108. projects () { local p; for p in $SCENARIOS; do echo "$p"; done; for p in "${EXTRA[@]}"; do echo "$p"; done; }
  109. measure_all () { # run number
  110. local n=$1 s b p
  111. calibrate "$n-a"
  112. for s in $(projects); do for b in $BLOCKS; do echo "== bench $s@$b #$n"; bench "$s@$b" "$n"; done; done
  113. if [ -n "$LYNX_APP" ]; then
  114. [ -f "$OUT/perfgen.txt" ] || { echo "== perfgen"; app_run --perfgen "$OUT/projects" "$OUT/perfgen.txt" "$(echo $SCENARIOS | tr ' ' ,)" > "$OUT/perfgen.log" 2>&1; grep -q '^result=OK' "$OUT/perfgen.txt" 2> /dev/null || echo "perf-gate: perfgen failed" | tee -a "$OUT/errors.txt" >&2; }
  115. for p in $(projects); do echo "== paint $p #$n"; paint "$p" "$n"; done
  116. if [ "${OPENTIME:-0}" = 1 ]; then for p in $(projects); do echo "== opentime $p #$n"; opentime "$p" "$n"; done; fi
  117. fi
  118. calibrate "$n-b"
  119. }
  120. retry_cells () { # run number; the cells perf_gate.py listed
  121. local n=$1 c
  122. calibrate "$n-a"
  123. while read -r c; do
  124. case $c in
  125. bench:*) echo "== retry bench ${c#bench:}"; bench "${c#bench:}" "$n" ;;
  126. paint:*) echo "== retry paint ${c#paint:}"; paint "${c#paint:}" "$n" ;;
  127. open:*) echo "== retry opentime ${c#open:}"; opentime "${c#open:}" "$n" ;;
  128. esac
  129. done < "$OUT/retry.txt"
  130. calibrate "$n-b"
  131. }
  132. echo "perf-gate: $mode, profile $PROFILE, results in $OUT; perfbench $PERFBENCH; app ${LYNX_APP:-none}; extra: ${EXTRA[*]:-none}"
  133. case $mode in
  134. measure) measure_all 1; python3 scripts/perf_gate.py collect "$OUT" > /dev/null; echo "perf-gate: $OUT/results.json" ;;
  135. update) measure_all 1; measure_all 2; mkdir -p "$(dirname "$BASELINE")"; python3 scripts/perf_gate.py update "$OUT" "$BASELINE" ;;
  136. compare)
  137. if [ ! -f "$BASELINE" ]; then # (a new profile: nothing to compare with; the run becomes a candidate baseline)
  138. measure_all 1; python3 scripts/perf_gate.py update "$OUT" "$OUT/baseline-candidate.json" > /dev/null
  139. echo "::warning::perf-gate: no baseline $BASELINE: nothing compared; candidate in $OUT/baseline-candidate.json (commit it as the baseline after a review)"
  140. exit 0
  141. fi
  142. measure_all 1
  143. python3 scripts/perf_gate.py compare "$OUT" "$BASELINE" > /dev/null && { cat "$OUT/summary.md"; exit 0; }
  144. for n in $(seq 2 $((1 + ${RETRIES:-2}))); do # (failing cells again: a busy moment on a shared runner is not a regression)
  145. echo "perf-gate: measuring again: $(tr '\n' ' ' < "$OUT/retry.txt")"
  146. retry_cells "$n"
  147. python3 scripts/perf_gate.py compare "$OUT" "$BASELINE" > /dev/null && { cat "$OUT/summary.md"; exit 0; }
  148. done
  149. cat "$OUT/summary.md"; exit 1 ;;
  150. ab)
  151. [ -n "${BASE_PERFBENCH:-}" ] && [ -x "$BASE_PERFBENCH" ] || { echo "perf-gate: ab needs BASE_PERFBENCH (main's perfbench)" >&2; exit 2; }
  152. ROUNDS=${ROUNDS:-5}; [ "$ROUNDS" -lt 5 ] && ROUNDS=5
  153. TOP=$OUT; NEW_PERFBENCH=$PERFBENCH
  154. side () { # side (base|new) run number
  155. OUT=$TOP/$1; mkdir -p "$OUT"; cp "$TOP/machine.txt" "$OUT/machine.txt"
  156. if [ "$1" = base ]; then PERFBENCH=$BASE_PERFBENCH; else PERFBENCH=$NEW_PERFBENCH; fi
  157. calibrate "$2-a"
  158. for s in $(projects); do for b in $BLOCKS; do echo "== ab $1 bench $s@$b #$2"; bench "$s@$b" "$2"; done; done
  159. calibrate "$2-b"
  160. OUT=$TOP; PERFBENCH=$NEW_PERFBENCH
  161. }
  162. for n in $(seq 1 "$ROUNDS"); do
  163. if [ $((n % 2)) = 1 ]; then side base "$n"; side new "$n"; else side new "$n"; side base "$n"; fi
  164. done
  165. python3 scripts/perf_gate.py ab "$TOP" > /dev/null; rc=$?
  166. cat "$TOP/summary-ab.md"; exit $rc ;;
  167. *) echo "usage: $0 [compare|update|measure|ab] [results dir]" >&2; exit 2 ;;
  168. esac