java btrace

这篇文章是关于BTrace的 ,我正在考虑将其作为Java开发人员的隐藏宝藏。 BTrace是用于Java平台的安全,动态跟踪工具。 BTrace可用于动态跟踪正在运行的Java程序(类似于DTrace,适用于OpenSolaris应用程序和OS)。

很快,该工具允许注入跟踪点,而无需在运行时重新启动或重新配置Java应用程序。 而且,尽管有几种方法可以做到这一点,但我今天要讨论的是使用标准JDK捆绑包中的JVisualVM工具。

太酷了, BTrace本身使用Java语言来定义注入跟踪点。 如果您曾经进行过面向方面的编程(AOP),则该方法看起来非常熟悉。

因此,让我们开始一个问题:我们有一个使用NoSQL数据库之一(例如,让它成为MongoDB)的应用程序,突然开始出现明显的性能下降。 开发人员怀疑应用程序运行过多的查询或更新,但不能自信地说。 BTrace在这里可以提供帮助。

首先,让我们运行JVisualVM并安装BTrace插件:

JVisualVM应该重新启动以使插件出现。 现在,当我们的应用程序启动并运行时,让我们在JVisualVM应用程序树中右键单击它:

将出现以下非常直观的BTrace编辑器(带有简单的工具栏):

在这里可以定义跟踪工具并将其动态注入正在运行的应用程序中。 BTrace有一个非常丰富的模型来定义应该精确跟踪的内容:方法,构造函数,方法返回,错误等。 它还支持开箱即用的聚合,因此在应用程序运行时很容易收集大量指标。 对于我们的问题,我们想查看与MongoDB相关的哪些方法正在执行。

当我的应用程序使用Spring Data MongoDB时 ,我对应用程序正在调用org.springframework.data.mongodb.core.MongoOperations接口的任何实现的方法以及每次调用花费的时间感兴趣。 所以我定义了一个非常简单的BTrace脚本:

import com.sun.btrace.*;
import com.sun.btrace.annotations.*;
import static com.sun.btrace.BTraceUtils.*;@BTrace
public class TracingScript {@TLS private static String method;@OnMethod(clazz = '+org.springframework.data.mongodb.core.MongoOperations', method = '/.*/')public static void onMongo( @ProbeClassName String className, @ProbeMethodName String probeMethod, AnyType[] args ) {method = strcat( strcat( className, '::' ), probeMethod );}@OnMethod(clazz = '+org.springframework.data.mongodb.core.MongoOperations', method = '/.*/', location = @Location( Kind.RETURN ) )public static void onMongoReturn( @Duration long duration ) {println( strcat( strcat( strcat( strcat( 'Method ', method ), ' executed in ' ), str( duration / 1000 ) ), 'ms' ) );}
}

让我简要地解释一下我在做什么。 基本上,我想知道何时调用org.springframework.data.mongodb.core.MongoOperations的任何实现的任何方法( onMongo标记)和调用持续时间( onMongoReturn依次标记)。 线程局部变量方法保存完整的合格方法名称(带有类),而由于使用了有用的BTrace预定义注释, duration参数保存了方法执行时间(以纳秒为单位)。 尽管它是纯Java,但BTrace仅允许使用Java类的一小部分。 这不是问题,因为com.sun.btrace.BTraceUtils类提供了许多有用的方法(fe, strcat )来填补空白。 运行此脚本将产生以下输出:

** Compiling the BTrace script ...
*** Compiled
** Instrumenting 1 classes ...
Method org.springframework.data.mongodb.core.MongoTemplate::maybeEmitEvent executed in 25ms
Method org.springframework.data.mongodb.core.MongoTemplate::maybeEmitEvent executed in 3ms
Method org.springframework.data.mongodb.core.MongoTemplate::getDb executed in 22ms
Method org.springframework.data.mongodb.core.MongoTemplate::prepareCollection executed in 2ms
Method org.springframework.data.mongodb.core.MongoTemplate::prepareCollection executed in 19ms
Method org.springframework.data.mongodb.core.MongoTemplate::access$100 executed in 2ms
Method org.springframework.data.mongodb.core.MongoTemplate::access$100 executed in 1ms
Method org.springframework.data.mongodb.core.MongoTemplate::maybeEmitEvent executed in 3ms
Method org.springframework.data.mongodb.core.MongoTemplate::maybeEmitEvent executed in 2ms
Method org.springframework.data.mongodb.core.MongoTemplate::getDb executed in 2ms
Method org.springframework.data.mongodb.core.MongoTemplate::prepareCollection executed in 1ms
Method org.springframework.data.mongodb.core.MongoTemplate::prepareCollection executed in 6ms
Method org.springframework.data.mongodb.core.MongoTemplate::access$100 executed in 1ms
Method org.springframework.data.mongodb.core.MongoTemplate::access$100 executed in 0ms
Method org.springframework.data.mongodb.core.MongoTemplate::maybeEmitEvent executed in 2ms
Method org.springframework.data.mongodb.core.MongoTemplate::maybeEmitEvent executed in 1ms
Method org.springframework.data.mongodb.core.MongoTemplate::getDb executed in 2ms
Method org.springframework.data.mongodb.core.MongoTemplate::prepareCollection executed in 1ms
Method org.springframework.data.mongodb.core.MongoTemplate::prepareCollection executed in 6ms
Method org.springframework.data.mongodb.core.MongoTemplate::access$100 executed in 1ms
Method org.springframework.data.mongodb.core.MongoTemplate::access$100 executed in 0ms
Method org.springframework.data.mongodb.core.MongoTemplate::maybeEmitEvent executed in 2ms
Method org.springframework.data.mongodb.core.MongoTemplate::maybeEmitEvent executed in 1ms
...

