需要記錄每個任務執行時間,或者記錄一段代碼執行時間,簡單方法是打印當前時間與執行完時間的差值,若執行大量測試很麻煩並且不直觀。若想對執行時間做進一步控制,則需要在程序中很多地方修改。spring-framework提供了一個StopWatch類可以做類似任務執行時間控制。
StopWatch sw = new StopWatch();
sw.start();
// 業務操作
sw.stop();
logger.info("耗時間:" + sw.getTotalTimeMillis());
2 配合攔截器
在filter中用spring StopWatch來統計每個請求的執行時間。在filter的doFilter中加入如下代碼:
StopWatch stopWatch = new StopWatch(url+System.currentTimeMillis());
stopWatch.start();
doFilter(arg0,arg1);
opWatch.stop();
loginfo(stopWatch.getTotalTimeMillis()+"---"+request.getRequestURI()+"執行時間");
從源代碼構造看出,StopWatch根據id構造對象,確保構造id唯一即可區分不同的請求。
public StopWatch() {
keepTaskList = true;
taskList = new LinkedList();
id = "";
}
public StopWatch(String id) {
keepTaskList = true;
taskList = new LinkedList();
this.id = id;
}
原貼地址:
http://blog.csdn.net/linfssay/article/details/7680323http://blog.csdn.net/u012186154/article/details/54923210