性能監(jiān)控代碼
package com.jd.app.server.test.service;
import org.springframework.util.StopWatch;
public class Test {
@org.junit.Test
public void test(){
StopWatch stopWatch = new StopWatch("方法性能測試");
try {
stopWatch.start("任務(wù)1");
Thread.sleep(1000);
stopWatch.stop();
stopWatch.start("任務(wù)2");
Thread.sleep(3000);
stopWatch.stop();
stopWatch.start("任務(wù)3");
Thread.sleep(500);
stopWatch.stop();
stopWatch.start("任務(wù)4");
Thread.sleep(2000);
stopWatch.stop();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (stopWatch.isRunning()) {
stopWatch.stop();
}
System.out.println(stopWatch.prettyPrint());
}
}
}
執(zhí)行結(jié)果
image.png