JMH (Java Microbenchmark Harness) from OpenJDK should meet your needs:
JMH is a Java harness for building, running, and analysing
nano/micro/milli/macro benchmarks written in Java and other languages
targetting the JVM.
The recommended way to run a JMH benchmark is to use Maven to setup a
standalone project that depends on the jar files of your application.
This approach is preferred to ensure that the benchmarks are correctly
initialized and produce reliable results. It is possible to run
benchmarks from within an existing project, and even from within an
IDE, however setup is more complex and the results are less reliable.
Here's a simple approach to benchmarking with JMH:
- Create a Maven project using the archetype jmh-java-benchmark-archetype
- Edit that project in Eclipse to create the code you want to benchmark, and build a jar file.
- Execute your jar file from the terminal/Command Prompt. JMH will do its thing, and display its results.
To give you a better idea, here's some trivial annotated code to load data into a HashMap:
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
public class MyBenchmark {
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void testMethod() {
HashMap<Integer, String> map = new HashMap<>();
for (int i = 0; i < 1000; i++) {
map.put(i, "" + i);
}
}
}
That was literally all the code needed to use JMH, and the class and method were already created by Maven; I just provided the annotations and four lines of Java.
Of course there's far more to it than that to getting useful information out of JMH, but you should be able to get something up and running in about 15 minutes if you follow one of the many on-line tutorials. Openjdk also provide 38 code samples.