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;