From 9d26bbf2a1a6106a0944b2bb91e7912a27270984 Mon Sep 17 00:00:00 2001 From: Alexander Holbreich Date: Sat, 28 Nov 2026 00:17:17 +0100 Subject: [PATCH] Share timing code between the developer benchmarks --- bin/omarchy-benchmark-functions | 34 +++++++++ bin/omarchy-dev-benchmark-cli | 66 ++++++----------- bin/omarchy-dev-benchmark-theme-switcher | 43 ++++-------- test/shell.d/developer-benchmarks-test.sh | 86 +++++++++++++++++++++++ 4 files changed, 155 insertions(+), 74 deletions(-) create mode 100755 bin/omarchy-benchmark-functions create mode 100755 test/shell.d/developer-benchmarks-test.sh diff --git a/bin/omarchy-benchmark-functions b/bin/omarchy-benchmark-functions new file mode 100755 index 000000000000..896fddef8725 --- /dev/null +++ b/bin/omarchy-benchmark-functions @@ -0,0 +1,34 @@ +#!/bin/bash + +# omarchy:summary=Shared reporting helpers for developer benchmarks (source this, don't run it). +# omarchy:hidden=true + +benchmark_format_ms() { + local us="$1" + printf '%d.%03d' "$(( us / 1000 ))" "$(( us % 1000 ))" +} + +# The measurement function writes elapsed microseconds to its first argument +# (a variable name). It receives the label and workload as remaining arguments. +benchmark_run_case() { + local label="$1" repeat="$2" measure="$3" + shift 3 + local total_us=0 min_us=0 max_us=0 elapsed_us=0 i + + for (( i = 1; i <= repeat; i++ )); do + "$measure" elapsed_us "$label" "$@" + total_us=$(( total_us + elapsed_us )) + if (( i == 1 || elapsed_us < min_us )); then + min_us=$elapsed_us + fi + if (( elapsed_us > max_us )); then + max_us=$elapsed_us + fi + done + + printf '%-34s avg %8s ms min %8s ms max %8s ms\n' \ + "$label" \ + "$(benchmark_format_ms "$(( total_us / repeat ))")" \ + "$(benchmark_format_ms "$min_us")" \ + "$(benchmark_format_ms "$max_us")" +} diff --git a/bin/omarchy-dev-benchmark-cli b/bin/omarchy-dev-benchmark-cli index 440e71467ffa..d03cbc11c5a3 100755 --- a/bin/omarchy-dev-benchmark-cli +++ b/bin/omarchy-dev-benchmark-cli @@ -10,6 +10,8 @@ OMARCHY_BIN_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd) CLI="$OMARCHY_BIN_DIR/omarchy" REPEAT=5 +source "$OMARCHY_BIN_DIR/omarchy-benchmark-functions" + show_help() { cat <<'EOF' Usage: @@ -22,54 +24,32 @@ Options: EOF } -format_ms() { - local us="$1" - printf '%d.%03d' "$(( us / 1000 ))" "$(( us % 1000 ))" +measure_run() { + local -n elapsed="$1" + local label="$2" start_us end_us status + shift 2 + + # EPOCHREALTIME uses the locale's decimal separator, a comma in many. + start_us=${EPOCHREALTIME//[!0-9]/} + if "$@" >/dev/null; then + status=0 + else + status=$? + fi + end_us=${EPOCHREALTIME//[!0-9]/} + + if (( status != 0 )); then + printf '%-34s failed (exit %d)\n' "$label" "$status" + return "$status" + fi + + elapsed=$(( end_us - start_us )) } run_case() { local label="$1" shift - local total_us=0 - local min_us=0 - local max_us=0 - local elapsed_us=0 - local start_us=0 - local end_us=0 - local status=0 - - for (( i = 1; i <= REPEAT; i++ )); do - # EPOCHREALTIME uses the locale's decimal separator, a comma in many. - start_us=${EPOCHREALTIME//[!0-9]/} - if "$@" >/dev/null; then - status=0 - else - status=$? - fi - end_us=${EPOCHREALTIME//[!0-9]/} - - if (( status != 0 )); then - printf '%-34s failed (exit %d)\n' "$label" "$status" - return "$status" - fi - - elapsed_us=$(( end_us - start_us )) - total_us=$(( total_us + elapsed_us )) - - if (( i == 1 || elapsed_us < min_us )); then - min_us=$elapsed_us - fi - - if (( elapsed_us > max_us )); then - max_us=$elapsed_us - fi - done - - printf '%-34s avg %8s ms min %8s ms max %8s ms\n' \ - "$label" \ - "$(format_ms "$(( total_us / REPEAT ))")" \ - "$(format_ms "$min_us")" \ - "$(format_ms "$max_us")" + benchmark_run_case "$label" "$REPEAT" measure_run "$@" } while (( $# > 0 )); do diff --git a/bin/omarchy-dev-benchmark-theme-switcher b/bin/omarchy-dev-benchmark-theme-switcher index 24cb33a6bb17..bbfddcf7248b 100755 --- a/bin/omarchy-dev-benchmark-theme-switcher +++ b/bin/omarchy-dev-benchmark-theme-switcher @@ -12,6 +12,8 @@ OMARCHY_ROOT=$(cd -- "$OMARCHY_BIN_DIR/.." && pwd) REPEAT=5 KEEP_CACHE=false +source "$OMARCHY_BIN_DIR/omarchy-benchmark-functions" + show_help() { cat <<'EOF' Usage: @@ -28,11 +30,6 @@ Options: EOF } -format_ms() { - local us="$1" - printf '%d.%03d' "$(( us / 1000 ))" "$(( us % 1000 ))" -} - measure_once() { local start_us end_us # EPOCHREALTIME uses the locale's decimal separator, a comma in many. @@ -42,32 +39,16 @@ measure_once() { printf '%s' "$(( end_us - start_us ))" } +measure_run() { + local -n elapsed="$1" + shift 2 + elapsed=$(measure_once "$@") +} + run_case() { local label="$1" shift - local total_us=0 - local min_us=0 - local max_us=0 - local elapsed_us=0 - - for (( i = 1; i <= REPEAT; i++ )); do - elapsed_us=$(measure_once "$@") - total_us=$(( total_us + elapsed_us )) - - if (( i == 1 || elapsed_us < min_us )); then - min_us=$elapsed_us - fi - - if (( elapsed_us > max_us )); then - max_us=$elapsed_us - fi - done - - printf '%-34s avg %8s ms min %8s ms max %8s ms\n' \ - "$label" \ - "$(format_ms "$(( total_us / REPEAT ))")" \ - "$(format_ms "$min_us")" \ - "$(format_ms "$max_us")" + benchmark_run_case "$label" "$REPEAT" measure_run "$@" } while (( $# > 0 )); do @@ -153,12 +134,12 @@ prepare_image_cache() { } printf 'Theme switcher benchmark (%d warm runs each)\n\n' "$REPEAT" -printf '%-34s %s ms\n' "theme index cold" "$(format_ms "$(measure_once build_theme_index)")" +printf '%-34s %s ms\n' "theme index cold" "$(benchmark_format_ms "$(measure_once build_theme_index)")" run_case "theme index warm" build_theme_index -printf '%-34s %s ms\n' "selector prep cold (lazy)" "$(format_ms "$(measure_once prepare_selector_lazy)")" +printf '%-34s %s ms\n' "selector prep cold (lazy)" "$(benchmark_format_ms "$(measure_once prepare_selector_lazy)")" run_case "selector prep warm (lazy)" prepare_selector_lazy "${thumbnail_env[@]}" "$OMARCHY_BIN_DIR/omarchy-theme-switcher" >/dev/null -printf '%-34s %s ms\n' "thumbnail cache cold" "$(format_ms "$(measure_once prepare_image_cache)")" +printf '%-34s %s ms\n' "thumbnail cache cold" "$(benchmark_format_ms "$(measure_once prepare_image_cache)")" run_case "thumbnail cache warm" prepare_image_cache printf '\nTheme previews: %d\n' "$(find -L "$preview_dir" -maxdepth 1 -type f 2>/dev/null | wc -l)" diff --git a/test/shell.d/developer-benchmarks-test.sh b/test/shell.d/developer-benchmarks-test.sh new file mode 100755 index 000000000000..ca2855965688 --- /dev/null +++ b/test/shell.d/developer-benchmarks-test.sh @@ -0,0 +1,86 @@ +#!/bin/bash + +set -euo pipefail + +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" +source "$ROOT/bin/omarchy-benchmark-functions" + +# Exercise the shared calculator with a sub-millisecond sample and a minimum +# which is not the first sample. +measure_sample() { + local -n duration="$1" + duration=${samples[sample_index++]} +} +samples=(1750 2500 250) +sample_index=0 +summary=$(benchmark_run_case "example" 3 measure_sample) +[[ $summary == "$(printf '%-34s avg %8s ms min %8s ms max %8s ms' example 1.500 0.250 2.500)" ]] || + fail "benchmark reports the mean, minimum and maximum" "$summary" +pass "benchmark reports the mean, minimum and maximum" + +tmp=$(mktemp -d) +trap 'rm -rf "$tmp"' EXIT +mkdir -p "$tmp/bin" "$tmp/home" +cp "$ROOT/bin/"{omarchy-dev-benchmark-cli,omarchy-dev-benchmark-theme-switcher,omarchy-benchmark-functions} "$tmp/bin/" +export HOME="$tmp/home" TMPDIR="$tmp" CALLS="$tmp/calls" + +cat >"$tmp/bin/stub" <<'STUB' +#!/bin/bash +printf '%s %s\n' "${0##*/}" "$*" >>"$CALLS" +if [[ ${0##*/} == omarchy && ${FAIL_CLI:-} == 1 ]]; then + exit 17 +fi +if [[ ${0##*/} == omarchy-theme-switcher ]]; then + mkdir -p "$XDG_CACHE_HOME/omarchy/theme-selector/previews" + touch "$XDG_CACHE_HOME/omarchy/theme-selector/previews/mock.png" +fi +STUB +chmod +x "$tmp/bin/stub" +for stub in omarchy omarchy-theme-switcher omarchy-menu-images; do + ln -s stub "$tmp/bin/$stub" +done + +cli="$tmp/bin/omarchy-dev-benchmark-cli" +theme="$tmp/bin/omarchy-dev-benchmark-theme-switcher" +out=$("$cli" --repeat=2) +[[ $out == 'Omarchy CLI benchmark (2 runs each)'* && $(grep -c ' avg ' <<<"$out") == 9 ]] || + fail "CLI benchmark prints one summary per workload" "$out" +[[ $(wc -l <"$CALLS") == 18 ]] || fail "CLI benchmark repeats each of its nine workloads twice" +pass "CLI benchmark repeats each workload and reports its summary" + +: >"$CALLS" +status=0 +out=$(FAIL_CLI=1 "$cli" --repeat=2) || status=$? +[[ $status == 17 && $out == *'failed (exit 17)' && $(wc -l <"$CALLS") == 1 ]] || + fail "CLI benchmark stops on the first command failure" "$out" +pass "CLI benchmark preserves the failed case and its exit status" + +: >"$CALLS" +out=$("$theme" --repeat=2) +[[ $out == 'Theme switcher benchmark (2 warm runs each)'* && $out == *'Theme previews: 1' && $(grep -c ' avg ' <<<"$out") == 3 ]] || + fail "theme benchmark reports the three warm workloads" "$out" +[[ $(grep -c ' cold' <<<"$out") == 3 && $(wc -l <"$CALLS") == 10 ]] || + fail "theme benchmark preserves cold runs, warm repetitions and preview setup" "$out" +[[ $(find "$tmp" -mindepth 1 -maxdepth 1 -type d | wc -l) == 2 ]] || + fail "theme benchmark removes its scratch directories by default" +pass "theme benchmark preserves workloads and cleans up caches by default" + +out=$("$theme" --repeat=1 --keep-cache) +benchmark_cache=$(awk -F ': ' '/^Benchmark cache: / {print $2}' <<<"$out") +thumbnail_cache=$(awk -F ': ' '/^Thumbnail cache: / {print $2}' <<<"$out") +[[ -d $benchmark_cache && -d $thumbnail_cache && $benchmark_cache != "$thumbnail_cache" ]] || + fail "theme benchmark keeps and reports both caches on request" "$out" +[[ $(find "$tmp" -mindepth 1 -maxdepth 1 -type d | wc -l) == 4 ]] || + fail "theme benchmark does not keep its command stub directory" +pass "theme benchmark keeps only the two requested caches" + +for command in "$cli" "$theme"; do + status=0 + out=$("$command" --repeat=0 2>&1) || status=$? + [[ $status == 2 && $out == '--repeat must be a positive integer' ]] || + fail "both benchmarks reject an invalid repeat count" "$out" + out=$("$command" --help) + [[ $out == Usage:* && $out == *'--repeat='* ]] || + fail "both benchmarks still show help" "$out" +done +pass "both benchmarks retain repeat validation and help"