java中StopWatch的使用详解
作者:初夏0811 发布时间:2023-12-21 03:19:04
stopWatch是org.springframework.util包下的一个工具类,使用它可直观的输出代码执行耗时,以及执行时间百分比,瞬间感觉比之前的方式高大上了一个档次。
在未使用这个工具类之前,如果我们需要统计某段代码的耗时,我们会这样写:
public static void main(String[] args) throws InterruptedException {
test0();
}
public static void test0() throws InterruptedException {
long start = System.currentTimeMillis();
// do something
Thread.sleep(100);
long end = System.currentTimeMillis();
long start2 = System.currentTimeMillis();
// do something
Thread.sleep(200);
long end2 = System.currentTimeMillis();
System.out.println("某某1执行耗时:" + (end - start));
System.out.println("某某2执行耗时:" + (end2 - start2));
}
执行结果如下:
某某1执行耗时:100
某某2执行耗时:200
可以看出我们的代码比较繁琐,如果要统计更多的任务计时呢?更繁琐,甚至让代码显得有点low
下面来看一个stopWatch实现的一个示例:
public class StopWatchDemo {
public static void main(String[] args) throws InterruptedException {
test1();
}
public static void test1() throws InterruptedException {
StopWatch sw = new StopWatch("test");
sw.start("task1");
// do something
Thread.sleep(100);
sw.stop();
sw.start("task2");
// do something
Thread.sleep(200);
sw.stop();
System.out.println("sw.prettyPrint()~~~~~~~~~~~~~~~~~");
System.out.println(sw.prettyPrint());
}
}
运行结果如下:
sw.prettyPrint()~~~~~~~~~~~~~~~~~
StopWatch 'test': running time (millis) = 310
-----------------------------------------
ms % Task name
-----------------------------------------
00110 035% task1
00200 065% task2
瞬间代码简介了,也高大上了,除此之外,还有以下两个方法shortSummary,getTotalTimeMillis,查看程序执行时间。
System.out.println("sw.shortSummary()~~~~~~~~~~~~~~~~~");
System.out.println(sw.shortSummary());
System.out.println("sw.getTotalTimeMillis()~~~~~~~~~~~~~~~~~");
System.out.println(sw.getTotalTimeMillis());
运行结果
sw.shortSummary()~~~~~~~~~~~~~~~~~
StopWatch 'test': running time (millis) = 308
sw.getTotalTimeMillis()~~~~~~~~~~~~~~~~~
308
其实以上内容在该工具类中实现也极其简单,通过start与stop方法分别记录开始时间与结束时间,其中在记录结束时间时,会维护一个链表类型的tasklist属性,从而使该类可记录多个任务,最后的输出也仅仅是对之前记录的信息做了一个统一的归纳输出,从而使结果更加直观的展示出来。
我们来看下源码:
import java.text.NumberFormat;
import java.util.LinkedList;
import java.util.List;
public class StopWatch {
private final String id;
private boolean keepTaskList = true;
private final List<TaskInfo> taskList = new LinkedList();
private long startTimeMillis;
private boolean running;
private String currentTaskName;
private StopWatch.TaskInfo lastTaskInfo;
private int taskCount;
private long totalTimeMillis;
public StopWatch() {
this.id = "";
}
public StopWatch(String id) {
this.id = id;
}
public void setKeepTaskList(boolean keepTaskList) {
this.keepTaskList = keepTaskList;
}
public void start() throws IllegalStateException {
this.start("");
}
public void start(String taskName) throws IllegalStateException {
if (this.running) {
throw new IllegalStateException("Can't start StopWatch: it's already running");
} else {
this.startTimeMillis = System.currentTimeMillis();
this.running = true;
this.currentTaskName = taskName;
}
}
public void stop() throws IllegalStateException {
if (!this.running) {
throw new IllegalStateException("Can't stop StopWatch: it's not running");
} else {
long lastTime = System.currentTimeMillis() - this.startTimeMillis;
this.totalTimeMillis += lastTime;
this.lastTaskInfo = new StopWatch.TaskInfo(this.currentTaskName, lastTime);
if (this.keepTaskList) {
this.taskList.add(this.lastTaskInfo);
}
++this.taskCount;
this.running = false;
this.currentTaskName = null;
}
}
public boolean isRunning() {
return this.running;
}
public long getLastTaskTimeMillis() throws IllegalStateException {
if (this.lastTaskInfo == null) {
throw new IllegalStateException("No tasks run: can't get last task interval");
} else {
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");
} else {
return this.lastTaskInfo.getTaskName();
}
}
public StopWatch.TaskInfo getLastTaskInfo() throws IllegalStateException {
if (this.lastTaskInfo == null) {
throw new IllegalStateException("No tasks run: can't get last task info");
} else {
return this.lastTaskInfo;
}
}
public long getTotalTimeMillis() {
return this.totalTimeMillis;
}
public double getTotalTimeSeconds() {
return (double) this.totalTimeMillis / 1000.0D;
}
public int getTaskCount() {
return this.taskCount;
}
public StopWatch.TaskInfo[] getTaskInfo() {
if (!this.keepTaskList) {
throw new UnsupportedOperationException("Task info is not being kept!");
} else {
return (StopWatch.TaskInfo[]) this.taskList.toArray(new StopWatch.TaskInfo[this.taskList.size()]);
}
}
public String shortSummary() {
return "StopWatch '" + this.id + "': running time (millis) = " + this.getTotalTimeMillis();
}
public String prettyPrint() {
StringBuilder sb = new StringBuilder(this.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);
StopWatch.TaskInfo[] var7;
int var6 = (var7 = this.getTaskInfo()).length;
for (int var5 = 0; var5 < var6; ++var5) {
StopWatch.TaskInfo task = var7[var5];
sb.append(nf.format(task.getTimeMillis())).append(" ");
sb.append(pf.format(task.getTimeSeconds() / this.getTotalTimeSeconds())).append(" ");
sb.append(task.getTaskName()).append("\n");
}
}
return sb.toString();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(this.shortSummary());
if (this.keepTaskList) {
StopWatch.TaskInfo[] var5;
int var4 = (var5 = this.getTaskInfo()).length;
for (int var3 = 0; var3 < var4; ++var3) {
StopWatch.TaskInfo task = var5[var3];
sb.append("; [").append(task.getTaskName()).append("] took ").append(task.getTimeMillis());
long percent = Math.round(100.0D * task.getTimeSeconds() / this.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 (double) this.timeMillis / 1000.0D;
}
}
}
看源码就知道为何会打印如上那么高大上了。
最后来说下StopWatch优缺点:
优点:
1.spring自带工具类,可直接使用;
2.代码实现简单,使用更简单;
3.统一归纳,展示每项任务耗时与占用总时间的百分比,展示结果直观,性能消耗相对较小,并且最大程度的保证了start与stop之间的时间记录的准确性;
4.可在start时直接指定任务名字,从而更加直观的显示记录结果。
缺点:
1.一个StopWatch实例一次只能开启一个task,不能同时start多个task,并且在该task未stop之前不能,start一个新的task,必须在该task stop之后才能开启新的task,若要一次开启多个,需要new不同的StopWatch实例;
2.代码侵入式使用,需要改动多处代码。
来源:https://blog.csdn.net/m0_37899908/article/details/125341503


猜你喜欢
- 线程的同步是保证多线程安全访问竞争资源的一种手段。线程的同步是Java多线程编程的难点,往往开发者搞不清楚什么是竞争资源、什么时候需要考虑同
- 基于项目需求,想要实现Post消息推送,故采用HttpClient组件进行实现,相关代码如下(注:程序采用的httpclient和httpc
- 在Java编程中,代码块就是指用“{}”括起来的代码。下面看一下这四种代码块。1、普通代码块就是指类中方法的方法体。public void
- 卫星菜单 ArcMenu 相信大家接触安卓,从新手到入门的过渡,就应该会了解到卫星菜单、抽屉、Xutils、Coolmenu、一些大神封装好
- mybatis count()按条件查询1、sql count()函数count()函数返回匹配指定条件的行数。sql count(colu
- Log4j组件构成 Log4j由三个重要的组件构成:1.日志信息的优先级(Logger)2.日志信息的输出目的地(Appender
- Nacos简介Nacos 英文全称为 Dynamic Naming and Configuration Service,是一个由阿里巴巴团队
- 一、默认异常处理机制默认情况下,SpringBoot 提供 /error 请求,来处理所有异常的。1.浏览器客户端,请求头里的属性是Acce
- 1、private实现封装处理如果像想要知道封装,首先必须清楚如果没有封装会怎么样?没有封装方法中的属性,在所有方法被调用后都可以进行无权限
- 游标查询(scroll)简介scroll 查询 可以用来对 Elasticsearch 有效地执行大批量的文档查询,而又不用付出深度分页那种
- 1.线程池Executors的简单使用1)创建一个线程的线程池。 Executors.newSingleThreadExecutor();
- 一、概述一个Process组件提供了在计算机运行进程的访问权限。 进程,在最简单的术语中,是正在运行的应用。提供对本地和远程进程的访问权限并
- 1、本篇内容本文让大家掌握 springmvc 中异步处理请求,特别牛逼的一个功能,大家一定要掌握。2、看段代码,分析问题@Response
- 建造者模式:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。这是建造者模式的标准表达,不过看着让人迷惑,什么叫构建
- 之前不怎么了解这个,一直以为做起来很复杂。 直到前两天公司要求要做这个功能。 做了之后才发现 这不过就是一个POST请求就能实现的东西。现在
- 本教程为大家分享了学籍管理系统的具体java代码,供大家参考,具体内容如下1.需求分析 1.1系统功能设计 (1)能够查询学生的基本信息,如
- 本文实例讲述了spring boot validation参数校验。分享给大家供大家参考,具体如下:对于任何一个应用而言在客户端做的数据有效
- 最近要做一个网站,要求实现验证码程序,经过不断调试,终于成功实现功能。一、验证码生成类生成验证码的话需要用到java的Graphics类库,
- 最近在补看《C++ Primer Plus》第六版,这的确是本好书,其中关于智能指针的章节解析的非常清晰,一解我以前的多处困惑。C++面试过
- int -> String int i=12345;String s="";核心:s=i+""