开发者

In spring MVC, where to start and end counter to test for speed execution time?

I want to know how long it takes a particular page to render, where are the various parts in the Spring MVC framework that I can start and stop my timer?

BTW, what java class should I use to get t开发者_开发百科he most accurate time in milliseconds?


  • Create a javax.servlet.Filter and map it in web.xml above all other filters. In the doFilter(..) place your coutner code around chain.doFilter(request, response)

  • Either use System.currentTimeMillis, or System.nanoTime() (more accurate), or perf4j.

This will show you how much time did it take for the server to generate the response. If you want to see the full loading time of the page as the client sees it, install FireBug on firefox.


A note on how a Filter operates. A request comes in, and is passed as argument to the first filter in the chain. Now the filter may decide to proceed the execution, or to stop it. If it choses to proceed, it invokes .chain.doFilter(..). Now the same happens with all defined filters for that resource, and ultimately the flow reaches the target Servlet (in this case - the DispatcherServlet). When the doGet(..) / doPost(..) method completes, the program flow, logically, returns to the caller - i.e. the last filter in the chain. When it completes - the program flow returns to the one above, until they reach the first filter which has called chain.doFilter(..). That is the line where you put your timing calculations. When the first filter returns, the flow goes to the servlet container internals, and soon the response is sent to the browser. So, your code will be:

public void doFilter(ServletRequest request,
   ServletResponse response, FilterChain chain) {

    long start = System.nanoTime();
    chain.doFilter(request, response);
    long end = System.nanoTime();
    // log end-start
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