5 Measuring Time

The example MeasureTimeSample.java shows you how to measure the time of an operation by calling the Event.begin and Event.commit methods.

import jdk.jfr.Event;
import jdk.jfr.Label;
import jdk.jfr.Name;
public class MeasureTimeSample {

    @Name("com.oracle.MeasureDuration")
    @Label("Measure Duration")
    static class MeasureMyDuration extends Event { }

    public static void main(String... args) throws Exception {
        MeasureMyDuration event = new MeasureMyDuration();
        event.begin();
        Thread.sleep(42);
        event.commit();
    }
}

Note that the commit method ends the timing of an event without the need of an explicit call to the end method.

Run MeasureTimeSample with the following commands:

java -XX:StartFlightRecording:filename=mt.jfr MeasureTimeSample.java
jfr print --events MeasureDuration mt.jfr

The last command prints output similar to the following:

com.oracle.MeasureDuration {
  startTime = 12:26:43.169
  duration = 45.3 ms
  ...
}