Skip to content

Instantly share code, notes, and snippets.

@GaetanoPiazzolla
Last active June 22, 2024 15:51
Show Gist options
  • Save GaetanoPiazzolla/eca111d55a279ce025bfa8d190d612cb to your computer and use it in GitHub Desktop.
Save GaetanoPiazzolla/eca111d55a279ce025bfa8d190d612cb to your computer and use it in GitHub Desktop.
Useful to fast-check startup time of a spring-boot app with CDS and Jar Extract.
#!/bin/bash
mkfifo pipe
APP_NAME="xxx"
GRADLE_OR_MAVEN="gradle"
JAR_NAME="xxx-0.0.1-SNAPSHOT.jar"
# Set to 0 for standard startup
# Only for Java 21+ and Spring Boot 3.3
# - Set to 1 to use fast startup with the new JAR format
# - Set to 2 to use CDS + Jar Format
# - Set to 3 to use CDS + Jar Format + Tiered Compilation
FAST_STARTUP=3
# Create the Jar
if [ "$GRADLE_OR_MAVEN" = "gradle" ]; then
./gradlew clean bootJar
else
mvn clean install
fi
# Decompress the JAR for 1, 2 and 3
if [ "$FAST_STARTUP" -ge 1 ]; then
java -Djarmode=tools -jar "build/libs/$JAR_NAME" extract --destination fast-jar --force
fi
# Training run for 2 and 3
if [ "$FAST_STARTUP" -ge 2 ]; then
java -XX:ArchiveClassesAtExit=application.jsa -Dspring.context.exit=onRefresh -jar fast-jar/$JAR_NAME
fi
# Run the application
if [ "$FAST_STARTUP" = 0 ]; then
java -jar "./build/libs/$JAR_NAME" > pipe & PID=$!
elif [ "$FAST_STARTUP" = 1 ]; then
java -jar fast-jar/$JAR_NAME > pipe & PID=$!
elif [ "$FAST_STARTUP" = 2 ]; then
java -Xshare:on -XX:SharedArchiveFile=application.jsa -jar "fast-jar/$JAR_NAME" > pipe & PID=$!
elif [ "$FAST_STARTUP" = 3 ]; then
java -Xshare:on -XX:TieredStopAtLevel=1 -XX:SharedArchiveFile=application.jsa -jar "fast-jar/$JAR_NAME" > pipe & PID=$!
else
echo "Invalid FAST_STARTUP value"
exit 1
fi
# Wait for the application to start
while IFS= read -r line
do
echo "$line"
if [[ "$line" == *"Started $APP_NAME"* ]]; then
echo "----------------- Application started -----------------"
break
fi
done < pipe
# Kill the application
kill $PID
rm pipe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment