For Java, we will be using VisualVM to profile the code. VisualVM is a lightweight profiling tool that used to be packaged with JDK until JDK 9. Therefore, it would be better to download the latest version to benefit from the new features.
To profile your code, follow the below steps:
-
Make sure to enable the benchmark test case in BenchmarkTest.java
-
Download VisualVM
-
Start VisualVM by running the following command:
visualvm\bin\visualvm.exe --jdkhome "<path to JDK>"
-
Run your benchmark test:
- Gradle:
./gradlew clean test --tests BenchmarkTest
- Maven:
./mvnw clean test -Dtest=BenchmarkTest
- Gradle:
-
From VisualVM:
- Expand the
Local
group under theApplications
window - Right click on
org.openjdk.jmh.runner.ForkedMain
- Select
Sample
, this should open a dedicated tab dedicated to this process - Click on
CPU
button - Under the
CPU Samples
tab, you should see a thread associated to the test or main class you are running. - Expand that thread and play analyze where most of the time is consumed
- Expand the
-
Improve your code and redo steps 4 & 5