Spring?Boot源碼實(shí)現(xiàn)StopWatch優(yōu)雅統(tǒng)計(jì)耗時(shí)
引言
昨天,一位球友問我能不能給他解釋一下 @SpringBootApplication 注解是什么意思,還有 Spring Boot 的運(yùn)行原理,于是我就帶著他扒拉了一下這個(gè)注解的源碼,還有 SpringApplication 類的 run() 方法的源碼,一下子他就明白了。
你別說,看源碼的過程還真的是挺有趣,這不,我就發(fā)現(xiàn)了一個(gè)有意思的點(diǎn)。
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
......
stopWatch.stop();
}
Spring Boot 是用 StopWatch 來統(tǒng)計(jì)耗時(shí)的,而通常情況下,我們會(huì)用 System.currentTimeMillis() 來統(tǒng)計(jì)耗時(shí),對(duì)吧?編程喵??開源項(xiàng)目里就有這樣一段代碼,在處理統(tǒng)一日志處理切面的時(shí)候。
@Around("webLog()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
long startTime = System.currentTimeMillis();
long endTime = System.currentTimeMillis();
webLog.setSpendTime((int) (endTime - startTime));
}
對(duì)比之下,我們就能發(fā)現(xiàn),JDK 提供的 System.currentTimeMillis() 沒有 Spring 提供的 StopWatch 簡(jiǎn)潔、清晰。
StopWatch使用
尤其是在多任務(wù)的情況下,StopWatch 簡(jiǎn)直好用到爆??!
// 創(chuàng)建一個(gè) StopWatch 實(shí)例
StopWatch sw = new StopWatch("沉默王二是傻 X");
// 開始計(jì)時(shí)
sw.start("任務(wù)1");
Thread.sleep(1000);
// 停止計(jì)時(shí)
sw.stop();
System.out.printf("任務(wù)1耗時(shí):%d%s.\n", sw.getLastTaskTimeMillis(), "ms");
sw.start("任務(wù)2");
Thread.sleep(1100);
sw.stop();
System.out.printf("任務(wù)2耗時(shí):%d%s.\n", sw.getLastTaskTimeMillis(), "ms");
System.out.printf("任務(wù)數(shù)量:%s,總耗時(shí):%ss.\n", sw.getTaskCount(), sw.getTotalTimeSeconds());
看到?jīng)],是不是很簡(jiǎn)單?
- 先 new 一個(gè) StopWatch 對(duì)象
- 再 start 開始計(jì)時(shí)
- 然后 stop 停止計(jì)時(shí)
- 最后通過 sw.getLastTaskTimeMillis() 得出時(shí)間差
如果換成 System.currentTimeMillis() 就要了老命,先得聲明好幾個(gè) long 型的局部變量,然后要第二個(gè)減第一個(gè),第三個(gè)減第二個(gè),稍微粗心一點(diǎn)(尤其是 CV 大法)時(shí),很容易搞錯(cuò)。
除了可以通過局部時(shí)間,還可以通過 sw.getTotalTimeSeconds() 獲取總的耗時(shí)。
任務(wù)1耗時(shí):1002ms.
任務(wù)2耗時(shí):1105ms.
任務(wù)數(shù)量:2,總耗時(shí):2.107820109s.
另外,StopWatch 還提供了一個(gè) sw.prettyPrint() 方法供打印出漂亮的格式化結(jié)果:
StopWatch '沉默王二是傻 X': running time = 2108529351 ns
---------------------------------------------
ns % Task name
---------------------------------------------
1004338467 048% 任務(wù)1
1104190884 052% 任務(wù)2
有耗時(shí),有占用百分比,還有任務(wù)名,非常清晰。
除了 Spring,hutool 工具庫(kù)和 Apache common 工具包都提供了各自的 StopWatch。

查看 hutool 工具庫(kù)中的 StopWatch 源碼可以得出,該類其實(shí)就來自 Spring 的 StopWatch.java,用法也完全一致。

這說明 hutool 的作者也認(rèn)為 Spring 的 StopWatch 寫得好,哈哈哈??。
使用Beyond compare比較
使用 Beyond compare 比較后也能得出,兩者除了一個(gè)中文注釋,一個(gè)英文注釋,代碼幾乎一樣。setKeepTaskList 方法有比較大的不同。

那也就是說,如果你的項(xiàng)目中沒有使用 Spring 全家桶,只用了 hutool 工具包,那就可以使用 hutool 的 StopWatch 來代替 System.currentTimeMillis()。
通過分析 StopWatch 的 stop 方法源碼:
public void stop() throws IllegalStateException {
if (null == this.currentTaskName) {
throw new IllegalStateException("Can't stop StopWatch: it's not running");
}
final long lastTime = System.nanoTime() - this.startTimeNanos;
this.totalTimeNanos += lastTime;
this.lastTaskInfo = new TaskInfo(this.currentTaskName, lastTime);
if (null != this.taskList) {
this.taskList.add(this.lastTaskInfo);
}
++this.taskCount;
this.currentTaskName = null;
}
其實(shí)可以發(fā)現(xiàn),StopWatch 的內(nèi)部是通過 System.nanoTime() 來計(jì)時(shí)的,本質(zhì)上和 System.currentTimeMillis() 差別并不大。
nanoTime 比 currentTimeMillis 的粒度更細(xì),前者是以納秒為單位,后者是以毫秒為單位。

