Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions bin/omarchy-benchmark-functions
Original file line number Diff line number Diff line change
@@ -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")"
}
66 changes: 23 additions & 43 deletions bin/omarchy-dev-benchmark-cli
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
43 changes: 12 additions & 31 deletions bin/omarchy-dev-benchmark-theme-switcher
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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)"
86 changes: 86 additions & 0 deletions test/shell.d/developer-benchmarks-test.sh
Original file line number Diff line number Diff line change
@@ -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=<count>'* ]] ||
fail "both benchmarks still show help" "$out"
done
pass "both benchmarks retain repeat validation and help"
Loading