如您所见,输出包含一堆内部类,可以通过提供更精确的方法名称模板(甚至可以跟踪MongoDB驱动程序)来消除它们。

我刚刚开始发现BTrace,但是使用该功能强大的工具对开发人员来说, 无疑是很有价值的。

参考: BTrace:Java开发人员工具箱中的隐藏宝石,来自我们的JCG合作伙伴 Andrey Redko在Andriy Redko {devmind}博客上。

翻译自: https://www.javacodegeeks.com/2012/08/btrace-hidden-gem-in-java-developer.html

java btrace

java btrace_BTrace:Java开发人员工具箱中的隐藏宝石相关推荐

  1. BTrace:Java开发人员工具箱中的隐藏宝石

    这篇文章是关于BTrace的 ,我正在考虑将其作为Java开发人员的隐藏宝藏. BTrace是用于Java平台的安全,动态跟踪工具. BTrace可用于动态跟踪正在运行的Java程序(类似于DTrac ...

  2. think in java interview-高级开发人员面试宝典(二)

    think in java interview-高级开发人员面试宝典(二) 分类: 面经2013-08-05 00:4318634人阅读评论(58)收藏举报 目录(?)[+] 从现在开始,以样题的方式 ...

  3. Java 8:开发人员怎么看?

    由于Java 8发行倒计时已经开始,因此Java开发人员似乎肯定已经准备好参与其中. 根据Typesafe的一项调查 ,参与其中的开发人员中有65%回答说他们将在24个月的计划中迁移到Java 8,而 ...

  4. stackoverflow_StackOverflow帐户如何确保您在公认的开发人员表格中占有一席之地

    stackoverflow by Melchor Tatlonghari 由Melchor Tatlonghari StackOverflow帐户如何确保您在公认的开发人员表格中占有一席之地 (How ...

  5. 有趣的java 开发_开发人员历史中的五个有趣时刻

    有趣的java 开发 我在整个开发环境中工作了30多年. 我开始共享一个实际上有门的办公室. 是的,那是很久以前了. 我从办公室到立方体再到开放空间. 我曾在大型团队,小型团队和许多中型团队中工作. ...

  6. [置顶] think in java interview-高级开发人员面试宝典(二)

    从现在开始,以样题的方式一一列出各种面试题以及点评,考虑到我在前文中说的,对于一些大型的外资型公司,你将会面临全程英语面试,因此我在文章中也会出现许多全英语样题. 这些题目来自于各个真实的公司,公司名 ...

  7. java三件套_Java开发人员应该知道的三件事

    java三件套 对于那些长期关注JavaOne 2012会议的读者来说,这是一篇有趣的文章. 我最近对Java冠军Heinz Kabutz的采访引起了我的注意: 包括他的Java内存难题程序,从Jav ...

  8. javaone_JavaOne 2012:101种改进Java的方法-开发人员参与为何如此重要

    javaone Bruno Souza , Martijn Verburg和Heather Vancura在希尔顿酒店的美国大陆宴会厅4中展示了" 101种改善Java的方法:开发人员为何如 ...

  9. JavaOne 2012:101种改进Java的方法-开发人员参与为何如此重要

    Bruno Souza , Martijn Verburg和Heather Vancura在希尔顿酒店的大陆宴会厅4中展示了" 101种改进Java的方法:开发人员参与为何如此重要" ...

最新文章

  1. mysql进程SHOW PROCESSLIST详解Command命令状态
  2. mysql using filesort_Mysql执行计划中的Using filesort
  3. 交叉驰豫的影响因素_什么因素影响了软管总成的质量?
  4. python二重积分0到正无穷_python函数的数值二重积分
  5. LibLinear(SVM包)使用说明之(三)实践
  6. 类似色/同色系电商海报模板,PSD分层模板
  7. asp判断ajax请求 -asp.net,一个asp注册验证用户名是否重复的Ajax实例
  8. Java反射初探 ——“当类也学会照镜子”
  9. Flutter实战之FlutterPlugin插件入门指南
  10. exfat最佳单元大小_回音壁构造和单元相关的个人看法
  11. cad怎么导出jpg图片格式?
  12. Ubuntu系统安装ghostscript seq2logo
  13. 苍南县勤奋高中计算机,浙江省苍南县勤奋高级中学高中语文公开课教案 为政以德.doc.doc...
  14. 读唐巧博客2011年总结感想
  15. 苹果或将采用高通屏下指纹方案,5GiPhone基带由三星、高通共同提供...
  16. html文件如何设置为桌面壁纸,怎样把文件里的图片设置为桌面背景时全部是全屏图?最好详细一点的。...
  17. AMS1084电路图
  18. 全栈创新加速“算力网络”时代来临:英特尔携手中国移动共谱数智华章
  19. Babylon.js 踩坑之正交摄像机,平行投影的相关设置
  20. 单链表的简单实现(C语言)

热门文章

  1. java之String
  2. 别在被骗了!!!!!!
  3. mybatis简单案例源码详细【注释全面】——Dao层映射文件(UserMapper.xml)【重要】
  4. 263. 丑数---LeetCode---JAVA
  5. MyBatis_1 简介
  6. PHP WEB程序设计信息表,PHP WEB程序设计
  7. pg和oracle比较,Oracle与PostgreSQL使用差异对比与总结
  8. java 刷新界面_利用java如何实现在删除信息后刷新页面功能
  9. 数据库表连接总结:等值连接, 自然连接,左外连接,右外连接,内连接,全外连接;
  10. 朝着理想坚实迈进_坚实原则:单一责任原则