Home     RSSRSS

Log the Servlet Processing Time

January 12, 2014 by kiranbadi1991 | Filed under Development, Environment, Performance Engineering.

Recently one of my friend who had a Java based web application which was was recently developed and deployed in the production environment was facing some performance issues contacted me. For some reasons in that application couple of functionality was taking more time to show up than few other functionalities, after doing some investigation, we realized that couple of servlets were taking more time than others for processing on the server side. First we identified servlets, then identified DAO associated with those servlets and then finally queries associated with that DAO.Once queries were identified we did some load testing for those queries using different data sets.

We used the below program to gather the required information about the servlet processing time,

import javax.servlet.ServletRequest;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpServletRequest;
 
@WebListener
public class logprocessingtime implements ServletRequestListener {
    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        ServletRequest servletRequest = sre.getServletRequest();
        servletRequest.setAttribute("start", System.nanoTime());
    }
 
    @Override
    public void requestDestroyed(ServletRequestEvent sre) {
        ServletRequest servletRequest = sre.getServletRequest();
        Long start = (Long) servletRequest.getAttribute("start");
        Long end = System.nanoTime();
        HttpServletRequest httpServletRequest =
                (HttpServletRequest) servletRequest;
        String uri = httpServletRequest.getRequestURI();
        System.out.println("time taken to execute servlet " + uri + ":" + ((end - start) / 1000) + "microseconds");
    }
}

The above java program is basically a web listener which we registered it to the servlet container and it listens for every request that web application receives. For every request the application receives we log the response time taken for it to process the request.

This program meets the servlet 3.0 Specs and in case if you want it to port to 2.4 , then probably you need to register the listener with web.xml.

More information about Servlet Request listener can be found here. Its powerful interface and it helps to get lot of troubleshooting information.

← Previous

Tags: , , ,

Comments are disabled