如何優(yōu)雅的統(tǒng)計(jì)代碼耗時(shí)?
關(guān)注
一、前言
二、常規(guī)方法
2.1 時(shí)間差統(tǒng)計(jì)
public?class?TimeDiffTest?{
????public?static?void?main(String[]?args)?throws?InterruptedException?{
????????final?long?startMs?=?TimeUtils.nowMs();
????????TimeUnit.SECONDS.sleep(5);?//?模擬業(yè)務(wù)代碼
????????System.out.println("timeCost:?"?+?TimeUtils.diffMs(startMs));
????}
}
/*?output:
timeCost:?5005
*/
public?class?TimeUtils?{
????/**
?????*?@return?當(dāng)前毫秒數(shù)
?????*/
????public?static?long?nowMs()?{
????????return?System.currentTimeMillis();
????}
????/**
?????*?當(dāng)前毫秒與起始毫秒差
?????*?@param?startMillis?開(kāi)始納秒數(shù)
?????*?@return?時(shí)間差
?????*/
????public?static?long?diffMs(long?startMillis)?{
???????return?diffMs(startMillis,?nowMs());
????}
}
2.2 StopWatch
public?class?TraceWatchTest?{
????public?static?void?main(String[]?args)?throws?InterruptedException?{
????????TraceWatch?traceWatch?=?new?TraceWatch();
????????traceWatch.start("function1");
????????TimeUnit.SECONDS.sleep(1);?//?模擬業(yè)務(wù)代碼
????????traceWatch.stop();
????????traceWatch.start("function2");
????????TimeUnit.SECONDS.sleep(1);?//?模擬業(yè)務(wù)代碼
????????traceWatch.stop();
????????traceWatch.record("function1",?1);?//?直接記錄耗時(shí)
????????System.out.println(JSON.toJSONString(traceWatch.getTaskMap()));
????}
}
/*?output:
{"function2":[{"data":1000,"taskName":"function2"}],"function1":[{"data":1000,"taskName":"function1"},{"data":1,"taskName":"function1"}]}
*/
public?class?TraceWatch?{
????/**?Start?time?of?the?current?task.?*/
????private?long?startMs;
????/**?Name?of?the?current?task.?*/
????@Nullable
????private?String?currentTaskName;
????@Getter
????private?final?Map>?taskMap?=?new?HashMap<>();
????/**
?????*?開(kāi)始時(shí)間差類型指標(biāo)記錄,如果需要終止,請(qǐng)調(diào)用?{@link?#stop()}
?????*
?????*?@param?taskName?指標(biāo)名
?????*/
????public?void?start(String?taskName)?throws?IllegalStateException?{
????????if?(this.currentTaskName?!=?null)?{
????????????throw?new?IllegalStateException("Can't?start?TraceWatch:?it's?already?running");
????????}
????????this.currentTaskName?=?taskName;
????????this.startMs?=?TimeUtils.nowMs();
????}
????/**
?????*?終止時(shí)間差類型指標(biāo)記錄,調(diào)用前請(qǐng)確保已經(jīng)調(diào)用
?????*/
????public?void?stop()?throws?IllegalStateException?{
????????if?(this.currentTaskName?==?null)?{
????????????throw?new?IllegalStateException("Can't?stop?TraceWatch:?it's?not?running");
????????}
????????long?lastTime?=?TimeUtils.nowMs()?-?this.startMs;
????????TaskInfo?info?=?new?TaskInfo(this.currentTaskName,?lastTime);
????????this.taskMap.computeIfAbsent(this.currentTaskName,?e?->?new?LinkedList<>()).add(info);
????????this.currentTaskName?=?null;
????}
????/**
?????*?直接記錄指標(biāo)數(shù)據(jù),不局限于時(shí)間差類型
?????*??@param?taskName?指標(biāo)名
?????*?@param?data?指標(biāo)數(shù)據(jù)
?????*/
????public?void?record(String?taskName,?Object?data)?{
????????TaskInfo?info?=?new?TaskInfo(taskName,?data);
????????this.taskMap.computeIfAbsent(taskName,?e?->?new?LinkedList<>()).add(info);
????}
????@Getter
????@AllArgsConstructor
????public?static?final?class?TaskInfo?{
????????private?final?String?taskName;
????????private?final?Object?data;
????}
}
org.springframework.util.StopWatch?的實(shí)現(xiàn),寫了 TraceWatch 類,這個(gè)方法提供了兩種耗時(shí)統(tǒng)計(jì)的方法:Start(name)?和?Stop()?方法,進(jìn)行耗時(shí)統(tǒng)計(jì)。通過(guò)調(diào)用?Record(name, timeCost),方法,直接記錄耗時(shí)信息。這種方式本質(zhì)上和“時(shí)間差統(tǒng)計(jì)”是一致的,只是抽取了一層,稍微優(yōu)雅了一點(diǎn)。TraceWatch?內(nèi)部的數(shù)據(jù)結(jié)構(gòu),我這里簡(jiǎn)單起見(jiàn),內(nèi)部的數(shù)據(jù)結(jié)構(gòu)只是隨便舉了個(gè)例子。三、高級(jí)方法
3.1 Function
java.util.function?包,通過(guò)該類提供的接口,能夠?qū)崿F(xiàn)在指定代碼段的上下文執(zhí)行額外代碼的功能。public?class?TraceHolderTest?{
????public?static?void?main(String[]?args)?{
????????TraceWatch?traceWatch?=?new?TraceWatch();
????????TraceHolder.run(traceWatch,?"function1",?i?->?{
????????????try?{
????????????????TimeUnit.SECONDS.sleep(1);?//?模擬業(yè)務(wù)代碼
????????????}?catch?(InterruptedException?e)?{
????????????????e.printStackTrace();
????????????}
????????});
????????String?result?=?TraceHolder.run(traceWatch,?"function2",?()?->?{
????????????try?{
????????????????TimeUnit.SECONDS.sleep(1);?//?模擬業(yè)務(wù)代碼
????????????????return?"YES";
????????????}?catch?(InterruptedException?e)?{
????????????????e.printStackTrace();
????????????????return?"NO";
????????????}
????????});
????????TraceHolder.run(traceWatch,?"function1",?i?->?{
????????????try?{
????????????????TimeUnit.SECONDS.sleep(1);?//?模擬業(yè)務(wù)代碼
????????????}?catch?(InterruptedException?e)?{
????????????????e.printStackTrace();
????????????}
????????});
????????System.out.println(JSON.toJSONString(traceWatch.getTaskMap()));
????}
}
/*?output:
{"function2":[{"data":1004,"taskName":"function2"}],"function1":[{"data":1001,"taskName":"function1"},{"data":1002,"taskName":"function1"}]}
*/
public?class?TraceHolder?{
????/**
?????*?有返回值調(diào)用
?????*/
????public?static??T?run(TraceWatch?traceWatch,?String?taskName,?Supplier ?supplier) ?{
????????try?{
????????????traceWatch.start(taskName);
????????????return?supplier.get();
????????}?finally?{
????????????traceWatch.stop();
????????}
????}
????/**
?????*?無(wú)返回值調(diào)用
?????*/
????public?static?void?run(TraceWatch?traceWatch,?String?taskName,?IntConsumer?function)?{
????????try?{
????????????traceWatch.start(taskName);
????????????function.accept(0);
????????}?finally?{
????????????traceWatch.stop();
????????}
????}
}
Supplier?和?IntConsumer?接口,對(duì)外提供了有返回值和無(wú)返回值得調(diào)用,在 TraceHolder 類中,在核心代碼塊的前后,分別調(diào)用了前文的?TraceWatch?的方法,實(shí)現(xiàn)了耗時(shí)統(tǒng)計(jì)的功能。3.2 AutoCloseable
Function?的特性,我們還可以使用 jdk 1.7 的?AutoCloseable?特性。說(shuō)?AutoCloseable?可能有同學(xué)沒(méi)聽(tīng)過(guò),但是給大家展示下以下代碼,就會(huì)立刻明白是什么東西了。//?未使用?AutoCloseable
public?static?String?readFirstLingFromFile(String?path)?throws?IOException?{
????BufferedReader?br?=?null;
????try?{
????????br?=?new?BufferedReader(new?FileReader(path));
????????return?br.readLine();
????}?catch?(IOException?e)?{
????????e.printStackTrace();
????}?finally?{
????????if?(br?!=?null)?{
????????????br.close();
????????}
????}
????return?null;
}
//?使用?AutoCloseable
public?static?String?readFirstLineFromFile(String?path)?throws?IOException?{
????try?(BufferedReader?br?=?new?BufferedReader(new?FileReader(path)))?{
????????return?br.readLine();
????}
}
try?后方可以加載一個(gè)實(shí)現(xiàn)了?AutoCloseable?接口的對(duì)象,該對(duì)象作用于整個(gè)?try?語(yǔ)句塊中,并且在執(zhí)行完畢后回調(diào)?AutoCloseable#close()?方法。AutoCloseable?接口,實(shí)現(xiàn)?close()?接口:@Override
public?void?close()?{
????this.stop();
}
start()?方法,使其支持鏈?zhǔn)秸{(diào)用:public?TraceWatch?start(String?taskName)?throws?IllegalStateException?{
????if?(this.currentTaskName?!=?null)?{
????????throw?new?IllegalStateException("Can't?start?TraceWatch:?it's?already?running");
????}
????this.currentTaskName?=?taskName;
????this.startMs?=?TimeUtils.nowMs();
????return?this;
}
public?class?AutoCloseableTest?{
????public?static?void?main(String[]?args)?{
????????TraceWatch?traceWatch?=?new?TraceWatch();
????????try(TraceWatch?ignored?=?traceWatch.start("function1"))?{
????????????try?{
????????????????TimeUnit.SECONDS.sleep(1);?//?模擬業(yè)務(wù)代碼
????????????}?catch?(InterruptedException?e)?{
????????????????e.printStackTrace();
????????????}
????????}
????????try(TraceWatch?ignored?=?traceWatch.start("function2"))?{
????????????try?{
????????????????TimeUnit.SECONDS.sleep(1);?//?模擬業(yè)務(wù)代碼
????????????}?catch?(InterruptedException?e)?{
????????????????e.printStackTrace();
????????????}
????????}
????????try(TraceWatch?ignored?=?traceWatch.start("function1"))?{
????????????try?{
????????????????TimeUnit.SECONDS.sleep(1);?//?模擬業(yè)務(wù)代碼
????????????}?catch?(InterruptedException?e)?{
????????????????e.printStackTrace();
????????????}
????????}
????????System.out.println(JSON.toJSONString(traceWatch.getTaskMap()));
????}
}
/*?output:
{"function2":[{"data":1001,"taskName":"function2"}],"function1":[{"data":1002,"taskName":"function1"},{"data":1002,"taskName":"function1"}]}
*/
四、總結(jié)
時(shí)間差統(tǒng)計(jì) StopWatch Function AutoCloseable
推薦閱讀:
我用Java幾分鐘處理完30億個(gè)數(shù)據(jù)...
MySQL 去重的 3 種方法,還有誰(shuí)不會(huì)?!
內(nèi)容包含Java基礎(chǔ)、JavaWeb、MySQL性能優(yōu)化、JVM、鎖、百萬(wàn)并發(fā)、消息隊(duì)列、高性能緩存、反射、Spring全家桶原理、微服務(wù)、Zookeeper......等技術(shù)棧!
?戳閱讀原文領(lǐng)取!? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??朕已閱?

