在filter中用spring stopWatch 來統計每個請求的執行時間;
雖然在firefox 中可以清楚的看到每個請求的執行時間,但是爲了測試,記錄日誌, 方便以後查詢維護。 還是必要的,下面進入正題:
在filter 的doFilter中加入如下代碼
StopWatch stopWatch = new StopWatch(url+System.currentTimeMillis());
stopWatch.start();
doFilter(arg0,arg1);
opWatch.stop();
loginfo(stopWatch.getTotalTimeMillis()+"---"+request.getRequestURI()+"執行時間");
對於StopWatch 從源代碼構造可以看出,是根據構造StopWatch的id 來找對象,爲了確保構造StopWatch的參數唯一就行了(防止多線程下的操作)。
public StopWatch() {
keepTaskList = true;
taskList = new LinkedList();
id = "";
}
public StopWatch(String id) {
keepTaskList = true;
taskList = new LinkedList();
this.id = id;
}
最後通過StopWatch的
stopWatch.getTotalTimeMillis()
方法得到總共的請求時間