Spring計(jì)時(shí)器StopWatch使用示例
StopWatch是位于org.springframework.util包下的一個(gè)工具類(lèi),通過(guò)它可方便的對(duì)程序部分代碼進(jìn)行計(jì)時(shí)(ms級(jí)別),適用于同步單線(xiàn)程代碼塊。
正常情況下,我們?nèi)绻枰茨扯未a的執(zhí)行耗時(shí),會(huì)通過(guò)如下的方式進(jìn)行查看:
public static void main(String[] args) throws InterruptedException {
StopWatchTest.test0();
// StopWatchTest.test1();
}
public static void test0() throws InterruptedException {
long start = System.currentTimeMillis();
// do something
Thread.sleep(100);
long end = System.currentTimeMillis();
long start2 = System.currentTimeMillis();
// do something
Thread.sleep(200);
long end2 = System.currentTimeMillis();
System.out.println("某某1執(zhí)行耗時(shí):" + (end - start));
System.out.println("某某2執(zhí)行耗時(shí):" + (end2 - start2));
}
運(yùn)行結(jié)果:
某某1執(zhí)行耗時(shí):105
某某2執(zhí)行耗時(shí):203
該種方法通過(guò)獲取執(zhí)行完成時(shí)間與執(zhí)行開(kāi)始時(shí)間的差值得到程序的執(zhí)行時(shí)間,簡(jiǎn)單直接有效,但想必寫(xiě)多了也是比較煩人的,尤其是碰到不可描述的代碼時(shí),會(huì)更加的讓人忍不住多寫(xiě)幾個(gè)bug聊表敬意,而且該結(jié)果也不夠直觀,此時(shí)會(huì)想是否有一個(gè)工具類(lèi),提供了這些方法,或者自己寫(xiě)個(gè)工具類(lèi),剛好可以滿(mǎn)足這種場(chǎng)景,并且把結(jié)果更加直觀的展現(xiàn)出來(lái)。
首先我們的需求如下:
- 記錄開(kāi)始時(shí)間點(diǎn)
- 記錄結(jié)束時(shí)間點(diǎn)
- 輸出執(zhí)行時(shí)間及各個(gè)時(shí)間段的占比
根據(jù)該需求,我們可直接使用org.springframework.util包下的一個(gè)工具類(lèi)StopWatch,通過(guò)該工具類(lèi),我們對(duì)上述代碼做如下改造:
public static void main(String[] args) throws InterruptedException {
// StopWatchTest.test0();
StopWatchTest.test1();
}
public static void test1() throws InterruptedException {
StopWatch sw = new StopWatch("test");
sw.start("task1");
// do something
Thread.sleep(100);
sw.stop();
sw.start("task2");
// do something
Thread.sleep(200);
sw.stop();
System.out.println("sw.prettyPrint()~~~~~~~~~~~~~~~~~");
System.out.println(sw.prettyPrint());
}
運(yùn)行結(jié)果:
sw.prettyPrint()~~~~~~~~~~~~~~~~~
StopWatch 'test': running time (millis) = 308
-----------------------------------------
ms % Task name
-----------------------------------------
00104 034% task1
00204 066% task2
start開(kāi)始記錄,stop停止記錄,然后通過(guò)StopWatch的prettyPrint方法,可直觀的輸出代碼執(zhí)行耗時(shí),以及執(zhí)行時(shí)間百分比,瞬間感覺(jué)比之前的方式高大上了一個(gè)檔次。
除此之外,還有以下兩個(gè)方法shortSummary,getTotalTimeMillis,查看程序執(zhí)行時(shí)間。
運(yùn)行代碼及結(jié)果:
System.out.println("sw.shortSummary()~~~~~~~~~~~~~~~~~");
System.out.println(sw.shortSummary());
System.out.println("sw.getTotalTimeMillis()~~~~~~~~~~~~~~~~~");
System.out.println(sw.getTotalTimeMillis());
運(yùn)行結(jié)果
sw.shortSummary()~~~~~~~~~~~~~~~~~
StopWatch 'test': running time (millis) = 308
sw.getTotalTimeMillis()~~~~~~~~~~~~~~~~~
308
其實(shí)以上內(nèi)容在該工具類(lèi)中實(shí)現(xiàn)也極其簡(jiǎn)單,通過(guò)start與stop方法分別記錄開(kāi)始時(shí)間與結(jié)束時(shí)間,其中在記錄結(jié)束時(shí)間時(shí),會(huì)維護(hù)一個(gè)鏈表類(lèi)型的tasklist屬性,從而使該類(lèi)可記錄多個(gè)任務(wù),最后的輸出也僅僅是對(duì)之前記錄的信息做了一個(gè)統(tǒng)一的歸納輸出,從而使結(jié)果更加直觀的展示出來(lái)。
StopWatch優(yōu)缺點(diǎn):
優(yōu)點(diǎn):
- spring自帶工具類(lèi),可直接使用
- 代碼實(shí)現(xiàn)簡(jiǎn)單,使用更簡(jiǎn)單
- 統(tǒng)一歸納,展示每項(xiàng)任務(wù)耗時(shí)與占用總時(shí)間的百分比,展示結(jié)果直觀
- 性能消耗相對(duì)較小,并且最大程度的保證了start與stop之間的時(shí)間記錄的準(zhǔn)確性
- 可在start時(shí)直接指定任務(wù)名字,從而更加直觀的顯示記錄結(jié)果
缺點(diǎn):
- 一個(gè)StopWatch實(shí)例一次只能開(kāi)啟一個(gè)task,不能同時(shí)start多個(gè)task,并且在該task未stop之前不能start一個(gè)新的task,必須在該task stop之后才能開(kāi)啟新的task,若要一次開(kāi)啟多個(gè),需要new不同的StopWatch實(shí)例
- 代碼侵入式使用,需要改動(dòng)多處代碼
spring中StopWatch源碼實(shí)現(xiàn)如下:
import java.text.NumberFormat;
import java.util.LinkedList;
import java.util.List;
public class StopWatch {
private final String id;
private boolean keepTaskList = true;
private final List<TaskInfo> taskList = new LinkedList();
private long startTimeMillis;
private boolean running;
private String currentTaskName;
private StopWatch.TaskInfo lastTaskInfo;
private int taskCount;
private long totalTimeMillis;
public StopWatch() {
this.id = "";
}
public StopWatch(String id) {
this.id = id;
}
public void setKeepTaskList(boolean keepTaskList) {
this.keepTaskList = keepTaskList;
}
public void start() throws IllegalStateException {
this.start("");
}
public void start(String taskName) throws IllegalStateException {
if (this.running) {
throw new IllegalStateException("Can't start StopWatch: it's already running");
} else {
this.startTimeMillis = System.currentTimeMillis();
this.running = true;
this.currentTaskName = taskName;
}
}
public void stop() throws IllegalStateException {
if (!this.running) {
throw new IllegalStateException("Can't stop StopWatch: it's not running");
} else {
long lastTime = System.currentTimeMillis() - this.startTimeMillis;
this.totalTimeMillis += lastTime;
this.lastTaskInfo = new StopWatch.TaskInfo(this.currentTaskName, lastTime);
if (this.keepTaskList) {
this.taskList.add(this.lastTaskInfo);
}
++this.taskCount;
this.running = false;
this.currentTaskName = null;
}
}
public boolean isRunning() {
return this.running;
}
public long getLastTaskTimeMillis() throws IllegalStateException {
if (this.lastTaskInfo == null) {
throw new IllegalStateException("No tasks run: can't get last task interval");
} else {
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");
} else {
return this.lastTaskInfo.getTaskName();
}
}
public StopWatch.TaskInfo getLastTaskInfo() throws IllegalStateException {
if (this.lastTaskInfo == null) {
throw new IllegalStateException("No tasks run: can't get last task info");
} else {
return this.lastTaskInfo;
}
}
public long getTotalTimeMillis() {
return this.totalTimeMillis;
}
public double getTotalTimeSeconds() {
return (double) this.totalTimeMillis / 1000.0D;
}
public int getTaskCount() {
return this.taskCount;
}
public StopWatch.TaskInfo[] getTaskInfo() {
if (!this.keepTaskList) {
throw new UnsupportedOperationException("Task info is not being kept!");
} else {
return (StopWatch.TaskInfo[]) this.taskList.toArray(new StopWatch.TaskInfo[this.taskList.size()]);
}
}
public String shortSummary() {
return "StopWatch '" + this.id + "': running time (millis) = " + this.getTotalTimeMillis();
}
public String prettyPrint() {
StringBuilder sb = new StringBuilder(this.shortSummary());
sb.append('\n');
if (!this.keepTaskList) {
sb.append("No task info kept");
} else {
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);
StopWatch.TaskInfo[] var7;
int var6 = (var7 = this.getTaskInfo()).length;
for (int var5 = 0; var5 < var6; ++var5) {
StopWatch.TaskInfo task = var7[var5];
sb.append(nf.format(task.getTimeMillis())).append(" ");
sb.append(pf.format(task.getTimeSeconds() / this.getTotalTimeSeconds())).append(" ");
sb.append(task.getTaskName()).append("\n");
}
}
return sb.toString();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(this.shortSummary());
if (this.keepTaskList) {
StopWatch.TaskInfo[] var5;
int var4 = (var5 = this.getTaskInfo()).length;
for (int var3 = 0; var3 < var4; ++var3) {
StopWatch.TaskInfo task = var5[var3];
sb.append("; [").append(task.getTaskName()).append("] took ").append(task.getTimeMillis());
long percent = Math.round(100.0D * task.getTimeSeconds() / this.getTotalTimeSeconds());
sb.append(" = ").append(percent).append("%");
}
} else {
sb.append("; no task info kept");
}
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;
}
public String getTaskName() {
return this.taskName;
}
public long getTimeMillis() {
return this.timeMillis;
}
public double getTimeSeconds() {
return (double) this.timeMillis / 1000.0D;
}
}
}
到此這篇關(guān)于Spring計(jì)時(shí)器StopWatch使用示例的文章就介紹到這了,更多相關(guān)Spring計(jì)時(shí)器StopWatch內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java使用異或?qū)崿F(xiàn)變量互換和異或加密解密示例
這篇文章主要介紹了使用異或?qū)崿F(xiàn)變量互換和異或加密解密示例,需要的朋友可以參考下2014-02-02
詳細(xì)談?wù)凧ava中l(wèi)ong和double的原子性
原子性是指一個(gè)操作或多個(gè)操作要么全部執(zhí)行,且執(zhí)行的過(guò)程不會(huì)被任何因素打斷,要么就都不執(zhí)行,下面這篇文章主要給大家介紹了關(guān)于Java中l(wèi)ong和double原子性的相關(guān)資料,需要的朋友可以參考下2021-08-08
實(shí)現(xiàn)Java線(xiàn)程的取值并返回的方法
在本篇文章中我們給大家分享了關(guān)于Java線(xiàn)程的取值并返回的實(shí)現(xiàn)方法,對(duì)此有需要的朋友們可以跟著學(xué)習(xí)參考下。2018-10-10
Java中JUC包(java.util.concurrent)下的常用子類(lèi)
相信大家已經(jīng)對(duì)并發(fā)機(jī)制中出現(xiàn)的很多的常見(jiàn)知識(shí)點(diǎn)進(jìn)行了總結(jié),下面這篇文章主要給大家介紹了關(guān)于Java中JUC包(java.util.concurrent)下的常用子類(lèi)的相關(guān)資料,文中通過(guò)圖文以及示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
springboot+jwt+微信小程序授權(quán)登錄獲取token的方法實(shí)例
本文主要介紹了springboot+jwt+微信小程序授權(quán)登錄獲取token的方法實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03

