Web应用请求平均时间统计

使用Filter统计web应用平均请求时间:

使用两个Map分别存储请求时间和请求次数,当请求来临时,使用请求总时间/请求次数即可获取平均请求时间。

import java.io .IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;

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 jxl.common.Logger;

public class RequestCostFilter implements Filter {

	private Logger logger = Logger.getLogger(getClass());

	private static Map reqTimes = new ConcurrentHashMap<>();

	private static Map reqAvgTime = new ConcurrentHashMap<>();

	private static ExecutorService executorService = Executors.newFixedThreadPool(20);

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
			throws IOException, ServletException {
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		String url = httpRequest.getServletPath();
		if (checkUrl(url)) {
			Date beginTime = new Date();
			filterChain.doFilter(request, response); // 继续执行
			Date endTime = new Date();
			long i = (long) ((endTime.getTime() - beginTime.getTime()));
			stat(url,i);
		} else {
			filterChain.doFilter(request, response); // 继续执行
		}
	}

	/**
	 * 统计URL的平均请求时间
	 * @param url
	 * @param time
	 */
	private void stat(final String url,final long time){
		executorService.submit(new Runnable(){
			public void run(){
				try {
					if (reqTimes.get(url) == null) {
						reqTimes.put(url, new AtomicLong(1L));
					} else {
						reqTimes.get(url).addAndGet(1L);
					}
					if (reqAvgTime.get(url) == null) {
						reqAvgTime.put(url, new BigDecimal(time));
					} else {
						BigDecimal tt = reqAvgTime.get(url).add(new BigDecimal(time));
						reqAvgTime.put(url, tt);
					}
					BigDecimal avgtime = reqAvgTime.get(url).divide(new BigDecimal(reqTimes.get(url).get()), 2, BigDecimal.ROUND_HALF_EVEN);
					if (avgtime.longValue() > 500) {
						logger.warn(">>>>>>>>>>>|请求平均耗时:" + avgtime + "ms|请求次数:" + reqTimes.get(url).get() + "|URL:" + url + "|");
					}
				}catch (Exception e){
					logger.error("统计URL请求时间出错",e);
				}
			}
		});
	}

	/**
	 * 一级验证
	 */
	private boolean checkUrl(String url) {
		boolean isSucc = true;
		try {
			// 不拦截登录请求
			if ("//login.jsp".equals(url)) {
				return false;
			}
			int pointIndex = url.lastIndexOf(".");
			if (pointIndex > 0) {
				String relUrl = url.substring(pointIndex, url.length());
				if (".css".equals(relUrl)) {
					return false;
				}
				if (".jpg".equals(relUrl)) {
					return false;
				}
				if (".gif".equals(relUrl)) {
					return false;
				}
				if (".png".equals(relUrl)) {
					return false;
				}
				if (".ico".equals(relUrl)) {
					return false;
				}
				if (".js".equals(relUrl)) {
					return false;
				}
				if (".jsp".equals(relUrl)) {
					return false;
				}
			}
		} catch (Exception e) {
			logger.error("系统进行资源一级验证时错误!", e);
		}
		return isSucc;
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

}
在web.xml中配置filter:


	
		requestCostFilter
		org.extremecomponents.table.filter.RequestCostFilter
	
	
		requestCostFilter
		/*
	
打印日志如下:

Warning:  >>>>>>>>>>>|请求平均耗时:532.34ms|请求次数:1467|URL:/servlet/UploadServlet|
Warning:  >>>>>>>>>>>|请求平均耗时:949.64ms|请求次数:4407|URL:/leaderApproval!list.action|



你可能感兴趣的:(Web开发)