Use filter to count the HTTP request response time of Java (J2EE) web programs

Source: Internet
Author: User

In the work related to website performance, an important aspect is monitoring and log statistics, and then analyzing the data to know when it is slow and where it is, then we can carry out targeted performance optimization;

Here, the 80/20 criterion is very suitable. Most of the situations I have seen are that about 80% of pages are okay, but about 20% of pages have poor performance;

There are many ways to monitor the processing time of HTTP requests. Here we introduce a Java (J2EE) WebProgramThe easy way to monitor/count the processing time of HTTP requests is to use filter to count the processing time of all requests;

Filter is a J2EE filter mode. It can be used to mount many different filters on a filterchain for various processing, such as security and encoding. Filters can specify the URLs corresponding to them, if the specified URL-pattern is/*, you can make statistics on all requests.

Add a filter named perffilter and add the following in Web. xml:Code

<Filter>

<Display-Name> perffilter </display-Name>

<Filter-Name> perffilter </filter-Name>

<Filter-class> perffilter </filter-class>

</Filter>

<Filter-mapping>

<Filter-Name> perffilter </filter-Name>

<URL-pattern>/* </url-pattern>

</Filter-mapping>

The following code is available in perffilter:

Public VoidDofilter (servletrequest request, servletresponse response, filterchain chain)ThrowsIoexception, servletexception {

Httpservletrequest Req = (httpservletrequest) request;

LongT1 = system.Currenttimemillis();

Chain. dofilter (request, response );

LongT2 = system.Currenttimemillis();

System.Out. Println ("*** request (" + Req. getrequesturi () + ") finished with time (MS):" + (t2-t1 ));

}

At this time, all HTTP requests, including servlet, JSP, and other frameworks, such as struts, springmvc, and JSF, can be counted by this filter, it facilitates subsequent log analysis [in practice, system is usually used. switch out to related logger], find slow requests, and then focus on this.

For example, if you access Servlet and JSP locally, the following output is displayed on the console;

* ** Request (/webtest/testservlet) finished with time (MS): 5000

* ** Request (/webtest/jsp1.jsp) finished with time (MS): 3000

If necessary, you can print the request-related parameters, because in actual work, the response time of many requests is closely related to the parameters;

The advantage of this method is that there is a separate and unified performance log. The system will add any servlet/jsp or switch framework in the future without any changes;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.