StopWatch记录多个方法执行时间
1. StopWatch使用
springframework.util.StopWatch 提供的计时器可以满足一次性记录多个方法或业务的执行时间,最后一次性输出各个业务的执行时间。
1.1. StopWatch记录多个业务执行时间实例
import org.springframework.util.StopWatch;
public class Test {
public static void main(String[] args) throws InterruptedException {
swatchTest();
}
private static void swatchTest() throws InterruptedException {
StopWatch sw = new StopWatch();
//业务一
sw.start("创建订单");
Thread.sleep(1000);
sw.stop();
//业务二
sw.start("查看订单");
Thread.sleep(2000);
sw.stop();
//查看所有业务的耗时统计情况
String result = sw.prettyPrint();
System.out.println(result);
}
}
1.2. 多个业务执行时间输出结果
StopWatch '': running time (millis) = 3001
-----------------------------------------
ms % Task name
-----------------------------------------
01000 033% 创建订单
02001 067% 查看订单