Spring StopWatch源码
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; } } }
来源:https://blog.csdn.net/gxs1688/article/details/87185030
Spring StopWatch源码的更多相关文章
- 别再用 System.currentTimeMillis 统计耗时了,太 Low,试试 Spring Boot 源码在用的 StopWatch吧,够优雅!
大家好,我是二哥呀! 昨天,一位球友问我能不能给他解释一下 @SpringBootApplication 注解是什么意思,还有 Spring Boot 的运行原理,于是我就带着他扒拉了一下这个注解的源 ...
- Spring Boot源码分析-启动过程
Spring Boot作为目前最流行的Java开发框架,秉承"约定优于配置"原则,大大简化了Spring MVC繁琐的XML文件配置,基本实现零配置启动项目. 本文基于Spring ...
- 曹工说Spring Boot源码(19)-- Spring 带给我们的工具利器,创建代理不用愁(ProxyFactory)
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
- Spring Boot系列(四):Spring Boot源码解析
一.自动装配原理 之前博文已经讲过,@SpringBootApplication继承了@EnableAutoConfiguration,该注解导入了AutoConfigurationImport Se ...
- 精尽Spring Boot源码分析 - SpringApplication 启动类的启动过程
该系列文章是笔者在学习 Spring Boot 过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring Boot 源码分析 GitHub 地址 进行阅读 Sprin ...
- Spring IoC源码解析——Bean的创建和初始化
Spring介绍 Spring(http://spring.io/)是一个轻量级的Java 开发框架,同时也是轻量级的IoC和AOP的容器框架,主要是针对JavaBean的生命周期进行管理的轻量级容器 ...
- spring事务源码研读1
转载摘录自:Spring事务源码分析(一)Spring事务入门 有时为了保证一些操作要么都成功,要么都失败,这就需要事务来保证. 传统的jdbc事务如下: @Test public void test ...
- Spring IoC源码解决——工具篇Eclipse
题外话 对于Spring框架,平时都是点到为止,停留在会用的程度.一直以来都想深入学习下,刚好最近看到<Spring源码深度解析>,所以想随着书本深入学习一下. 如果用Maven 如果使用 ...
- 深入Spring IOC源码之ResourceLoader
在<深入Spring IOC源码之Resource>中已经详细介绍了Spring中Resource的抽象,Resource接口有很多实现类,我们当然可以使用各自的构造函数创建符合需求的Re ...
随机推荐
- WinServer-SMTP服务
摘要 SMTP服务是用来发送邮件的,常用于代码中发送邮件,不能接收.本章介绍SMTP服务的安装,配置. 搭建F5负载均衡集群注意事项: 1.集群不能与exchang在同网段,否则发不出邮件. 2.AP ...
- Django的结构
一.Django的结构 二.静态文件的配置 STATIC_URL = '/static/' # HTML中使用的静态文件夹前缀 STATICFILES_DIRS = [ os.path.join(BA ...
- centos8 安装 mongodb 4.2 (使用yum)
1.制作 repo 文件 参考 mongodb 官方的安装文档,使用下面的脚本制作Yum库安装mongodb4.2,但安装过程提示 "Failed to synchronize cache ...
- Oracle前期工具【1】
目录: 1.oracle下载安装[Oracle11g] 2.Oracle客户端工具下载安装 [sqldeveloper.exe] 3.cmd进入Oracle命令界面操作 4.客户端图形化命令操作 or ...
- Eclipse启动tomcat超时
启动tomcat 超时 Server Tomcat v8.0 Server at localhost was unable to start within 45 seconds. If the ser ...
- Kotlin基础特性深入讲解
继续学习基础语法,在上次https://www.cnblogs.com/webor2006/p/11183077.html中定义了一个两数相加的函数,如下: 其实对于这个函数还可以进一步简写,因为函数 ...
- JS多线程之Web Worker
什么是Web Worker web worker 是运行在后台的 JavaScript,不会影响页面的性能. 当在 HTML 页面中执行脚本时,页面的状态是不可响应的,直到脚本已完成. web wor ...
- 聊聊rocketmq的sendBatchMessage
序 本文主要研究一下rocketmq的sendBatchMessage SendMessageRequestHeader rocketmq-all-4.6.0-source-release/commo ...
- centos 安装mysql5.7.18.tar.gz
1.解压mysql.tar.gz tar -zxvf mysql--linux-glibc2.-x86_64.tar.gz 2.添加mysql用户组和mysql用户,命令如下: groupadd m ...
- sql server join ,inner join ,left join ,right join 的使用
测试数据脚本 CREATE TABLE Atable ( S# INT, Sname nvarchar(32), Sage INT, Sfrom nvarchar(8) ) insert into A ...