package org.springframework.util;
import java.text.NumberFormat;
import java.util.LinkedList;
import java.util.List;
import org.springframework.lang.Nullable;
public class StopWatch {
//执行任务id
private final String id;
//是否保存开启多任务
private boolean keepTaskList = true;
//任务列表
private final List<TaskInfo> taskList = new LinkedList<>();
//记录开启时间
private long startTimeMillis;
//任务名称
@Nullable
private String currentTaskName;
//最后一个任务对象
@Nullable
private TaskInfo lastTaskInfo;
//任务数量
private int taskCount;
//总用时
private long totalTimeMillis;
public StopWatch() {
this("");
}
public StopWatch(String id) {
this.id = id;
}
public String getId() {
return this.id;
}
public void setKeepTaskList(boolean keepTaskList) {
this.keepTaskList = keepTaskList;
}
//开启秒表
public void start() throws IllegalStateException {
start("");
}
//开启秒表
public void start(String taskName) throws IllegalStateException {
if (this.currentTaskName != null) {
throw new IllegalStateException("Can't start StopWatch: it's already running");
}
this.currentTaskName = taskName;
this.startTimeMillis = System.currentTimeMillis();
}
//关闭秒表
public void stop() throws IllegalStateException {
if (this.currentTaskName == null) {
throw new IllegalStateException("Can't stop StopWatch: it's not running");
}
long lastTime = System.currentTimeMillis() - this.startTimeMillis;
this.totalTimeMillis += lastTime;
this.lastTaskInfo = new TaskInfo(this.currentTaskName, lastTime);
if (this.keepTaskList) {
this.taskList.add(this.lastTaskInfo);
}
++this.taskCount;
this.currentTaskName = null;
}
//获取秒表状态
public boolean isRunning() {
return (this.currentTaskName != null);
}
@Nullable
public String currentTaskName() {
return this.currentTaskName;
}
//获取最后任务的完成时间
public long getLastTaskTimeMillis() throws IllegalStateException {
if (this.lastTaskInfo == null) {
throw new IllegalStateException("No tasks run: can't get last task interval");
}
return this.lastTaskInfo.getTimeMillis();
}
//获取最后一个任务的名称
public String getLastTaskName() throws IllegalStateException {
if (this.lastTaskInfo == null) {
throw new IllegalStateException("No tasks run: can't get last task name");
}
return this.lastTaskInfo.getTaskName();
}
//获取最后一个任务对象
public TaskInfo getLastTaskInfo() throws IllegalStateException {
if (this.lastTaskInfo == null) {
throw new IllegalStateException("No tasks run: can't get last task info");
}
return this.lastTaskInfo;
}
//获取总用时,返回毫秒
public long getTotalTimeMillis() {
return this.totalTimeMillis;
}
//获取总用时,返回秒
public double getTotalTimeSeconds() {
return this.totalTimeMillis / 1000.0;
}
//总任务数
public int getTaskCount() {
return this.taskCount;
}
public TaskInfo[] getTaskInfo() {
if (!this.keepTaskList) {
throw new UnsupportedOperationException("Task info is not being kept!");
}
return this.taskList.toArray(new TaskInfo[0]);
}
public String shortSummary() {
return "StopWatch '" + getId() + "': running time (millis) = " + getTotalTimeMillis();
}
public String prettyPrint() {
StringBuilder sb = new StringBuilder(shortSummary());
sb.append('\n');
if (!this.keepTaskList) {
sb.append("No task info kept");
}
else {
sb.append("-----------------------------------------\n");
sb.append("ms % Task name\n");
sb.append("-----------------------------------------\n");
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMinimumIntegerDigits(5);
nf.setGroupingUsed(false);
NumberFormat pf = NumberFormat.getPercentInstance();
pf.setMinimumIntegerDigits(3);
pf.setGroupingUsed(false);
for (TaskInfo task : getTaskInfo()) {
sb.append(nf.format(task.getTimeMillis())).append(" ");
sb.append(pf.format(task.getTimeSeconds() / getTotalTimeSeconds())).append(" ");
sb.append(task.getTaskName()).append("\n");
}
}
return sb.toString();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(shortSummary());
if (this.keepTaskList) {
for (TaskInfo task : getTaskInfo()) {
sb.append("; [").append(task.getTaskName()).append("] took ").append(task.getTimeMillis());
long percent = Math.round((100.0 * task.getTimeSeconds()) / getTotalTimeSeconds());
sb.append(" = ").append(percent).append("%");
}
}
else {
sb.append("; no task info kept");
}
return sb.toString();
}
public static final class TaskInfo {
private final String taskName;
private final long timeMillis;
TaskInfo(String taskName, long timeMillis) {
this.taskName = taskName;
this.timeMillis = timeMillis;
}
public String getTaskName() {
return this.taskName;
}
public long getTimeMillis() {
return this.timeMillis;
}
public double getTimeSeconds() {
return (this.timeMillis / 1000.0);
}
}
}
常用的计时方式:
long startTime = System.currentTimeMillis();
//执行操作
long endTime = System.currentTimeMillis();
long costTime = startTime - endTime;
使用StopWatch工具类:
StopWatch stopWatch = new StopWatch();
stopWatch.start("a");
Thread.sleep(1000L);
stopWatch.stop();
stopWatch.start("b");
Thread.sleep(1000L);
stopWatch.stop();
//打印结果[running time (millis) = 2009; [a] took 1004 = 50%; [b] took 1005 = 50%]
log.info( stopWatch.toString());
更多文章:
点击跳转CSDN博客
点击跳转简书博客
公众号:代码小搬运