/ de-omarchy / bin / omarchy-dev-benchmark-theme-switcher
swadhinbiswas Initial de-omarchy: Omarchy UI layer for Arch-based distros · 8/22/2026
9f393b2
161 lines · 4.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/bin/bash

# omarchy:summary=Measure theme switcher cache and selector prep times
# omarchy:args=[--repeat=<count>] [--keep-cache]
# omarchy:examples=omarchy dev benchmark theme switcher | omarchy dev benchmark theme-switcher --repeat=10
# omarchy:aliases=omarchy dev benchmark theme-switcher

set -euo pipefail

OMARCHY_BIN_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
REPEAT=5
KEEP_CACHE=false

show_help() {
  cat <<'EOF'
Usage:
  omarchy dev benchmark theme switcher [--repeat=<count>] [--keep-cache]

Measure the non-interactive parts of the theme switcher:
- theme preview index build (omarchy-theme-switcher before UI handoff)
- lazy selector row prep used by the interactive theme switcher
- full thumbnail cache warmup cost (omarchy-menu-images --cache-only)

Options:
  --repeat=<count>  Number of warm runs to measure for each case (default: 5)
  --keep-cache      Keep the temporary benchmark cache and print its path
EOF
}

now_us() {
  local now="${EPOCHREALTIME/./}"
  printf '%s' "$now"
}

format_ms() {
  local us="$1"
  printf '%d.%03d' "$(( us / 1000 ))" "$(( us % 1000 ))"
}

measure_once() {
  local start_us end_us
  start_us=$(now_us)
  "$@" >/dev/null
  end_us=$(now_us)
  printf '%s' "$(( 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

  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")"
}

while (( $# > 0 )); do
  case "$1" in
  --repeat=*)
    REPEAT="${1#*=}"
    ;;
  --keep-cache)
    KEEP_CACHE=true
    ;;
  --help | -h)
    show_help
    exit 0
    ;;
  *)
    echo "Unknown option: $1" >&2
    show_help >&2
    exit 2
    ;;
  esac
  shift
done

if [[ ! $REPEAT =~ ^[0-9]+$ ]] || (( REPEAT < 1 )); then
  echo "--repeat must be a positive integer" >&2
  exit 2
fi

benchmark_cache=$(mktemp -d)
thumbnail_cache=$(mktemp -d)
stub_bin=$(mktemp -d)

cleanup() {
  rm -rf "$stub_bin"

  if [[ $KEEP_CACHE == "true" ]]; then
    printf 'Benchmark cache: %s\n' "$benchmark_cache"
    printf 'Thumbnail cache: %s\n' "$thumbnail_cache"
  else
    rm -rf "$benchmark_cache" "$thumbnail_cache"
  fi
}
trap cleanup EXIT

cat >"$stub_bin/omarchy-menu-images" <<'EOF'
#!/bin/bash
exit 0
EOF
chmod +x "$stub_bin/omarchy-menu-images"

benchmark_env=(
  env
  "XDG_CACHE_HOME=$benchmark_cache"
  "PATH=$stub_bin:$OMARCHY_BIN_DIR:$PATH"
)

thumbnail_env=(
  env
  "XDG_CACHE_HOME=$thumbnail_cache"
  "PATH=$stub_bin:$OMARCHY_BIN_DIR:$PATH"
)

preview_dir="$benchmark_cache/omarchy/theme-selector/previews"
thumbnail_preview_dir="$thumbnail_cache/omarchy/theme-selector/previews"

build_theme_index() {
  "${benchmark_env[@]}" "$OMARCHY_BIN_DIR/omarchy-theme-switcher"
}

prepare_selector_lazy() {
  "${benchmark_env[@]}" "$OMARCHY_BIN_DIR/omarchy-menu-images" --prepare-only --lazy-thumbnails --show-labels --filterable "$preview_dir"
}

prepare_image_cache() {
  "${thumbnail_env[@]}" "$OMARCHY_BIN_DIR/omarchy-menu-images" --cache-only "$thumbnail_preview_dir"
}

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)")"
run_case "theme index warm" build_theme_index
printf '%-34s %s ms\n' "selector prep cold (lazy)" "$(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)")"
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)"