Spring?Boot源碼實(shí)現(xiàn)StopWatch優(yōu)雅統(tǒng)計(jì)耗時(shí)
引言
昨天,一位球友問(wèn)我能不能給他解釋一下 @SpringBootApplication 注解是什么意思,還有 Spring Boot 的運(yùn)行原理,于是我就帶著他扒拉了一下這個(gè)注解的源碼,還有 SpringApplication 類(lèi)的 run() 方法的源碼,一下子他就明白了。
你別說(shuō),看源碼的過(guò)程還真的是挺有趣,這不,我就發(fā)現(xiàn)了一個(gè)有意思的點(diǎn)。
public ConfigurableApplicationContext run(String... args) { StopWatch stopWatch = new StopWatch(); stopWatch.start(); ...... stopWatch.stop(); }
Spring Boot 是用 StopWatch 來(lái)統(tǒng)計(jì)耗時(shí)的,而通常情況下,我們會(huì)用 System.currentTimeMillis() 來(lái)統(tǒng)計(jì)耗時(shí),對(duì)吧?編程喵??開(kāi)源項(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() 沒(méi)有 Spring 提供的 StopWatch 簡(jiǎn)潔、清晰。
StopWatch使用
尤其是在多任務(wù)的情況下,StopWatch 簡(jiǎn)直好用到爆??!
// 創(chuàng)建一個(gè) StopWatch 實(shí)例 StopWatch sw = new StopWatch("沉默王二是傻 X"); // 開(kāi)始計(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 開(kāi)始計(jì)時(shí)
- 然后 stop 停止計(jì)時(shí)
- 最后通過(guò) sw.getLastTaskTimeMillis() 得出時(shí)間差
如果換成 System.currentTimeMillis() 就要了老命,先得聲明好幾個(gè) long 型的局部變量,然后要第二個(gè)減第一個(gè),第三個(gè)減第二個(gè),稍微粗心一點(diǎn)(尤其是 CV 大法)時(shí),很容易搞錯(cuò)。
除了可以通過(guò)局部時(shí)間,還可以通過(guò) 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 源碼可以得出,該類(lèi)其實(shí)就來(lái)自 Spring 的 StopWatch.java,用法也完全一致。
這說(shuō)明 hutool 的作者也認(rèn)為 Spring 的 StopWatch 寫(xiě)得好,哈哈哈??。
使用Beyond compare比較
使用 Beyond compare 比較后也能得出,兩者除了一個(gè)中文注釋?zhuān)粋€(gè)英文注釋?zhuān)a幾乎一樣。setKeepTaskList 方法有比較大的不同。
那也就是說(shuō),如果你的項(xiàng)目中沒(méi)有使用 Spring 全家桶,只用了 hutool 工具包,那就可以使用 hutool 的 StopWatch 來(lái)代替 System.currentTimeMillis()。
通過(guò)分析 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)部是通過(guò) System.nanoTime() 來(lái)計(jì)時(shí)的,本質(zhì)上和 System.currentTimeMillis() 差別并不大。
nanoTime 比 currentTimeMillis 的粒度更細(xì),前者是以納秒為單位,后者是以毫秒為單位。
注意兩者都是 native 方法,也就是說(shuō),值的粒度其實(shí)取決于底層的操作系統(tǒng)。
看到這,大家可能會(huì)恍然大悟,StopWatch 不過(guò)是披著一層外衣的 System.currentTimeMillis() 嘛?
但妙就妙在,這層外衣足夠的漂亮,足夠的優(yōu)雅。StopWatch 可以記錄每個(gè)子任務(wù)的名稱(chēng),以及按格式化打印結(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è)都是通過(guò) new 來(lái)創(chuàng)建 StopWatch 對(duì)象,commons-lang3 還可以通過(guò) createStarted(創(chuàng)建并立即啟動(dòng))、create(創(chuàng)建)來(lái)完成。
還可以調(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(); // 開(kāi)始計(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)文章!
相關(guān)文章
通過(guò)System.getProperty配置JVM系統(tǒng)屬性
這篇文章主要介紹了通過(guò)System.getProperty配置JVM系統(tǒng)屬性,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10java入門(mén)概念個(gè)人理解之package與import淺析
下面小編就為大家?guī)?lái)一篇java入門(mén)概念個(gè)人理解之package與import淺析。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-08-08深入淺出理解Java Lambda表達(dá)式之四大核心函數(shù)式的用法與范例
Lambda 表達(dá)式,也可稱(chēng)為閉包,它是推動(dòng) Java 8 發(fā)布的最重要新特性。Lambda 允許把函數(shù)作為一個(gè)方法的參數(shù)(函數(shù)作為參數(shù)傳遞進(jìn)方法中)。使用 Lambda 表達(dá)式可以使代碼變的更加簡(jiǎn)潔緊湊,今天小編帶你理解Lambda表達(dá)式之四大核心函數(shù)式的用法,感興趣的朋友快來(lái)看看吧2021-11-11ConcurrentModificationException日志關(guān)鍵字報(bào)警思考分析
本文將記錄和分析日志中的ConcurrentModificationException關(guān)鍵字報(bào)警,還有一些我的思考,有需要的朋友可以借鑒參考下,希望能夠有所幫助2023-12-12Intellij?IDEA創(chuàng)建web項(xiàng)目的超詳細(xì)步驟記錄
如果剛開(kāi)始接觸IDEA,或者之前使用的是eclipse/myEclipse的話,即使是創(chuàng)建一個(gè)JAVA WEB項(xiàng)目,估計(jì)也讓很多人費(fèi)了好幾個(gè)小時(shí),下面這篇文章主要給大家介紹了關(guān)于Intellij?IDEA創(chuàng)建web項(xiàng)目的超詳細(xì)步驟,需要的朋友可以參考下2022-08-08Java利用數(shù)組隨機(jī)抽取幸運(yùn)觀眾如何實(shí)現(xiàn)
這篇文章主要介紹了Java利用數(shù)組隨機(jī)抽取幸運(yùn)觀眾如何實(shí)現(xiàn),需要的朋友可以參考下2014-02-02springcloud gateway如何實(shí)現(xiàn)路由和負(fù)載均衡
這篇文章主要介紹了springcloud gateway如何實(shí)現(xiàn)路由和負(fù)載均衡的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07