参考:
[技巧]IDEA的debug+StopWatch监测程序运行时间
添加链接描述
1创建类StopWatchExpand
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StopWatch;
import java.text.NumberFormat;
/**
* 检测程序片段运行时间拓展
*
* @author sdevil507
* created on 2021/5/27
*/
@Slf4j
public class StopWatchExpand {
/**
* StopWatch实例
*/
private static StopWatch STOP_WATCH;
/**
* StopWatch实例初始化
*/
public static void init() {
STOP_WATCH = new StopWatch("运行时间");
}
/**
* 开启计时
*
* @param taskName 任务名称
* @return 提示字符串
*/
public static String start(String taskName) {
STOP_WATCH.start(taskName);
return "[ 任务: " + taskName + " ]" + " 监测运行时间开始......";
}
/**
* 结束计时
*/
public static void stop() {
STOP_WATCH.stop();
}
/**
* 格式化的统计输出
*
* @return 统计输出
*/
public static String prettyPrint() {
// 获取运行的毫秒数与秒数
long totalTimeMillis = STOP_WATCH.getTotalTimeMillis();
double totalTimeSeconds = STOP_WATCH.getTotalTimeSeconds();
// 编写总结
String shortSummary = "StopWatch '" + STOP_WATCH.getId() + "': running time [ " + String.format("%9s", totalTimeMillis) + "ms / " + String.format("%9.3f", totalTimeSeconds) + "s ]";
StringBuilder sb = new StringBuilder();
sb.append("---------------------------------------------------------------\n");
sb.append(shortSummary);
sb.append('\n');
sb.append("---------------------------------------------------------------\n");
sb.append(" ms s % Task name\n");
sb.append("---------------------------------------------------------------\n");
NumberFormat pf = NumberFormat.getPercentInstance();
pf.setMinimumIntegerDigits(3);
pf.setGroupingUsed(false);
for (StopWatch.TaskInfo task : STOP_WATCH.getTaskInfo()) {
sb.append(String.format("%9s", task.getTimeMillis())).append(" ");
sb.append(String.format("%9.3f", task.getTimeSeconds())).append(" ");
sb.append(pf.format((double) task.getTimeMillis() / totalTimeMillis)).append(" ");
sb.append(task.getTaskName()).append("\n");
}
return sb.toString();
}
}
2在需要查看代码的时间的地方打断点并进去
(0)在最开始的地方设置init:StopWatchExpand.init();
(1)在代码开始段设置start:StopWatchExpand.start("获取basic");
(2)在代码末尾段设置
StopWatchExpand.stop(); StopWatchExpand.prettyPrint();
可以在每个需要看时长的地方加上start和stop【这两个是一对,前提是一定要init】,最后调用的prettyPrint()是第一步自定义的方法,会格式化打印