|
| 1 | +# Java Stream Benchmark |
| 2 | + |
| 3 | +This repository contains the code for a simple Java Stream benchmark designed to run on the [GraalVM JDK](http://graalvm.org) and the [Oracle JDK](https://www.oracle.com/java/technologies/downloads/). |
| 4 | +## Preparation |
| 5 | + |
| 6 | +1. Download and install the GraalVM JDK using [SDKMAN!](https://sdkman.io/). For other installation options, visit the [Downloads page](https://www.graalvm.org/downloads/). |
| 7 | + ```bash |
| 8 | + sdk install java 23.0.1-graal |
| 9 | + ``` |
| 10 | + |
| 11 | +2. Download or clone the repository and navigate into the benchmark directory: |
| 12 | + ```bash |
| 13 | + git clone https://github.com/graalvm/graalvm-demos |
| 14 | + ``` |
| 15 | + ```bash |
| 16 | + cd graalvm-demos/compiler/java-stream-benchmark |
| 17 | + ``` |
| 18 | + |
| 19 | +## Build the Benchmark |
| 20 | + |
| 21 | +Build the benchmark with Maven: |
| 22 | +```bash |
| 23 | +./mvnw package |
| 24 | +``` |
| 25 | +Now you can run this benchmark with whatever `java` you have on your machine and compare the results between JVMs. |
| 26 | + |
| 27 | +> **A note about the results**: The benchmark mode is the `AverageTime` in nanoseconds per operation, which means lower numbers are better. |
| 28 | +Note that the results you see can be influenced by the hardware you are running this benchmark on, the CPU load, and other factors. |
| 29 | +Interpret them responsibly. |
| 30 | + |
| 31 | +### GraalVM JDK with Graal JIT |
| 32 | + |
| 33 | +Run the benchmark with the default GraalVM JIT compiler by executing the _target/benchmarks.jar_ file: |
| 34 | +```bash |
| 35 | +java -jar target/benchmarks.jar |
| 36 | +``` |
| 37 | + |
| 38 | +### GraalVM JDK with Graal JIT Deactivated |
| 39 | + |
| 40 | +Now deactivate the Graal compiler and run the benchmark on the same JVM (GraalVM): |
| 41 | +```bash |
| 42 | +java -XX:-UseJVMCICompiler -jar target/benchmarks.jar |
| 43 | +``` |
| 44 | +This way, the Graal JIT compiler is not be used as the JVMCI compiler and the JVM uses its default one (C2). |
| 45 | + |
| 46 | +### Oracle JDK with Graal JIT |
| 47 | + |
| 48 | +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**. |
| 49 | +Read more in [this blog post](https://blogs.oracle.com/java/post/including-the-graal-jit-in-oracle-jdk-23). |
| 50 | + |
| 51 | +1. Switch the JVM from GraalVM (Oracle GraalVM or Community Edition) to Oracle JDK 23 or higher. You can quickly do that with using [SDKMAN!](https://sdkman.io/): |
| 52 | + ```bash |
| 53 | + sdk install java 23.0.1-oracle |
| 54 | + ``` |
| 55 | + |
| 56 | +2. Run this benchmark with the Graal JIT compiler enabled. For that, pass the `-XX:+UseGraalJIT` option to `java`: |
| 57 | + ```bash |
| 58 | + java -XX:+UnlockExperimentalVMOptions -XX:+UseGraalJIT -jar target/benchmarks.jar |
| 59 | + ``` |
| 60 | + |
| 61 | +> 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`. |
| 62 | + |
| 63 | +Learn more about the Graal JIT compiler from [its official documentation](https://www.graalvm.org/reference-manual/java/compiler/). |
0 commit comments