Ayush Aggarwal
Ayush Aggarwal

Reputation: 21

How to log performance result of Restful API call using dropwizard as a framework?

I followed the link First REST API to create a Restful API.

I can view the response on hitting the url : http://localhost:8080/hello-world?name=XXX.

Now I want to test the performance of this API using completion service and log response time for each hit in a human readable format.

How can I achieve this?

Upvotes: 1

Views: 1236

Answers (2)

pandaadb
pandaadb

Reputation: 6466

this is fairly easy to do. There are 2 methods for that:

  1. Do it yourself with metrics or logging. In your resource, you can just use the metrics created that and write out whatever you want, e.g:

     @GET
     public String hello() {
    
        Timer timer = service.timer("test");
    
        try(Context t = timer.time()) {
            return "Hello World";
        }
    
    }
    

Alternatively just measure the time and log it somewhere, e.g:

    @GET
    public String hello() {
        long currentTimeMillis = System.currentTimeMillis();
        try {
            return "Hello World";
        } finally {
            log.info("Request took: " + (System.currentTimeMillis() - currentTimeMillis) + " ms");
        }
}

The DW alternative of doing that is to use the timed annotation, like:

@GET
@Timed(name="wohoho")
public String hello() {
    return "Hello World";
}

This is equal to the manual approach (the name of the metric will be "wohoho").

Additionally you need to report on the metrics, for example like this:

ConsoleReporter.forRegistry(environment.metrics()).build().start(10, TimeUnit.SECONDS);

This will report every ten seconds, and the result looks like:

-- Timers --------------------------------------------
dw.HelloResource.wohoho
             count = 9
         mean rate = 0.13 calls/second
     1-minute rate = 0.11 calls/second
     5-minute rate = 0.03 calls/second
    15-minute rate = 0.01 calls/second
               min = 0.16 milliseconds
               max = 5.41 milliseconds
              mean = 0.90 milliseconds
            stddev = 1.43 milliseconds
            median = 0.46 milliseconds
              75% <= 0.59 milliseconds
              95% <= 5.41 milliseconds
              98% <= 5.41 milliseconds
              99% <= 5.41 milliseconds
            99.9% <= 5.41 milliseconds

And that's all you have to do.

Using metrics would be the preferred way, because you could e.g. submit it to graphite and then plot your performance over time among other things.

Hope that helps,

Artur

Upvotes: 1

Nikhil Kumar K
Nikhil Kumar K

Reputation: 1127

Usually in many application we use Graphite for metrics .

Reporting to grahite in dropwizard is shown here

http://metrics.dropwizard.io/3.1.0/manual/graphite/

Upvotes: 0

Related Questions