-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·299 lines (234 loc) · 11.2 KB
/
run.sh
File metadata and controls
executable file
·299 lines (234 loc) · 11.2 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
#!/usr/bin/env bash
# Benchmark the HTML and OG card generators across languages and execution methods.
#
# Phase 1: Training/build cost (one-time setup)
# - Python first run (creates __pycache__)
# - Java AOT training run (creates .aot)
# - JBang export (creates fat JAR)
#
# Phase 2: Steady-state execution (5 runs averaged)
# - Python (warm, with __pycache__)
# - JBang (from source)
# - Fat JAR (java -jar)
# - Fat JAR + AOT (java -XX:AOTCache)
#
# Usage:
# ./html-generators/benchmark/run.sh # print results to stdout
# ./html-generators/benchmark/run.sh --update # also update LOCAL.md
set -euo pipefail
cd "$(git rev-parse --show-toplevel)"
JAR="html-generators/generate.jar"
AOT="html-generators/generate.aot"
OG_JAR="html-generators/generateog.jar"
OG_AOT="html-generators/generateog.aot"
STEADY_RUNS=5
UPDATE_MD=false
[[ "${1:-}" == "--update" ]] && UPDATE_MD=true
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
measure() {
local start end
start=$(python3 -c "import time; print(time.time())")
if "$@" > /dev/null 2>&1; then
end=$(python3 -c "import time; print(time.time())")
python3 -c "print(f'{$end - $start:.2f}')"
else
echo "FAIL"
fi
}
avg_runs() {
local n="$1"; shift
local sum=0 success=0
for ((i = 1; i <= n; i++)); do
local t
t=$(measure "$@")
if [[ "$t" != "FAIL" ]]; then
sum=$(echo "$sum + $t" | bc)
success=$((success + 1))
fi
done
if [[ $success -eq 0 ]]; then
echo "FAIL"
else
echo "scale=2; $sum / $success" | bc | sed 's/^\./0./'
fi
}
# ---------------------------------------------------------------------------
# Environment
# ---------------------------------------------------------------------------
CPU=$(sysctl -n machdep.cpu.brand_string 2>/dev/null || lscpu 2>/dev/null | awk -F: '/Model name/ {gsub(/^ +/,"",$2); print $2}' || echo "unknown")
RAM=$(sysctl -n hw.memsize 2>/dev/null | awk '{printf "%d GB", $1/1024/1024/1024}' || free -h 2>/dev/null | awk '/Mem:/ {print $2}' || echo "unknown")
JAVA_VER=$(java -version 2>&1 | head -1 | sed 's/.*"\(.*\)".*/\1/')
JBANG_VER=$(jbang version 2>/dev/null || echo "n/a")
PYTHON_VER=$(python3 --version 2>/dev/null | awk '{print $2}' || echo "n/a")
OS=$(uname -s)
SNIPPET_COUNT=$(find content \( -name '*.json' -o -name '*.yaml' -o -name '*.yml' \) -not -name 'template.*' | wc -l | tr -d ' ')
echo ""
echo "Environment: $CPU · $RAM · Java $JAVA_VER · $OS"
echo "Snippets: $SNIPPET_COUNT across 11 categories"
echo ""
# ---------------------------------------------------------------------------
# Phase 1: Training / build cost (one-time)
# ---------------------------------------------------------------------------
echo "=== Phase 1: Training / Build Cost (one-time) ==="
echo ""
# Clean up any cached state
rm -f html-generators/generate.aot html-generators/generate.jar
rm -f html-generators/generateog.aot html-generators/generateog.jar
find html-generators -name '__pycache__' -type d -exec rm -rf {} + 2>/dev/null || true
# Python first run (populates __pycache__)
PY_TRAIN=$(measure python3 html-generators/generate.py)
echo " Python first run (creates __pycache__): ${PY_TRAIN}s"
# JBang export (creates fat JAR)
JBANG_EXPORT=$(measure jbang export fatjar --force --output "$JAR" html-generators/generate.java)
echo " JBang export (creates fat JAR): ${JBANG_EXPORT}s"
# AOT training run (creates .aot from JAR)
AOT_TRAIN=$(measure java -XX:AOTCacheOutput="$AOT" -jar "$JAR")
echo " AOT training run (creates .aot): ${AOT_TRAIN}s"
echo ""
echo "--- OG Card Generator ---"
echo ""
OG_PY_TRAIN=$(measure python3 html-generators/generateog.py)
echo " Python first run (creates __pycache__): ${OG_PY_TRAIN}s"
OG_JBANG_EXPORT=$(measure jbang export fatjar --force --output "$OG_JAR" html-generators/generateog.java)
echo " JBang export (creates fat JAR): ${OG_JBANG_EXPORT}s"
OG_AOT_TRAIN=$(measure java -XX:AOTCacheOutput="$OG_AOT" -jar "$OG_JAR")
echo " AOT training run (creates .aot): ${OG_AOT_TRAIN}s"
echo ""
# ---------------------------------------------------------------------------
# Phase 2: Steady-state execution (averaged over $STEADY_RUNS runs)
# ---------------------------------------------------------------------------
echo "=== Phase 2: Steady-State Execution (avg of $STEADY_RUNS runs) ==="
echo ""
PY_STEADY=$(avg_runs $STEADY_RUNS python3 html-generators/generate.py)
echo " Python (warm): ${PY_STEADY}s"
JBANG_STEADY=$(avg_runs $STEADY_RUNS jbang html-generators/generate.java)
echo " JBang (from source): ${JBANG_STEADY}s"
JAR_STEADY=$(avg_runs $STEADY_RUNS java -jar "$JAR")
echo " Fat JAR: ${JAR_STEADY}s"
AOT_STEADY=$(avg_runs $STEADY_RUNS java -XX:AOTCache="$AOT" -jar "$JAR")
echo " Fat JAR + AOT: ${AOT_STEADY}s"
echo ""
echo "--- OG Card Generator ---"
echo ""
OG_PY_STEADY=$(avg_runs $STEADY_RUNS python3 html-generators/generateog.py)
echo " Python (warm): ${OG_PY_STEADY}s"
OG_JBANG_STEADY=$(avg_runs $STEADY_RUNS jbang html-generators/generateog.java)
echo " JBang (from source): ${OG_JBANG_STEADY}s"
OG_JAR_STEADY=$(avg_runs $STEADY_RUNS java -jar "$OG_JAR")
echo " Fat JAR: ${OG_JAR_STEADY}s"
OG_AOT_STEADY=$(avg_runs $STEADY_RUNS java -XX:AOTCache="$OG_AOT" -jar "$OG_JAR")
echo " Fat JAR + AOT: ${OG_AOT_STEADY}s"
echo ""
# ---------------------------------------------------------------------------
# Phase 3: CI cold start (no caches, simulates fresh runner)
# ---------------------------------------------------------------------------
echo "=== Phase 3: CI Cold Start (fresh runner, no caches) ==="
echo ""
find html-generators -name '__pycache__' -type d -exec rm -rf {} + 2>/dev/null || true
PY_CI=$(measure python3 html-generators/generate.py)
echo " Python (no __pycache__): ${PY_CI}s"
jbang cache clear > /dev/null 2>&1 || true
JBANG_CI=$(measure jbang html-generators/generate.java)
echo " JBang (no cache): ${JBANG_CI}s"
JAR_CI=$(measure java -jar "$JAR")
echo " Fat JAR: ${JAR_CI}s"
AOT_CI=$(measure java -XX:AOTCache="$AOT" -jar "$JAR")
echo " Fat JAR + AOT: ${AOT_CI}s"
echo ""
echo "--- OG Card Generator ---"
echo ""
find html-generators -name '__pycache__' -type d -exec rm -rf {} + 2>/dev/null || true
OG_PY_CI=$(measure python3 html-generators/generateog.py)
echo " Python (no __pycache__): ${OG_PY_CI}s"
OG_JAR_CI=$(measure java -jar "$OG_JAR")
echo " Fat JAR: ${OG_JAR_CI}s"
OG_AOT_CI=$(measure java -XX:AOTCache="$OG_AOT" -jar "$OG_JAR")
echo " Fat JAR + AOT: ${OG_AOT_CI}s"
echo ""
# ---------------------------------------------------------------------------
# Optionally update LOCAL.md
# ---------------------------------------------------------------------------
if $UPDATE_MD; then
MD="html-generators/benchmark/LOCAL.md"
cat > "$MD" <<EOF
# Local Benchmark Results
Local benchmark results from \`run.sh\`. These will differ from CI because of OS file caching and warm \`__pycache__/\`.
## HTML Generator
### Phase 1: Training / Build Cost (one-time)
These are one-time setup costs, comparable across languages.
| Step | Time | What it does |
|------|------|-------------|
| Python first run | ${PY_TRAIN}s | Interprets source, creates \`__pycache__\` bytecode |
| JBang export | ${JBANG_EXPORT}s | Compiles source + bundles dependencies into fat JAR |
| AOT training run | ${AOT_TRAIN}s | Runs JAR once to record class loading, produces \`.aot\` cache |
### Phase 2: Steady-State Execution (avg of $STEADY_RUNS runs)
After one-time setup, these are the per-run execution times.
| Method | Avg Time | Notes |
|--------|---------|-------|
| **Fat JAR + AOT** | **${AOT_STEADY}s** | Fastest; pre-loaded classes from AOT cache |
| **Fat JAR** | ${JAR_STEADY}s | JVM class loading on every run |
| **JBang** | ${JBANG_STEADY}s | Includes JBang launcher overhead |
| **Python** | ${PY_STEADY}s | Uses cached \`__pycache__\` bytecode |
### Phase 3: CI Cold Start (simulated locally)
Clears \`__pycache__/\` and JBang cache, then measures a single run. On a local machine the OS file cache still helps, so these numbers are faster than true CI.
| Method | Time | Notes |
|--------|------|-------|
| **Fat JAR + AOT** | **${AOT_CI}s** | AOT cache ships pre-loaded classes |
| **Fat JAR** | ${JAR_CI}s | JVM class loading from scratch |
| **JBang** | ${JBANG_CI}s | Must compile source before running |
| **Python** | ${PY_CI}s | No \`__pycache__\`; full interpretation |
## OG Card Generator
### Phase 1: Training / Build Cost (one-time)
| Step | Time | What it does |
|------|------|-------------|
| Python first run | ${OG_PY_TRAIN}s | Generates SVG+PNG via cairosvg |
| JBang export | ${OG_JBANG_EXPORT}s | Compiles source + bundles Batik dependencies into fat JAR |
| AOT training run | ${OG_AOT_TRAIN}s | Runs JAR once to record class loading, produces \`.aot\` cache |
### Phase 2: Steady-State Execution (avg of $STEADY_RUNS runs)
| Method | Avg Time | Notes |
|--------|---------|-------|
| **Fat JAR + AOT** | **${OG_AOT_STEADY}s** | Fastest; pre-loaded classes from AOT cache |
| **Fat JAR** | ${OG_JAR_STEADY}s | JVM class loading on every run |
| **JBang** | ${OG_JBANG_STEADY}s | Includes JBang launcher overhead |
| **Python** | ${OG_PY_STEADY}s | Uses cached \`__pycache__\` bytecode |
### Phase 3: CI Cold Start (simulated locally)
| Method | Time | Notes |
|--------|------|-------|
| **Fat JAR + AOT** | **${OG_AOT_CI}s** | AOT cache ships pre-loaded classes |
| **Fat JAR** | ${OG_JAR_CI}s | JVM class loading from scratch |
| **Python** | ${OG_PY_CI}s | No \`__pycache__\`; full interpretation |
## How each method works
- **Python** caches compiled bytecode in \`__pycache__/\` after the first run, similar to how Java's AOT cache works. But this cache is local-only and not available in CI.
- **Java AOT** (JEP 483) snapshots ~3,300 pre-loaded classes from a training run into a \`.aot\` file, eliminating class loading overhead on subsequent runs. The \`.aot\` file is stored in the GitHub Actions cache.
- **JBang** compiles and caches internally but adds launcher overhead on every invocation.
- **Fat JAR** (\`java -jar\`) loads and links all classes from scratch each time.
## AOT Cache Setup
\`\`\`bash
# HTML generator
jbang export fatjar --force --output html-generators/generate.jar html-generators/generate.java
java -XX:AOTCacheOutput=html-generators/generate.aot -jar html-generators/generate.jar
java -XX:AOTCache=html-generators/generate.aot -jar html-generators/generate.jar
# OG card generator
jbang export fatjar --force --output html-generators/generateog.jar html-generators/generateog.java
java -XX:AOTCacheOutput=html-generators/generateog.aot -jar html-generators/generateog.jar
java -XX:AOTCache=html-generators/generateog.aot -jar html-generators/generateog.jar
\`\`\`
## Environment
| | |
|---|---|
| **CPU** | $CPU |
| **RAM** | $RAM |
| **Java** | OpenJDK $JAVA_VER (Temurin) |
| **JBang** | $JBANG_VER |
| **Python** | $PYTHON_VER |
| **OS** | $OS |
## Reproduce
\`\`\`bash
./html-generators/benchmark/run.sh # print results to stdout
./html-generators/benchmark/run.sh --update # also update this file
\`\`\`
EOF
echo "Updated $MD"
fi