Skip to content

Commit 0f4b0df

Browse files
brunoborgesCopilot
andcommitted
Fix benchmark: detect failed commands and install cairo deps
- measure() now verifies command exit code; reports FAIL instead of a misleadingly fast time when a command crashes - avg_runs() handles individual failures gracefully - Install libcairo2-dev on Ubuntu for cairosvg PNG generation - Fix applies to both CI workflow and local run.sh Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 1eaa4c4 commit 0f4b0df

File tree

2 files changed

+56
-17
lines changed

2 files changed

+56
-17
lines changed

.github/workflows/benchmark.yml

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ jobs:
2828
with:
2929
python-version: '3.13'
3030

31+
- name: Install system dependencies (Ubuntu)
32+
if: runner.os == 'Linux'
33+
run: sudo apt-get update && sudo apt-get install -y libcairo2-dev
34+
3135
- name: Install Python dependencies
3236
run: pip install pyyaml cairosvg
3337

@@ -47,19 +51,38 @@ jobs:
4751
# Timing helper using bash TIMEFORMAT
4852
measure() {
4953
TIMEFORMAT='%R'
50-
local t
51-
t=$( { time "$@" > /dev/null 2>&1; } 2>&1 )
54+
local t ec
55+
t=$( { time "$@" > /dev/null 2>&1; ec=$?; } 2>&1 )
56+
if [[ ${ec:-1} -ne 0 ]]; then
57+
# Re-check: the subshell exit code isn't always captured
58+
if ! "$@" > /dev/null 2>&1; then
59+
echo "FAIL"
60+
return 1
61+
fi
62+
fi
5263
echo "$t"
5364
}
5465
5566
avg_runs() {
5667
local n="$1"; shift
57-
local sum=0
68+
local sum=0 failures=0
5869
for ((i = 1; i <= n; i++)); do
5970
local t
60-
t=$(measure "$@")
71+
t=$(measure "$@") || true
72+
if [[ "$t" == "FAIL" ]]; then
73+
((failures++)) || true
74+
continue
75+
fi
6176
sum=$(awk "BEGIN {print $sum + $t}")
6277
done
78+
local success=$((n - failures))
79+
if [[ $success -eq 0 ]]; then
80+
echo "FAIL"
81+
return 0
82+
fi
83+
awk "BEGIN {printf \"%.2f\", $sum / $success}"
84+
}
85+
done
6386
awk "BEGIN {printf \"%.2f\", $sum / $n}"
6487
}
6588
@@ -194,6 +217,10 @@ jobs:
194217
with:
195218
python-version: '3.13'
196219

220+
- name: Install system dependencies (Ubuntu)
221+
if: runner.os == 'Linux'
222+
run: sudo apt-get update && sudo apt-get install -y libcairo2-dev
223+
197224
- name: Install Python dependencies
198225
run: pip install pyyaml cairosvg
199226

@@ -212,8 +239,14 @@ jobs:
212239
213240
measure() {
214241
TIMEFORMAT='%R'
215-
local t
216-
t=$( { time "$@" > /dev/null 2>&1; } 2>&1 )
242+
local t ec
243+
t=$( { time "$@" > /dev/null 2>&1; ec=$?; } 2>&1 )
244+
if [[ ${ec:-1} -ne 0 ]]; then
245+
if ! "$@" > /dev/null 2>&1; then
246+
echo "FAIL"
247+
return 1
248+
fi
249+
fi
217250
echo "$t"
218251
}
219252

html-generators/benchmark/run.sh

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,35 @@ UPDATE_MD=false
3030
# ---------------------------------------------------------------------------
3131
# Helpers
3232
# ---------------------------------------------------------------------------
33-
time_once() {
34-
/usr/bin/time -p "$@" > /dev/null 2>&1 | awk '/^real/ {print $2}'
35-
# fallback: capture from stderr
36-
local t
37-
t=$( { /usr/bin/time -p "$@" > /dev/null; } 2>&1 | awk '/^real/ {print $2}' )
38-
echo "$t"
39-
}
40-
4133
measure() {
4234
local t
4335
t=$( { /usr/bin/time -p "$@" > /dev/null; } 2>&1 | awk '/^real/ {print $2}' )
36+
# Verify the command actually succeeded
37+
if ! "$@" > /dev/null 2>&1; then
38+
echo "FAIL"
39+
return 1
40+
fi
4441
echo "$t"
4542
}
4643

4744
avg_runs() {
4845
local n="$1"; shift
49-
local sum=0
46+
local sum=0 failures=0
5047
for ((i = 1; i <= n; i++)); do
5148
local t
52-
t=$(measure "$@")
49+
t=$(measure "$@") || true
50+
if [[ "$t" == "FAIL" ]]; then
51+
((failures++)) || true
52+
continue
53+
fi
5354
sum=$(echo "$sum + $t" | bc)
5455
done
55-
echo "scale=2; $sum / $n" | bc | sed 's/^\./0./'
56+
local success=$((n - failures))
57+
if [[ $success -eq 0 ]]; then
58+
echo "FAIL"
59+
return 0
60+
fi
61+
echo "scale=2; $sum / $success" | bc | sed 's/^\./0./'
5662
}
5763

5864
# ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)