spring stopwatch
来源:互联网 发布:十大淘宝服装模特 编辑:程序博客网 时间:2024/06/04 20:09
spring stopwatch
简单总结一句,Spring提供的计时器StopWatch对于秒、毫秒为单位方便计时的程序,尤其是单线程、顺序执行程序的时间特性的统计输出支持比较好。也就是说假如我们手里面有几个在顺序上前后执行的几个任务,而且我们比较关心几个任务分别执行的时间占用状况,希望能够形成一个不太复杂的日志输出,StopWatch提供了这样的功能。而且Spring的StopWatch基本上也就是仅仅为了这样的功能而实现。
下面是工作当中具体实现的一个实例(代码依赖其他部分,所以此处只是展现一个实际例子)
public void work() {
LOG.info("数据抓取任务");
StopWatch clock = new StopWatch();
clock.start("初始化注入数据");
duomiDataInitService.initJsonData();// 初始化注入数据
clock.stop();
clock.start("更新热门歌手");
entertainerService.updateHotSinger();// 更新热门歌手
LOG.info("更新热门歌手结束");
clock.stop();
clock.start("更新MV相关");
try {
mvService.updateMvConcert();
mvService.updateMvAppreciate();
mvService.updateMvHd();
mvService.updateMvRmd();
mvService.updateTreePosters();
} catch (JSONException e) {
e.printStackTrace();
LOG.error(e);
}
clock.stop();
clock.start("表更新消息发送");
try {
informApiService.sendMessage();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
LOG.error(e);
} catch (IOException e) {
e.printStackTrace();
LOG.error(e);
}
clock.stop();
LOG.info("数据抓取任务全部执行结束");
LOG.info(clock.prettyPrint());
double seconds = clock.getTotalTimeSeconds();
LOG.info("共耗费秒数=" + seconds);
}
工作的一个总任务性质属于抓取第三方提供的音乐相关的数据,包括热门歌手、歌手专辑、热门专辑、新曲推荐等N个任务,每个任务的逻辑复杂度不同,所以需要关心每个任务执行的时间,然后再考虑做一些优化就能够知道程序的优化效果。这里面总任务执行之前new 一个StopWatch然后将这个实例start("任务名称"),一个任务执行完毕则执行stop(),下一个任务开启前start(“下一任务名称‘)。这样最后可以调用 StopWatch.prettyPrint()方法返回一个小型的报表,输出的信息如下
-----------------------------------------
ms % Task name
-----------------------------------------
05907 001% 初始化注入数据
02322 000% 更新热门歌手
38966 004% 更新歌手专辑
09741 001% 更新热门专辑
32606 004% 更新新曲推荐
51684 006% 更新最新歌单
59119 006% 更新热门歌单
48566 005% 更新推荐歌单
475113 052% 更新所有榜单
185599 020% 更新推荐榜单
01667 000% 更新MV相关
00000 000% 表更新消息发送
>
为方便学习,下面笔者写一个可以直接执行的StopWatch执行的代码实例,实例依赖Spring的核心jar包
- import org.springframework.util.StopWatch;
- public class StopWatchDemo {
- /**
- * @param args
- * @throws InterruptedException
- */
- public static void main(String[] args) throws InterruptedException {
- // TODO Auto-generated method stub
- StopWatch clock = new StopWatch();
- clock.start("TaskOneName");
- Thread.sleep(1000 * 3);// 任务一模拟休眠3秒钟
- clock.stop();
- clock.start("TaskTwoName");
- Thread.sleep(1000 * 10);// 任务一模拟休眠10秒钟
- clock.stop();
- clock.start("TaskThreeName");
- Thread.sleep(1000 * 10);// 任务一模拟休眠10秒钟
- clock.stop();
- System.out.println(clock.prettyPrint());
- }
- }
控制台输出如下:
StopWatch '': running time (millis) = 22926
-----------------------------------------
ms % Task name
-----------------------------------------
02990 013% TaskOneName
09968 043% TaskTwoName
09968 043% TaskThreeName
可以定义自己的stopwatch
import org.springframework.util.StopWatch;public class HotMovieStopWatch extends StopWatch { private String id; public HotMovieStopWatch(String id) { this.id=id; } public String getId(){ return id; } public void startWatch(String taskName){ if(this.isRunning()){ this.stop(); } this.start(taskName); } public void stopWatch(){ if(this.isRunning()) { this.stop(); } } @Override public String toString() { StringBuilder sb = new StringBuilder(this.getId()+", all"+this.getTotalTimeMillis()); StopWatch.TaskInfo[] var2 = this.getTaskInfo(); int var3 = var2.length; for(int var4 = 0; var4 < var3; ++var4) { StopWatch.TaskInfo task = var2[var4]; sb.append(";").append(task.getTaskName()).append(" ").append(task.getTimeMillis()); long percent = Math.round(100.0D * task.getTimeSeconds() / this.getTotalTimeSeconds()); sb.append(" ").append(percent).append("%"); } return sb.toString(); }}
- spring stopwatch
- spring StopWatch 用法
- Spring中的计时器StopWatch
- Spring中的计时器StopWatch
- Spring定时器StopWatch
- Spring计时器StopWatch的使用
- Spring中StopWatch的使用
- spring StopWatch 监控请求执行时间
- Stopwatch
- Stopwatch
- Stopwatch
- StopWatch
- Stopwatch
- filter 中用spring StopWatch 监控请求执行时间
- spring StopWatch 简单便捷的计时器
- spring提供的一个简单的任务执行时间监视器 StopWatch
- C#-Stopwatch
- Stopwatch 类
- 四 :spring mvc处理方法支持如下的返回方式:ModelAndView, Model, ModelMap, Map,View, String, void 使用方法
- 深入 Docker:容器和镜像
- Docker私有仓库搭建
- (Java实现) HDOJ 2023 求平均成绩 面向对象设计思想
- Python で Curses プログラミング
- spring stopwatch
- keepalived安装实例
- 第1章 计算机系统漫游
- hdu1864 最大报销额 --01背包
- springMVC中controller的几种返回类型
- TDDL动态数据源开源-示例使用说明
- 安全问题1:
- 流形学习应用与展望
- javascript基础九(知识点类对象的引用)