3

I try to find a reasonable opportunity to meausure the speed of my java program/ program parts, e.g. to test which of two methods executes faster. Comment out one of both method, determine the runningtime and finally compare the times. I do not want to use in my program any codes like:

public long getTime() {
  return System.currentTimeMillis();
}

For eclipse oxygen I've tried the java profiler integrated in eclipse, called: JVM Monitor.

But I only get complex details of the cpu for e.g. and other informations, not really belonging to the execution time of my methods. Or did I overlook something.

Maybe another plugin from eclipse Marketplaces are more useful and concentrates only onto execution time?

1 Answers1

0

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.

skomisa
  • 16,436
  • 7
  • 61
  • 102
  • Thank you :). Ok I followed you instruction. Create a Maven project: File -> New -> Project. Then I've chosen Maven Project, specified my Location -> jmh-java-benchmark-archetype (As the Artifact Id, Group Id org.openjdk.jmh) . For the next step, I had to specify a Group Id and a Artficat Id: " com.Test.build.maven.simple " (for both) -> Finish. These were the steps to create my Maven Project. – neroscheni varatharajah Mar 16 '18 at 14:45
  • After that, I opened my new Project: com.Test.build.maven.simple -> Build there a jar file (MyBenchmark.jar), among them the code of your Hashmap. At least I executed my jar file from the eclipse terminal with this command : java -jar MyBenchmark.jar and got the following feedback: Invalid or corrupt jarfile MyBenchmark.jar _what went wrong, pls could you help me again_ – neroscheni varatharajah Mar 16 '18 at 14:45
  • You shouldn't be specifying the same value for groupId and artificatId. See https://maven.apache.org/guides/mini/guide-naming-conventions.html but I'm guessing you haven't used Maven before so here is a simpler alternative approach to get started. Follow the instructions given in the link of the first line of my post for "Preferred Usage: Command Line". I just did that and it takes less than a minute to create and start a project using JMH. It will run for a few minutes. Use that project as a starting point for your own development, and run it from the command line instead of within Eclipse. – skomisa Mar 16 '18 at 15:49