# MicroProfile: @Metered vs. @Timed

MicroProfile Metrics comes with @Metered:


@Path("metered")
public class MeteredResource {

    @GET
    @Metered(name = "ping_metered")
    public String ping() {
        return "metered " + System.currentTimeMillis();
    }    
}



and @Timed:



import javax.ws.rs.GET;
import javax.ws.rs.Path;
import org.eclipse.microprofile.metrics.annotation.Timed;

@Path("timed")
public class TimedResource {

    @GET
    @Timed(name = "ping_timed")
    public String ping() {
        return "timed " + System.currentTimeMillis();
    }    
}


annotations.

According to the microprofile specification, the @Metered annotation:

    "Denotes a meter, which tracks the frequency of invocations of the annotated object."

with default unit: "per second"


In contrary, the @Timed annotation:

    "Denotes a timer, which tracks duration of the annotated object."

with default unit: nanoseconds.


The output after a brief stress test:


{
    "com.airhacks.metrics.boundary.MeteredResource.ping_metered": {
        "count": 10,
        "fiveMinRate": 2.0,
        "oneMinRate": 2.0,
        "fifteenMinRate": 2.0,
        "meanRate": 1.1044614253525735
    },
    "com.airhacks.metrics.boundary.TimedResource.ping_timed": {
        "fiveMinRate": 1.9355159413581935,
        "max": 202722,
        "count": 20,
        "p50": 7658.0,
        "p95": 202722.0,
        "p98": 202722.0,
        "p75": 8228.0,
        "p99": 202722.0,
        "min": 5852,
        "fifteenMinRate": 1.9780232116334415,
        "meanRate": 0.9988264886468711,
        "mean": 23041.21494920979,
        "p999": 202722.0,
        "oneMinRate": 1.7175127368841634,
        "stddev": 47095.42163982647
    }
}    


The @Timed is useful to track the time spent in methods. Particularly the max attribute is useful to track possible performance problems.


On the other hand, the @Metered annotation exposes frequency / throughput, which can be directly used in stress tests / torture tests as thresholds.


See you at Web, MicroProfile and Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.
