Spring boot 自带的耗时统计-StopWatch

1,035 阅读1分钟

最近在做性能测试,之前一直用的是System.currentTimeMillis去统计耗时,但是无意间看到网友发的一篇文章说springboot自带的统计耗时工具,当时就测试了一下。感觉特别舒服,而且特别简洁

之前统计:

public void A(){
    long startTime = System.currentTimeMillis();
    ......
    long endTime = System.currentTimeMillis();
    Syetem.out.println(endTime - startTime);
}

springboot自带的的:
public void A(){
    StopWatch stopWatch = new StopWatch(); 
    stopWatch.start(); 
    ...... 
    stopWatch.stop();
    System.out.printf("耗时:%d%s.\n", stopWatch.getLastTaskTimeMillis(), "ms");
}