在开发过程中,经常会遇到接口相应慢的情况,这个时候,就需要分析接口到底慢在哪里,需要清楚具体哪个查询慢了,然后再具体有针对性的优化。使用spirngframework 框架的工具类StopWatch ,还可以使org.apache.commons.lang3.time.StopWatch
,个人常用的是使用spirngframework 的,比较方便。
一,原来耗时统计都这样写:
@Test
public void testTime() throws InterruptedException {
long startTime = System.currentTimeMillis();
Thread.sleep(10000);
long endTime = System.currentTimeMillis();
logger.info("消耗时长是:{}", (endTime - startTime)/1000);
// 消耗时长是:10
}
使用stopWatch之后,就方便多了
@Test
public void testStopWatch() throws InterruptedException {
StopWatch stopWatch = new StopWatch("testStopWatch()方法");
stopWatch.start("first task");
Thread.sleep(100);
stopWatch.stop();
stopWatch.start("