注意兩者都是 native 方法,也就是說,值的粒度其實(shí)取決于底層的操作系統(tǒng)。
看到這,大家可能會(huì)恍然大悟,StopWatch 不過是披著一層外衣的 System.currentTimeMillis() 嘛?
但妙就妙在,這層外衣足夠的漂亮,足夠的優(yōu)雅。StopWatch 可以記錄每個(gè)子任務(wù)的名稱,以及按格式化打印結(jié)果,尤其是針對(duì)多任務(wù)統(tǒng)計(jì)時(shí)更友好一點(diǎn)。
當(dāng)然了,除了選擇 Spring 和 hutool 的 StopWatch,Apache commons-lang3 的 StopWatch 也是一個(gè)不錯(cuò)的可選項(xiàng),更加靈活多變。
StopWatch sw = StopWatch.createStarted();
Thread.sleep(1000);
System.out.printf("耗時(shí):%dms.\n", sw.getTime());
其他兩個(gè)都是通過 new 來創(chuàng)建 StopWatch 對(duì)象,commons-lang3 還可以通過 createStarted(創(chuàng)建并立即啟動(dòng))、create(創(chuàng)建)來完成。
還可以調(diào)用 suspend 方法暫停計(jì)時(shí)、resume 方法恢復(fù)計(jì)時(shí)、reset 重新計(jì)時(shí)。
// 暫停計(jì)時(shí)
sw.suspend();
System.out.printf("暫停耗時(shí):%dms.\n", sw.getTime());
// 恢復(fù)計(jì)時(shí)
sw.resume();
System.out.printf("恢復(fù)耗時(shí):%dms.\n", sw.getTime());
// 停止計(jì)時(shí)
sw.stop();
System.out.printf("總耗時(shí):%dms.\n", sw.getTime());
// 重置計(jì)時(shí)
sw.reset();
// 開始計(jì)時(shí)
sw.start();
System.out.printf("重置耗時(shí):%dms.\n", sw.getTime());https://github.com/itwanger/toBeBetterJavaer
以上就是Spring Boot源碼實(shí)現(xiàn)StopWatch優(yōu)雅統(tǒng)計(jì)耗時(shí)的詳細(xì)內(nèi)容,更多關(guān)于Spring Boot StopWatch統(tǒng)計(jì)耗時(shí)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- SpringBoot中統(tǒng)計(jì)方法耗時(shí)的七種實(shí)現(xiàn)方式小結(jié)
- SpringBoot統(tǒng)計(jì)接口請(qǐng)求耗時(shí)的方法詳解
- SpringBoot統(tǒng)計(jì)接口調(diào)用耗時(shí)的三種方式
- Springboot之如何統(tǒng)計(jì)代碼執(zhí)行耗時(shí)時(shí)間
- springboot基于過濾器實(shí)現(xiàn)接口請(qǐng)求耗時(shí)統(tǒng)計(jì)操作
- SpringBoot中的7種耗時(shí)統(tǒng)計(jì)的實(shí)現(xiàn)方法與應(yīng)用場(chǎng)景
相關(guān)文章
SpringBoot實(shí)現(xiàn)單文件與多文件上傳功能
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)單文件與多文件上傳功能,Spring?MVC對(duì)文件上傳做了簡(jiǎn)化,而在Spring?Boot中對(duì)此做了更進(jìn)一步的簡(jiǎn)化,文件上傳變得更為方便,下面開始演示,需要的小伙伴可以參考一下,希望對(duì)你有所幫助2022-01-01
Java實(shí)現(xiàn)將Object轉(zhuǎn)換成指定Class對(duì)象的操作代碼
這篇文章主要介紹了Java實(shí)現(xiàn)將Object轉(zhuǎn)換成指定Class對(duì)象的操作,在Java中,將Object轉(zhuǎn)換為指定類型的Class對(duì)象實(shí)際上是兩個(gè)不同概念的操作,由于你提到的“將Object轉(zhuǎn)換成指定Class對(duì)象”可能有些混淆,我將分別展示這兩種操作的示例代碼,需要的朋友可以參考下2024-09-09
Python安裝Jupyter Notebook配置使用教程詳解
這篇文章主要介紹了Python安裝Jupyter Notebook配置使用教程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
RSA加密算法java簡(jiǎn)單實(shí)現(xiàn)方法(必看)
下面小編就為大家?guī)硪黄猂SA加密算法java簡(jiǎn)單實(shí)現(xiàn)方法(必看)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09
java selenium Selenium IDE介紹及用法
本文主要介紹java selenium Selenium IDE,這里整理了相關(guān)資料和介紹如何安裝 Selenium IDE和使用方法,有需要的小伙伴可以參考下2016-08-08
java實(shí)現(xiàn)一個(gè)簡(jiǎn)單的Web服務(wù)器實(shí)例解析
這篇文章主要介紹了java實(shí)現(xiàn)一個(gè)簡(jiǎn)單的Web服務(wù)器實(shí)例解析,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02
MyBatis JdbcType 與Oracle、MySql數(shù)據(jù)類型對(duì)應(yīng)關(guān)系說明
這篇文章主要介紹了MyBatis JdbcType 與Oracle、MySql數(shù)據(jù)類型對(duì)應(yīng)關(guān)系說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
springboot內(nèi)嵌Tomcat安全漏洞修復(fù)方式
針對(duì)CVE-2020-1938漏洞,建議升級(jí)Tomcat至安全版本以避免受影響,影響版本包括:Apache Tomcat 9.x小于9.0.31、Apache Tomcat 8.x小于8.5.51、Apache Tomcat 7.x小于7.0.100及Apache Tomcat 6.x,2024-10-10

