Tuesday, 2 September 2014

Performance testing Java web applications using servelet filter

By Sitansu S Swain


For performance testing in  java  web applications  1st register in web.xml

<filter><filter-name>PerformanceFilter</filter-name>
<filter-class>com.demo.PerformanceFilter</filter-class>
<</filter>
<filter-mapping>
<filter-name>PerformanceFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

Use slf4j to logging performance

package com.demo;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PerformanceFilter implements Filter {
private static final Logger LOGGER = LoggerFactory.getLogger(PerformanceFilter.class);

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
long startTime = System.currentTimeMillis();
chain.doFilter(request, response); // Process the request.
long elapsed = System.currentTimeMillis() - startTime;

String name = "Response ";
if (request instanceof HttpServletRequest) {
name = ((HttpServletRequest) request).getRequestURI();
}

LOGGER.debug("{} took {} milliseconds.", name, elapsed);
}

@Override
public void destroy() {
}

@Override
public void init(FilterConfig filterConfig) throws ServletException {
}

}



No comments:

Post a Comment