-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-benchmarks.sh
More file actions
420 lines (377 loc) · 13.9 KB
/
Copy pathrun-benchmarks.sh
File metadata and controls
420 lines (377 loc) · 13.9 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#!/bin/bash
# Benchmark runner script for the Image Color Palette Extractor
# This script runs comprehensive performance benchmarks
echo "🚀 Running Image Color Palette Extractor Benchmarks"
echo "====================================================="
# Function to check if command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Check if cargo is installed
if ! command_exists cargo; then
echo "❌ Cargo is not installed!"
echo "Please install Rust and Cargo from https://rustup.rs/"
exit 1
fi
# Ensure wasmtime is installed (via cargo) and available in PATH for this session
ensure_wasmtime() {
if command_exists wasmtime; then
echo "✅ wasmtime is already installed."
return 0
fi
echo "⚠️ wasmtime not found. Installing via 'cargo install wasmtime-cli'..."
cargo install wasmtime-cli
if [ $? -ne 0 ]; then
echo "❌ Failed to install wasmtime via cargo."
return 1
fi
# add cargo bin to PATH for this execution
if [ -n "$CARGO_HOME" ]; then
cargo_bin="$CARGO_HOME/bin"
else
cargo_bin="$HOME/.cargo/bin"
fi
if [ -d "$cargo_bin" ]; then
PATH="$cargo_bin:$PATH"
export PATH
fi
if command_exists wasmtime; then
echo "✅ wasmtime installed and available."
return 0
else
echo "❌ wasmtime still not available after install. Please add $cargo_bin to your PATH."
return 1
fi
}
# Ensure wasm32-wasi target is installed
ensure_wasi_target() {
if ! command_exists rustup; then
echo "⚠️ rustup not found; cannot auto-add wasm32-wasi target. Skipping wasm builds."
return 1
fi
echo "🔧 Ensuring a WASI-compatible target is installed (trying 'wasm32-wasi' then 'wasm32-wasip1')..."
# Try wasm32-wasi first
if rustup target add wasm32-wasi >/dev/null 2>&1; then
wasm_target="wasm32-wasi"
echo "✅ wasm32-wasi target ready."
return 0
fi
# Fallback to wasm32-wasip1 when wasm32-wasi isn't supported on this toolchain
if rustup target add wasm32-wasip1 >/dev/null 2>&1; then
wasm_target="wasm32-wasip1"
echo "✅ wasm32-wasip1 target ready (will use wasm32-wasip1)."
return 0
fi
echo "❌ Failed to add a WASI-compatible target (wasm32-wasi / wasm32-wasip1)."
return 1
}
# default wasm target name; may be overridden by ensure_wasi_target
wasm_target="wasm32-wasi"
# Build and run a native benchmark by executing only real executables found in
# the target deps directory. This avoids attempting to run .wasm/.pdb/.d files.
run_native_bench() {
bench="$1"
host="$2"
if [ -n "$host" ] && [[ "$host" == *-* ]]; then
echo "📦 Building native bench '$bench' for $host (release)..."
if ! cargo build -q --target "$host" --release --bench "$bench" >/dev/null 2>&1; then
echo "❌ Failed to build native bench $bench for $host"
return 1
fi
search_dir="target/$host/release/deps"
else
echo "📦 Building native bench '$bench' (release)..."
if ! cargo build -q --release --bench "$bench" >/dev/null 2>&1; then
echo "❌ Failed to build native bench $bench"
return 1
fi
search_dir="target/release/deps"
fi
native_files=$(find "$search_dir" -type f -executable -name "*$bench*" 2>/dev/null || true)
if [ -z "$native_files" ]; then
echo "⚠️ No native benchmark artifact found to run for $bench in $search_dir."
return 1
fi
for f in $native_files; do
# Prefer using 'file' to determine if this artifact is an actual executable/script
runnable=false
if command_exists file; then
desc=$(file -b "$f" 2>/dev/null || true)
if echo "$desc" | grep -E 'ELF|executable|script|Mach-O' >/dev/null 2>&1; then
runnable=true
fi
else
# Fallback: require executable bit and avoid common non-binary extensions like .d
if [ -x "$f" ] && ! [[ "$f" == *.d ]]; then
runnable=true
fi
fi
if [ "$runnable" != true ]; then
echo "⚠️ Skipping non-runnable artifact: $f"
continue
fi
echo "▶ Running native benchmark: $f"
"$f"
if [ $? -ne 0 ]; then
echo "❌ native benchmark run failed: $f"
return 1
fi
done
return 0
}
echo ""
echo "📊 Available Benchmark Suites:"
echo "1. Full Benchmarks (comprehensive, ~5-10 minutes)"
echo "2. Micro Benchmarks (quick, ~30 seconds)"
echo "3. All Benchmarks"
echo ""
# CLI arg parsing: --suite <full|micro|all|1|2|3> --toolchain <wasm|native|both|1|2|3>
while [ $# -gt 0 ]; do
case "$1" in
-s|--suite)
suite_arg="$2"; shift 2; ;;
-o|--report-out)
report_out="$2"; shift 2; ;;
-t|--toolchain)
tool_arg="$2"; shift 2; ;;
--help)
echo "Usage: $0 [-s|--suite <full|micro|all|1|2|3>] [-t|--toolchain <wasm|native|both|1|2|3>]"; exit 0 ;;
*)
echo "Unknown argument: $1"; shift ;;
esac
done
choice=""
if [ -n "$suite_arg" ]; then
case "${suite_arg,,}" in
1|full) choice=1 ;;
2|micro) choice=2 ;;
3|all) choice=3 ;;
*) echo "⚠️ Unknown suite '$suite_arg'; falling back to interactive prompt." ;;
esac
fi
toolChoice=""
if [ -n "$tool_arg" ]; then
case "${tool_arg,,}" in
1|wasm) toolChoice=1 ;;
2|native) toolChoice=2 ;;
3|both) toolChoice=3 ;;
*) echo "⚠️ Unknown toolchain '$tool_arg'; falling back to interactive prompt." ;;
esac
fi
if [ -z "$choice" ]; then
read -p "Select benchmark suite (1-3, or 'q' to quit): " choice
fi
if [ -z "$toolChoice" ]; then
echo ""
echo "Which toolchains should the benchmarks run on?"
echo "1. wasm (wasm32-wasi + wasmtime)"
echo "2. native (host architecture)"
echo "3. both"
read -p "Select toolchain (1-3): " toolChoice
fi
runWasm=false
runNative=false
case $toolChoice in
1) runWasm=true ;;
2) runNative=true ;;
3) runWasm=true; runNative=true ;;
*) echo "⚠️ Invalid toolchain choice; defaulting to native only."; runNative=true ;;
esac
# Prepare wasm support only when requested
canRunWasm=false
if $runWasm; then
if ensure_wasmtime && ensure_wasi_target; then
canRunWasm=true
else
echo "⚠️ wasm toolchain unavailable; wasm runs will be skipped."
canRunWasm=false
fi
fi
# Auto-detect host target triple (fallback to x86_64-pc-windows-msvc)
host_target="x86_64-pc-windows-msvc"
if command_exists rustc; then
host_line=$(rustc -vV 2>/dev/null | grep '^host:')
if [ -n "$host_line" ]; then
host_target=$(echo "$host_line" | awk '{print $2}')
else
echo "⚠️ Could not detect host target via rustc; defaulting to $host_target"
fi
else
echo "⚠️ rustc not found; defaulting host target to $host_target"
fi
case $choice in
1)
echo ""
echo "🔄 Running Full Benchmark Suite..."
echo "-----------------------------------"
if $runWasm && $canRunWasm; then
echo "📦 Building wasm full benchmark (release) for $wasm_target..."
cargo build --target "$wasm_target" --release --bench palette_extraction_benchmarks
if [ $? -ne 0 ]; then
echo "⚠️ Building wasm full benchmark failed; continuing to native run."
else
wasm_files=$(find "target/$wasm_target/release/deps" -name "*palette_extraction_benchmarks*.wasm" 2>/dev/null || true)
if [ -n "$wasm_files" ]; then
for f in $wasm_files; do
echo "▶ Running wasm full benchmark: $f"
wasmtime "$f"
if [ $? -ne 0 ]; then
echo "❌ wasm full benchmark run failed: $f"
fi
done
else
echo "⚠️ No wasm full benchmark artifact found to run."
fi
fi
else
echo "⚠️ Skipping wasm full benchmark."
fi
if $runNative; then
echo "\n📊 Running full benchmarks (native) on $host_target..."
if run_native_bench "palette_extraction_benchmarks" "$host_target"; then
echo "✅ Full benchmarks completed!"
else
echo "❌ Full benchmarks failed!"
exit 1
fi
fi
;;
2)
echo ""
echo "⚡ Running Micro Benchmark Suite..."
echo "------------------------------------"
if $runNative; then
if run_native_bench "micro_benchmarks" "$host_target"; then
echo "✅ Micro benchmarks (native) completed!"
else
echo "❌ Micro benchmarks (native) failed!"
exit 1
fi
fi
if $runWasm && $canRunWasm; then
echo "📦 Building wasm micro benchmark (release) for $wasm_target..."
cargo build --target "$wasm_target" --release --bench micro_benchmarks
if [ $? -ne 0 ]; then
echo "❌ Building wasm micro benchmark failed; skipping wasm run."
else
wasm_files=$(find "target/$wasm_target/release/deps" -name "*micro_benchmarks*.wasm" 2>/dev/null || true)
if [ -n "$wasm_files" ]; then
for f in $wasm_files; do
echo "▶ Running wasm micro benchmark: $f"
wasmtime "$f"
if [ $? -ne 0 ]; then
echo "❌ wasm micro benchmark run failed: $f"
fi
done
else
echo "⚠️ No wasm micro benchmark artifact found to run."
fi
fi
fi
;;
3)
echo ""
echo "🔄 Running All Benchmark Suites..."
echo "-----------------------------------"
if $runNative; then
echo "⚡ Starting with micro benchmarks (native) on $host_target..."
if ! run_native_bench "micro_benchmarks" "$host_target"; then
echo "❌ Micro benchmarks (native) failed!"
exit 1
fi
echo ""
echo "📊 Running full benchmarks (native) on $host_target..."
if ! run_native_bench "palette_extraction_benchmarks" "$host_target"; then
echo "❌ Full benchmarks (native) failed!"
exit 1
fi
fi
if $runWasm && $canRunWasm; then
echo "\n🔁 Running wasm variants..."
echo "📦 Building wasm micro benchmark (release) for $wasm_target..."
cargo build --target "$wasm_target" --release --bench micro_benchmarks
if [ $? -eq 0 ]; then
wasm_files=$(find "target/$wasm_target/release/deps" -name "*micro_benchmarks*.wasm" 2>/dev/null || true)
if [ -n "$wasm_files" ]; then
for f in $wasm_files; do
echo "▶ Running wasm micro benchmark: $f"
wasmtime "$f"
done
fi
else
echo "⚠️ Building wasm micro benchmark failed; skipping."
fi
echo "📦 Building wasm full benchmark (release) for $wasm_target..."
cargo build --target "$wasm_target" --release --bench palette_extraction_benchmarks
if [ $? -eq 0 ]; then
wasm_files=$(find "target/$wasm_target/release/deps" -name "*palette_extraction_benchmarks*.wasm" 2>/dev/null || true)
if [ -n "$wasm_files" ]; then
for f in $wasm_files; do
echo "▶ Running wasm full benchmark: $f"
wasmtime "$f"
done
fi
else
echo "⚠️ Building wasm full benchmark failed; skipping."
fi
fi
;;
q)
echo "👋 Goodbye!"
exit 0
;;
*)
echo "❌ Invalid choice. Please select 1, 2, 3, or 'q'"
exit 1
;;
esac
echo ""
echo "📈 Benchmark Results Location:"
echo "- HTML Reports: target/criterion/"
echo "- Raw Data: target/criterion/*/base/"
echo ""
echo "💡 Tips:"
echo "- View detailed HTML reports by opening target/criterion/report/index.html"
echo "- Compare performance over time with: cargo bench -- --save-baseline baseline_name"
echo "- Run specific benchmarks with: cargo bench pattern_name"
# Optionally copy the Criterion HTML report to a destination directory
copy_criterion_report() {
src="target/criterion/report"
if [ ! -d "$src" ]; then
echo "⚠️ Criterion report not found at '$src'. Nothing to copy."
return 1
fi
if [ -z "$1" ]; then
dest="$(pwd)/criterion-report"
else
dest="$1"
fi
mkdir -p "$dest"
echo "📋 Copying Criterion report from '$src' to '$dest'..."
cp -r "$src"/* "$dest"/
if [ $? -eq 0 ]; then
echo "✅ Report copied to: $dest"
return 0
else
echo "❌ Failed to copy report to: $dest"
return 1
fi
}
if [ -n "$report_out" ]; then
copy_criterion_report "$report_out"
else
# Only prompt if stdin is a terminal
if [ -t 0 ]; then
read -p "Would you like to copy the HTML report to a directory? (y/N): " copy_choice
if [ "${copy_choice,,}" = "y" ]; then
read -p "Enter destination directory path (leave blank for ./criterion-report): " dest_path
if [ -z "$dest_path" ]; then
copy_criterion_report
else
copy_criterion_report "$dest_path"
fi
fi
else
echo "ℹ️ Non-interactive session: to copy the Criterion report provide --report-out <path>"
fi
fi