Measure execution time java

699 Views Asked by At

I need to measure how many seconds takes to execute a particular method via JFR. It is possible to do that? I tried async-profiler but I don't this feature.

1

There are 1 best solutions below

0
On

If you have access to the source code, you can create an event:

@StackTrace(false)
@Name("MethodExecution")
@Label("Method Execution")
static class MethodExecution extends jdk.jfr.Event {
}

public void foo() {
   MethodExecution event = new MethodExecution();
   event.begin();
   bar();
   event.commit();
}

Then start JFR:

$ java -XX:StartFlightRecording:filename=measurement.jfr ...

Open the file in JDK Mission Control, or use the 'jfr' utility to print the results:

$ jfr print --events MethodExecution measurement.jfr

If you don't have access to the source, you may want to look into the JMC Agent that can add similar code like above using bytecode instrumentation.