我們經常需要統計一個方法的耗時,一般我們會這樣做: public class Test { public static void main(String[] args) throws InterruptedException { long start = System.currentTimeMill ...
我們經常需要統計一個方法的耗時,一般我們會這樣做:
public class Test {
public static void main(String[] args) throws InterruptedException {
long start = System.currentTimeMillis();
// 睡眠100ms,模擬任務耗時
Thread.sleep(100L);
long end = System.currentTimeMillis();
System.out.println("耗時:" + (end - start)); // 輸出 耗時:104
}
}
這樣做雖然簡單直接,當我們需要分段統計耗時的時候,一不留神就找不到開始時間在哪了。而且也不能彙總結果,不能直觀的看到每一步的耗時。
有一個簡單的小工具,可以幫助我們非常容易的統計方法耗時。
名字叫StopWatch,在常見的類庫,例如Spring、Apache Commons、Google Guava都有這個工具,功能和實現都是大同小異,推薦使用Spring里的,可以更方便統計耗時的彙總結果。
把耗時代碼換成StopWatch試一下:
public class Test {
public static void main(String[] args) throws InterruptedException {
// 創建統計任務
StopWatch stopWatch = new StopWatch();
// 開始計時
stopWatch.start();
// 睡眠100ms,模擬任務耗時
Thread.sleep(100L);
// 結束計時
stopWatch.stop();
// 列印統計結果
System.out.println(stopWatch.prettyPrint());
}
}
輸出結果是:
StopWatch '': running time (millis) = 104
-----------------------------------------
ms % Task name
-----------------------------------------
00104 100%
只統計了一個任務的耗時,還不能展示出StopWatch優勢,下麵一次統計多個任務的耗時:
public class CopyTest {
public static void main(String[] args) {
// 創建統計任務,並指定ID
StopWatch stopWatch = new StopWatch("Main方法耗時");
IntStream.rangeClosed(1, 10).forEach(index -> {
// 開始計時,並指定任務名稱,便於展示
stopWatch.start("task" + index);
// 睡眠100ms,模擬任務耗時
try {
Thread.sleep(100L);
} catch (InterruptedException e) {
}
// 結束計時
stopWatch.stop();
});
// 列印統計結果
System.out.println(stopWatch.prettyPrint());
}
}
輸出結果是:
StopWatch 'Main方法耗時': running time (millis) = 1039
-----------------------------------------
ms % Task name
-----------------------------------------
00103 010% task1
00107 010% task2
00104 010% task3
00104 010% task4
00105 010% task5
00102 010% task6
00104 010% task7
00102 010% task8
00105 010% task9
00103 010% task10
列印出了每個任務的耗時,並統計了在總耗時所占百分比,已經非常直觀了。
這是怎麼實現的呢?其實源碼非常簡單,就用一個LinkedList記錄了每個任務的耗時:
public class StopWatch {
// 總任務ID
private final String id;
// 任務集合
private final List<TaskInfo> taskList = new LinkedList<TaskInfo>();
private long startTimeMillis;
private boolean running;
private String currentTaskName;
private TaskInfo lastTaskInfo;
private long totalTimeMillis;
public StopWatch() {
this("");
}
public StopWatch(String id) {
this.id = id;
}
public void start() throws IllegalStateException {
start("");
}
// 開始計時,並記錄開始時間
public void start(String taskName) throws IllegalStateException {
if (this.running) {
throw new IllegalStateException("Can't start StopWatch: it's already running");
}
this.running = true;
this.currentTaskName = taskName;
this.startTimeMillis = System.currentTimeMillis();
}
// 結束計時,統計耗時,並添加到taskList裡面
public void stop() throws IllegalStateException {
if (!this.running) {
throw new IllegalStateException("Can't stop StopWatch: it's not running");
}
long lastTime = System.currentTimeMillis() - this.startTimeMillis;
this.totalTimeMillis += lastTime;
this.lastTaskInfo = new TaskInfo(this.currentTaskName, lastTime);
this.taskList.add(lastTaskInfo);
this.running = false;
this.currentTaskName = null;
}
public long getLastTaskTimeMillis() throws IllegalStateException {
if (this.lastTaskInfo == null) {
throw new IllegalStateException("No tasks run: can't get last task interval");
}
return this.lastTaskInfo.getTimeMillis();
}
public String getLastTaskName() throws IllegalStateException {
if (this.lastTaskInfo == null) {
throw new IllegalStateException("No tasks run: can't get last task name");
}
return this.lastTaskInfo.getTaskName();
}
public double getTotalTimeSeconds() {
return this.totalTimeMillis / 1000.0;
}
public TaskInfo[] getTaskInfo() {
return this.taskList.toArray(new TaskInfo[this.taskList.size()]);
}
public String shortSummary() {
return "StopWatch '" + getId() + "': running time (millis) = " + getTotalTimeMillis();
}
// 列印統計結果
public String prettyPrint() {
StringBuilder sb = new StringBuilder(shortSummary());
sb.append('\n');
sb.append("-----------------------------------------\n");
sb.append("ms % Task name\n");
sb.append("-----------------------------------------\n");
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMinimumIntegerDigits(5);
nf.setGroupingUsed(false);
NumberFormat pf = NumberFormat.getPercentInstance();
pf.setMinimumIntegerDigits(3);
pf.setGroupingUsed(false);
for (TaskInfo task : getTaskInfo()) {
sb.append(nf.format(task.getTimeMillis())).append(" ");
sb.append(pf.format(task.getTimeSeconds() / getTotalTimeSeconds())).append(" ");
sb.append(task.getTaskName()).append("\n");
}
return sb.toString();
}
public static final class TaskInfo {
private final String taskName;
private final long timeMillis;
TaskInfo(String taskName, long timeMillis) {
this.taskName = taskName;
this.timeMillis = timeMillis;
}
}
}
感覺怎麼樣?趕緊使用起來吧。