package com.ahut.common.utils;

import org.junit.Test;
import org.springframework.util.StopWatch;

/**
* desc : 测试StopWatch
* create_user : cheng
* create_date : 2019/5/22 9:57
*/
public class StopWatchTest {

/**
* desc :
* create_user : cheng
* create_date : 2019/5/22 9:58
*/
@Test
public void testStopWatch() throws Exception{
StopWatch stopWatch = new StopWatch("统计代码耗时操作");

stopWatch.start("第一个任务");
Thread.sleep(1003);
stopWatch.stop();

stopWatch.start("第二个任务");
Thread.sleep(2400);
stopWatch.stop();

System.out.println(stopWatch.prettyPrint());

for (StopWatch.TaskInfo taskInfo : stopWatch.getTaskInfo()) {
System.out.println(taskInfo.getTaskName() + ":" + taskInfo.getTimeMillis() + ":" + taskInfo.getTimeSeconds());
}
}

}

结果:

StopWatch '统计代码耗时操作': running time (millis) = 3404
-----------------------------------------
ms % Task name
-----------------------------------------
01004 029% 第一个任务
02400 071% 第二个任务

第一个任务:1004:1.004
第二个任务:2400:2.4