From 8101fd4da98adf41fe0a04af7f537ba749982f16 Mon Sep 17 00:00:00 2001 From: Jeremy Walker Date: Wed, 18 Nov 2026 12:38:28 +0900 Subject: [PATCH] Start the JVM from an ahead-of-time cache created at image build Every run spent more than a second loading and linking the classes of javac, JUnit and AssertJ before it compiled or ran any tests. When the image is built, bin/create-aot-cache.sh now runs the test runner once on a sample exercise in aot-training/ with -XX:AOTCacheOutput. The JVM saves the classes loaded during that run, already loaded and linked, in java-test-runner.aot. bin/run.sh passes that cache to the JVM with -XX:AOTCache, so each run starts from that state. If the JVM can't use the cache, it will print an error and run as before. This takes the median run over the track's 169 example solutions from 3.36s to 1.90s, with identical results. The image grows by about 50 MB. Co-Authored-By: Claude Opus 5.5 --- Dockerfile | 3 + aot-training/src/main/java/Training.java | 24 +++++ aot-training/src/test/java/TrainingTest.java | 92 ++++++++++++++++++++ bin/create-aot-cache.sh | 31 +++++++ bin/run.sh | 6 +- 5 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 aot-training/src/main/java/Training.java create mode 100644 aot-training/src/test/java/TrainingTest.java create mode 100644 bin/create-aot-cache.sh diff --git a/Dockerfile b/Dockerfile index dc867c7..4d8d947 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,5 +9,8 @@ FROM eclipse-temurin:27_35-jdk@sha256:473d02d477a8152a05dc42fde10b965754f1863d45 WORKDIR /opt/test-runner COPY bin/run.sh bin/run.sh COPY --from=build /app/build/libs/java-test-runner.jar . +COPY bin/create-aot-cache.sh bin/create-aot-cache.sh +COPY aot-training aot-training +RUN sh bin/create-aot-cache.sh ENTRYPOINT ["sh", "/opt/test-runner/bin/run.sh"] diff --git a/aot-training/src/main/java/Training.java b/aot-training/src/main/java/Training.java new file mode 100644 index 0000000..af7f157 --- /dev/null +++ b/aot-training/src/main/java/Training.java @@ -0,0 +1,24 @@ +import java.util.List; +import java.util.stream.Collectors; + +class Training { + + String greet(String name) { + System.out.println("Greeting " + name); + return "Hello, " + name + "!"; + } + + int divide(int a, int b) { + return a / b; + } + + List shout(List words) { + return words.stream().map(String::toUpperCase).collect(Collectors.toList()); + } + + void reject(String input) { + if (input.isEmpty()) { + throw new IllegalArgumentException("Input must not be empty"); + } + } +} diff --git a/aot-training/src/test/java/TrainingTest.java b/aot-training/src/test/java/TrainingTest.java new file mode 100644 index 0000000..1c16e1a --- /dev/null +++ b/aot-training/src/test/java/TrainingTest.java @@ -0,0 +1,92 @@ +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.List; +import java.util.stream.Stream; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +// This exercise is run once while the Docker image is built, to record which classes +// the test runner loads and how it uses them (see bin/create-aot-cache.sh). +// Its tests cover the paths a typical run takes: passing, failing and erroring tests, +// parameterized tests, printed output and task tags. Some tests fail on purpose. +public class TrainingTest { + + private Training training; + + @BeforeEach + public void setup() { + training = new Training(); + } + + @Test + @Tag("task:1") + @DisplayName("greets by name") + public void greetsByName() { + assertThat(training.greet("Alice")).isEqualTo("Hello, Alice!"); + } + + @Test + @Tag("task:1") + public void failsAnAssertJAssertion() { + assertThat(training.greet("Bob")).isEqualTo("Hi, Bob!"); + } + + @Test + @Tag("task:2") + public void failsAJUnitAssertion() { + assertEquals(3, training.divide(7, 3)); + } + + @Test + @Tag("task:2") + public void throwsAnUnexpectedException() { + assertThat(training.divide(1, 0)).isZero(); + } + + @Test + public void checksAnExpectedException() { + assertThatThrownBy(() -> training.reject("")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Input must not be empty"); + assertThrows(IllegalArgumentException.class, () -> training.reject("")); + } + + @Test + public void comparesLists() { + assertThat(training.shout(List.of("a", "b"))).containsExactly("A", "B"); + assertThat(training.shout(List.of("a", "b"))).containsExactly("B", "A"); + } + + @ParameterizedTest + @MethodSource("divisions") + public void dividesFromAMethodSource(int a, int b, int expected) { + assertThat(training.divide(a, b)).isEqualTo(expected); + } + + private static Stream divisions() { + return Stream.of(Arguments.of(6, 3, 2), Arguments.of(9, 3, 4)); + } + + @ParameterizedTest + @CsvSource({"Alice, 'Hello, Alice!'", "Bob, 'Hello, Bob!'"}) + public void greetsFromACsvSource(String name, String expected) { + assertThat(training.greet(name)).isEqualTo(expected); + } + + @ParameterizedTest(name = "{0} is not empty") + @ValueSource(strings = {"a", "b"}) + public void acceptsFromAValueSource(String input) { + training.reject(input); + } +} diff --git a/bin/create-aot-cache.sh b/bin/create-aot-cache.sh new file mode 100644 index 0000000..e7bab1e --- /dev/null +++ b/bin/create-aot-cache.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env sh + +# Synopsis: +# Create the JVM's ahead-of-time (AOT) cache for the test runner. +# This runs once, while the Docker image is built. + +# Output: +# Writes /opt/test-runner/java-test-runner.aot, which bin/run.sh passes to the JVM. + +# Without a cache, every run spends more than a second finding, loading and linking +# the classes of javac, JUnit and AssertJ, and running their code in the interpreter. +# This script runs the test runner once on the sample exercise in aot-training/. +# The JVM records every class loaded during that run and saves them, already loaded +# and linked, in the cache, along with profiling data for their methods. +# Every later run starts from that state. + +# The JVM only uses the cache when a run uses the same jar and compatible options, +# so this must use the same options as bin/run.sh. If the JVM can't use the cache, +# it will print an error and run as before, without the speed-up. + +set -e + +training_folder="/tmp/aot-training" + +rm -rf "${training_folder}" +cp -R /opt/test-runner/aot-training "${training_folder}" + +java -XX:AOTCacheOutput=/opt/test-runner/java-test-runner.aot \ + -jar /opt/test-runner/java-test-runner.jar training "${training_folder}" "${training_folder}" + +rm -rf "${training_folder}" diff --git a/bin/run.sh b/bin/run.sh index 743244f..76adb87 100755 --- a/bin/run.sh +++ b/bin/run.sh @@ -38,4 +38,8 @@ cp -R $input_folder/* . find . -mindepth 1 -type f | grep 'Test.java' | xargs -I file sed -i "s/@Ignore(.*)//g;s/@Ignore//g;s/@Disabled(.*)//g;s/@Disabled//g;" file -java -jar /opt/test-runner/java-test-runner.jar $problem_slug . $output_folder +# -XX:AOTCache loads the JVM's ahead-of-time cache, created by bin/create-aot-cache.sh +# when the image is built. The classes of javac, JUnit and AssertJ come from the cache +# already loaded and linked, which halves the time of a typical run. +java -XX:AOTCache=/opt/test-runner/java-test-runner.aot \ + -jar /opt/test-runner/java-test-runner.jar $problem_slug . $output_folder