使用注解的方式实现StopWatch查看程序执行时间(高级篇)
2017-10-30 00:00
183 查看
摘要: stopwatch 查看执行时间
上篇文章说到使用原生的StopWatch查看程序执行时间,本文将介绍使用注解+AOP(面向切面编程)的方式实现其功能,一来可以快速使用StopWatch功能,二来让大家熟悉一下如何使用注解进行切面编程
本注解主要用在方法上,故target为METHOD,并设定一个value,可用于定义此次观察的名称。
创建切面类
代码解释
通过@Aspect 注解声明一个切面
通过@Component 让此切面成为Spring容器管理的Bean
通过@Around注解声明一个建言,并使用定义了自定义注解@StopWatchTime的方法作为切入点
获取注解上的属性,如果无就默认当前的方法名“ methodSignature.getName() ”
作好StopWatch的前期准备后,执行切入点的方法体
执行完成后打印执行结果
只要@StopWatchTime写在哪个方法上就能监控得出哪个方法的执行时间,但且限于public非静态方法
StopWatch 'run': running time (millis) = 2985
-----------------------------------------
ms % Task name
-----------------------------------------
02985 100% 00
</pre>
大家可能会看出来,此方法虽然使用起来方便,但只能显示单个方法的执行时间,下一篇将告诉大家如何使用AOP的方法扩展出可同时观察一个代码段里多个方法的执行情况,敬请期待。
上篇文章说到使用原生的StopWatch查看程序执行时间,本文将介绍使用注解+AOP(面向切面编程)的方式实现其功能,一来可以快速使用StopWatch功能,二来让大家熟悉一下如何使用注解进行切面编程
1. 自定义一个注解 StopWatchTime
@Retention(RetentionPolicy.RUNTIME) @Target({METHOD}) public @interface StopWatchTime { String value() default ""; }
本注解主要用在方法上,故target为METHOD,并设定一个value,可用于定义此次观察的名称。
2. 创建切面类
引用AOP支持包<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
创建切面类
@Aspect // 1 @Component // 2 public class StopWatchTimeAdvice { @Around("@annotation(stopWatchTime)") // 3 public Object invoke(ProceedingJoinPoint thisJoinPoint, StopWatchTime stopWatchTime) throws Throwable { MethodSignature methodSignature = (MethodSignature) thisJoinPoint.getSignature(); String name = StringUtils.isEmpty(stopWatchTime.value()) ? methodSignature.getName() : stopWatchTime.value(); // 4 StopWatch stopWatch = new StopWatch(name); stopWatch.start("00"); Object object = thisJoinPoint.proceed(); // 5 stopWatch.stop(); System.err.println(stopWatch.prettyPrint()); // 6 return object; } }
代码解释
通过@Aspect 注解声明一个切面
通过@Component 让此切面成为Spring容器管理的Bean
通过@Around注解声明一个建言,并使用定义了自定义注解@StopWatchTime的方法作为切入点
获取注解上的属性,如果无就默认当前的方法名“ methodSignature.getName() ”
作好StopWatch的前期准备后,执行切入点的方法体
执行完成后打印执行结果
3. 注解被观察方法
@StopWatchTime public void run(String... strings) throws Exception { step1(); step2(); step3(); } private void step1() throws InterruptedException { Thread.sleep(100L); } private void step2() throws InterruptedException { Thread.sleep(850L); } private void step3() throws InterruptedException { Thread.sleep(2000L); }
只要@StopWatchTime写在哪个方法上就能监控得出哪个方法的执行时间,但且限于public非静态方法
输出结果
<pre>StopWatch 'run': running time (millis) = 2985
-----------------------------------------
ms % Task name
-----------------------------------------
02985 100% 00
</pre>
大家可能会看出来,此方法虽然使用起来方便,但只能显示单个方法的执行时间,下一篇将告诉大家如何使用AOP的方法扩展出可同时观察一个代码段里多个方法的执行情况,敬请期待。
相关文章推荐
- 使用StopWatch查看程序执行时间(普通篇)
- C#使用StopWatch获取程序毫秒级执行时间的方法
- Windows环境是使用C语言计算程序或算法执行时间的不同粒度实现
- cygwin中使用time查看程序执行时间
- 使用System.Diagnostics.Stopwatch对程序的运行时间精确地测量
- 使用原始套接字实现时间戳程序
- 5.11 一个用Spring AOP实现异常处理和记录程序执行时间的实例
- 一个用Spring AOP实现异常处理和记录程序执行时间的实例--邵京国
- at 命令来安排命令、脚本或程序在指定的日期和时间运行。您也可以使用此命令查看现有的计划任务。
- 使用Spring的注解方式实现AOP入门
- 转“使用System.Timers.Timer类实现程序定时执行”
- 使用Spring的注解方式实现AOP
- linux 使用程序获取时间的方式
- 使用Spring的注解方式实现AOP的细节
- 【asp.net 】使用js结合hidden控件实现在后台运行一段程序后提示确认,确认通过后继续执行后台代码~~~
- linux 使用程序获取时间的方式
- ITCAST视频-Spring学习笔记(使用Spring的注解方式实现AOP的细节)
- 查看程序运行时间,gprof使用详细介绍
- ITCAST视频-Spring学习笔记(使用Spring的注解方式实现AOP入门)
- 使用Dependency Walker查看可执行程序的引用关系(Windows+VC开发)