package leyan.website.util;
import org.springframework.util.StopWatch;
import java.text.NumberFormat;
public class StopWatchUtil extends StopWatch {
final NumberFormat nf = NumberFormat.getNumberInstance();
{
nf.setMaximumFractionDigits(1);
nf.setGroupingUsed(false);
}
public StopWatchUtil() {
}
public StopWatchUtil(String id) {
super(id);
}
@Override
public String shortSummary() {
return "\n" + getId() + "\t" + nf.format(getTotalTimeNanos() / 1000000) + "\t\t";
}
@Override
public String prettyPrint() {
StringBuilder sb = new StringBuilder(shortSummary());
sb.append('\n');
for (TaskInfo task : getTaskInfo()) {
sb.append(task.getTaskName()).append(" \t ");
sb.append(nf.format(task.getTimeNanos() / 1000000.0)).append(" \t ");
sb.append(nf.format((double) task.getTimeNanos() * 100 / getTotalTimeNanos())).append("% \t ");
sb.append("\n");
}
return sb.toString();
}
}
原生使用:
stopWatch.start("开始");
Thread.sleep(1000);
stopWatch.stop();
System.out.println(stopWatch.prettyPrint());