Files
spring-boot-demo/spring-boot-startup-time/scripts/demo-jfr.sh
Ankur Mhatre 958b401f0f Spring Boot startup time: bean-by-bean diagnosis, and one directory per post
Adds spring-boot-startup-time/, the companion project for BLOG-618: a runnable
Spring Boot 4.1.1 application on JDK 25 that installs BufferingApplicationStartup
and FlightRecorderApplicationStartup behind a system property, and a /diag/startup
endpoint that computes step self time -- the number /actuator/startup does not give
you and the one that names the actual culprits.

Captured under docs/output/: the step tree sorted both ways, the same startup as JFR
events, a +5000-class experiment putting 0.11 ms per scanned class on the classpath
scan tax, the silent truncation a 2048-step buffer performs, and JDK 25 AOT cache
timings (6.93 s to 4.82 s). Post body and metadata live in post/.

Moves the existing Actuator project into actuator-in-production/ so the repository
holds one directory per article; the root README is now an index.
2026-09-05 00:17:37 +05:30

70 lines
2.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# Records the same startup as JFR events instead of an in-memory buffer, then reads the
# recording with the JDK's own `jfr` tool -- no Mission Control, no extra dependency.
set -uo pipefail
source "$(dirname "$0")/env.sh"
REC=/tmp/startup.jfr
rm -f "$REC"
"$(dirname "$0")/stop.sh"
"$JAVA_HOME/bin/java" \
-XX:StartFlightRecording=filename=$REC,settings=profile,dumponexit=true \
-Dstartup.tracking=jfr -jar "$JAR" > /tmp/startup-app.log 2>&1 &
PID=$!
for _ in $(seq 1 180); do curl -fs -o /dev/null localhost:8080/actuator/health && break; sleep 0.5; done
grep -h 'Started StartupDiagnosisApplication' /tmp/startup-app.log | sed 's/^.*: //'
kill -TERM $PID 2>/dev/null; wait $PID 2>/dev/null
sleep 2
echo
echo "=== jfr summary (Spring rows only) ==="
"$JAVA_HOME/bin/jfr" summary "$REC" | head -3
"$JAVA_HOME/bin/jfr" summary "$REC" | grep -i -E 'spring|Event Count|=====' | head -10
echo
echo "=== the event type, in full ==="
"$JAVA_HOME/bin/jfr" metadata "$REC" | grep -A22 '@Name("org.springframework' | head -24
echo
echo "=== the selector matters: the event is named by its FQCN, not 'StartupEvent' ==="
echo -n " --events StartupEvent -> "
"$JAVA_HOME/bin/jfr" print --events StartupEvent --json "$REC" \
| python3 -c "import sys,json;print(len(json.load(sys.stdin)['recording']['events']),'events')"
echo -n " --events 'org.springframework.core.*' -> "
"$JAVA_HOME/bin/jfr" print --events 'org.springframework.core.metrics.jfr.FlightRecorderStartupEvent' --json "$REC" \
| python3 -c "import sys,json;print(len(json.load(sys.stdin)['recording']['events']),'events')"
echo
echo "=== 8 slowest startup steps, straight out of the recording ==="
"$JAVA_HOME/bin/jfr" print --events 'org.springframework.core.metrics.jfr.FlightRecorderStartupEvent' \
--json "$REC" > /tmp/startup-jfr.json 2>/dev/null
python3 - <<'PY'
import json
d = json.load(open('/tmp/startup-jfr.json'))
ev = d['recording']['events']
print(f"{len(ev)} StartupEvent records in the recording\n")
rows = []
for e in ev:
v = e['values']
rows.append((v.get('duration', 0), v.get('name'), v.get('tags') or ''))
rows.sort(reverse=True)
print(f"{'duration':>14} name / tags")
for dur, name, tags in rows[:8]:
print(f"{dur!s:>14} {name} {tags}")
PY
echo
echo "=== what JFR gives you that the buffer does not: JVM context in the same file ==="
"$JAVA_HOME/bin/jfr" summary "$REC" | grep -E 'GCPhasePause|ClassLoad|JavaMonitorEnter|ExecutionSample|Compilation ' | head -6
echo
echo -n " total GC pause time during this startup: "
"$JAVA_HOME/bin/jfr" print --events jdk.GCPhasePause --json "$REC" | python3 -c "
import sys, json, re
ev = json.load(sys.stdin)['recording']['events']
tot = 0.0
for e in ev:
m = re.match(r'PT(?:(\d+)M)?([\d.]+)S', e['values']['duration'])
tot += (int(m.group(1) or 0) * 60 + float(m.group(2)))
print(f'{len(ev)} events, {tot*1000:.1f} ms')"
true