在filter中用spring stopWatch 来统计每个请求的执行时间;

虽然在firefox 中可以清楚的看到每个请求的执行时间,但是为了测试,记录日志, 方便以后查询维护。 还是必要的,下面进入正题:

在filter 的doFilter中加入如下代码

[java] view plain copy
 
  1. StopWatch stopWatch = new StopWatch(url+System.currentTimeMillis());  
  2. stopWatch.start();  
  3.  doFilter(arg0,arg1);  
  4.  opWatch.stop();  
  5.   
  6. loginfo(stopWatch.getTotalTimeMillis()+"---"+request.getRequestURI()+"执行时间");  

 

 

对于StopWatch 从源代码构造可以看出,是根据构造StopWatch的id 来找对象,为了确保构造StopWatch的参数唯一就行了(防止多线程下的操作)。

 

[java] view plain copy
 
  1. public StopWatch() {  
  2.     keepTaskList = true;  
  3.     taskList = new LinkedList();  
  4.     id = "";  
  5. }  
  6.   
  7. public StopWatch(String id) {  
  8.     keepTaskList = true;  
  9.     taskList = new LinkedList();  
  10.     this.id = id;  
  11. }  



 

最后通过StopWatch的

[java] view plain copy
 
  1. stopWatch.getTotalTimeMillis()  

方法得到总共的请求时间

相关文章:

  • 2021-05-23
  • 2021-09-03
  • 2021-07-14
  • 2021-10-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-02-03
  • 2022-12-23
  • 2021-10-13
  • 2021-12-17
  • 2022-12-23
  • 2022-02-23
  • 2021-11-22
相关资源
相似解决方案