This demo contains the code for a simple Java Stream benchmark designed to run on the GraalVM JDK and the Oracle JDK.
-
Download and install the GraalVM JDK using SDKMAN!. For other installation options, visit the Downloads page.
sdk install java 23.0.1-graal
-
Download or clone the repository and navigate into the benchmark directory:
git clone https://github.com/graalvm/graalvm-demos
cd graalvm-demos/compiler/java-stream-benchmark
Build the benchmark with Maven:
./mvnw package
Now you can run this benchmark with whatever java
you have on your machine and compare the results between JVMs.
A note about the results: The benchmark mode is the
AverageTime
in nanoseconds per operation, which means lower numbers are better. Note that the results you see can be influenced by the hardware you are running this benchmark on, the CPU load, and other factors. Interpret them responsibly.
Run the benchmark with the default GraalVM JIT compiler by executing the target/benchmarks.jar file:
java -jar target/benchmarks.jar
Now deactivate the Graal compiler and run the benchmark on the same JVM (GraalVM):
java -XX:-UseJVMCICompiler -jar target/benchmarks.jar
This way, the Graal JIT compiler is not be used as the JVMCI compiler and the JVM uses its default one (C2).
Note that starting with Oracle JDK 23, the Oracle GraalVM JIT compiler (Graal JIT) is now included among the JITs available as part of the Oracle JDK. Read more in this blog post.
-
Switch the JVM from GraalVM (Oracle GraalVM or Community Edition) to Oracle JDK 23 or higher. You can quickly do that with using SDKMAN!:
sdk install java 23.0.1-oracle
-
Run this benchmark with the Graal JIT compiler enabled. For that, pass the
-XX:+UseGraalJIT
option tojava
:java -XX:+UnlockExperimentalVMOptions -XX:+UseGraalJIT -jar target/benchmarks.jar
To switch between JVMs in the same terminal window (without affecting the global setting), use the
sdk
tool. For example, to switch back to Oracle GraalVM for JDK 23, run:sdk use java 23.0.1-graal
.
Learn more about the Graal JIT compiler from its official documentation.