Btrace是一个实时监控工具,可以无需修改应用代码(事实上它修改了字节码),来达到不可告人的秘密!这是性能调优和诊断的利器!

它可以获取应用程序代码的执行时间,他可以让你无需修改代码,帮你做时间的打点。它还可以做的有很多多!

但是,你需要编写btrace脚本,它是一个java文件。在Eclipse中编写java类很简单,在linux上,vm不熟悉的可能就会比较痛苦了,并且脚本可复制性很强。

因此,我对btrace做了一个封装和拓展,你无须再编写java脚本,只需要敲几个命令,便可以实现你想要的功能!

为了减少重复工作的增加,拓展工具提供了以下特色功能:

1) 命令行交互式方式

2) 封装了常见的方法监控命令,无需再编写btrace脚本

3) 集成了一些典型的监控对象,如druid连接池,sql监控,isearch等

4) 未改变原有的btrace功能,仍然支持btrace脚本方式

软件获取和运行

github:https://github.com/xxzmxx/btrace_extend/

svn co 到linux/windows某个位置,进入bin目录

Linux: chmod +x btrace (赋予btrace 可执行权限)

windows无需操作。

命令行下,执行命令

./btrace [pid]

【pid可以通过jps获取】

进入交互界面

提供命令行交互

原有的btrace是通过形如 btrace pid xx.java文件。通过本次改造,我们提供了命令行交互方式,增加用户体验:

输入help,获取可用的命令

btrace>help
-------------------------------------------------------------------------------------------------------------------------------
| CommandName  CommandArgs                       CommandUsage
| time         {clazzname methodname}[interval]  collect response time ,and it can be executed by a interval set(s),;Example
|                                                time java.lang.Thread start 
2
|
| aggre        {clazzname methodname}[interval]  aggregation response time,it's a well fuction;Example
|                                                aggre java.lang.Thread start 
2
|
| call         {clazzname methodname}[interval]  when method called,it prints;Example
|                                                call java.lang.Thread start 
2
|
| jstack       {clazzname methodname}            when method called,it prints stackTrace;Example:
|                                                jstack java.lang.Thread start 
2
|
| file         {filePath}                        based on old btrace funtion,it can be run from a java file.Example:
|                                                file Test.java
|
| druid                                          Monitor druid dataSource?Example
|                                                druid
|
| isearch                                        Monitor Isearch execute time ;Example
|                                                isearch
|
| sql                                            Monitor sql execute time ;Example
|                                                sql
|
| sqlde                                          Monitor sql execute time and print Sql ;Example
|                                                sqlde
|
| quit                                           exit btrace
|                                                quit
| -------------------------------------------------------------------------------------------------------------------------------

btrace运行操作

btrace在运行监控时,控制台是阻塞输入的。只有通过Ctrl+c,来向应用发送信号,btrace会有3个选项供选择:

btrace>time java.lang.Thread start
StartMonitor [java.lang.Thread.start()] time(ms)
Please enter your option:
        
1
. exit
        
2
. send an event
        
3
. send a named event

1代表退出监控,回到btrace交互模式;

2代表发送一个事件,会触发btrace去打印监控信息到控制台。如aggre 、file、druid、isearch、sql、sqlde命令均可以通过该方式,打印出监控信息

3发送一个指定名称的事件,暂时未支持

命令演示

本次拓展,共支持9个命令功能。

1)time

usage:time clazzname method [interval]

time功能为获取指定类的方法的响应时间。并且它包含一个可选参数,即可以提供。

例子:我们想监控类com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp 中方法storeProductToDraftOrNormal的耗时情况。则输入命令

btrace>time com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp storeProductToDraftOrNormal
StartMonitor [com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp.storeProductToDraftOrNormal()] time(ms)

btrace就开始收集这个方法调用的时间,如果这个方法被调用,就会打印出时间信息。

btrace>time com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp storeProductToDraftOrNormal
StartMonitor [com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp.storeProductToDraftOrNormal()] time(ms)
[Clazz.method]          Time(ms)
com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp.storeProductToDraftOrNormal  
19
[Clazz.method]          Time(ms)
com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp.storeProductToDraftOrNormal  
7
[Clazz.method]          Time(ms)
com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp.storeProductToDraftOrNormal  
7
[Clazz.method]          Time(ms)
com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp.storeProductToDraftOrNormal  
7
[Clazz.method]          Time(ms)
com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp.storeProductToDraftOrNormal  
7
[Clazz.method]          Time(ms)
com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp.storeProductToDraftOrNormal  
7
[Clazz.method]          Time(ms)
com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp.storeProductToDraftOrNormal  
8
[Clazz.method]          Time(ms)
com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp.storeProductToDraftOrNormal  
7
[Clazz.method]          Time(ms)
com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp.storeProductToDraftOrNormal  
7
[Clazz.method]          Time(ms)
com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp.storeProductToDraftOrNormal  
8

但是如果我的时间调用次数很多,就会出现残酷的刷屏现象,我希望每隔2s进行采样。输入如下命令:

btrace>time com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp storeProductToDraftOrNormal 
2
StartMonitor [com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp.storeProductToDraftOrNormal()] time(ms)
[Clazz.method]                                                                               AvgTime(ms)     Count(total)
com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp.storeProductToDraftOrNormal 
6              
3
[Clazz.method]                                                                               AvgTime(ms)     Count(total)
com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp.storeProductToDraftOrNormal 
7              
13
[Clazz.method]                                                                               AvgTime(ms)     Count(total)
com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp.storeProductToDraftOrNormal 
7              
20
[Clazz.method]                                                                               AvgTime(ms)     Count(total)
com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp.storeProductToDraftOrNormal 
7              
20

btrace会每隔2s收集一次信息,并对每次的信息做统计,包括均值和调用次数总计。

2)aggre

usage:time clazzname method [interval]

如果我们觉得time命令还是不够满足我需求,我希望能够提供一些更详细的值,比如在采集的这段时间,rt的最大值,最小值,均值,以及分布,还有调用总数。此时可以通过聚合命令来完成

例子:我们想监控类com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp 中方法storeProductToDraftOrNormal的耗时,并提供聚合报告。则输入命令

btrace>aggre com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp storeProductToDraftOrNormal
StartMonitor [com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp.storeProductToDraftOrNormal()] By Aggregation time(ms)

通过触发Ctrl+c,选择选项2,获取输出结果:

btrace>aggre com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp storeProductToDraftOrNormal
StartMonitor [com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp.storeProductToDraftOrNormal()] By Aggregation time(ms)
Please enter your option:
        
1
. exit
        
2
. send an event
        
3
. send a named event
2
QUANTIZE
  
com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp
                  
value  ------------- Distribution ------------- count
                      
2 
|                                         
0
                      
4 
|@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@     
45
                      
8 
|@@@                                      
4
                     
16 
|                                         
1
                     
32 
|                                         
0
 
AVERAGE
  
com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp                
7
MAXIMUM
  
com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp               
19
SUM
  
com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp              
361
COUNT
  
com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp               
50

这里提供了针对时间的聚合信息,以ms为单位。并提供了时间分布的功能,可以清晰的看出该模块时间是否稳定。

3)call

这个命令和time命令用法基本相同,请参考time命令

4)jstack

usage:jstack clazzname method

当应用中存在调用clazzname的method时,会打印出调用堆栈,这个方便我们查找问题。

例如想监控com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp 中方法storeProductToDraftOrNormal是否被调用,如果调用,希望看到它的堆栈。

btrace>jstack com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp storeProductToDraftOrNormal
StartMonitor [com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp.storeProductToDraftOrNormal()]  && Print java stack info!
-------------------------------stack info--------------------------
com.alibaba.intl.ae.module.wsproduct.utils.WholesaleProductHelp.storeProductToDraftOrNormal(WholesaleProductHelp.java)
com.alibaba.intl.ae.module.wsproduct.persistent.AeProductForMysqlPersistent.persistent(AeProductForMysqlPersistent.java:
32
)
com.alibaba.intl.ae.morina.core.actionhandler.BasePublishActionHandler.invoke(BasePublishActionHandler.java:
69
)
com.alibaba.intl.ae.morina.core.skeleton.publish.impl.ActionHandlerInterceptorChainInvocation.invoke(ActionHandlerInterceptorChainInvocation.java:
67
)
com.alibaba.intl.ae.module.wsproduct.interceptor.AeProductDetailInterceptor.intercept(AeProductDetailInterceptor.java:
40
)
com.alibaba.intl.ae.morina.core.skeleton.publish.impl.ActionHandlerInterceptorChainInvocation.invoke(ActionHandlerInterceptorChainInvocation.java:
63
)
com.alibaba.intl.ae.module.wsproduct.interceptor.AeProductCatPropertyInterceptor.intercept(AeProductCatPropertyInterceptor.java:
101
)
com.alibaba.intl.ae.morina.core.skeleton.publish.impl.ActionHandlerInterceptorChainInvocation.invoke(ActionHandlerInterceptorChainInvocation.java:
63
)
com.alibaba.intl.ae.module.wsproduct.interceptor.AeProductPostCountCheckInterceptor.intercept(AeProductPostCountCheckInterceptor.java:
51
)
com.alibaba.intl.ae.morina.core.skeleton.publish.impl.ActionHandlerInterceptorChainInvocation.invoke(ActionHandlerInterceptorChainInvocation.java:
63
)
com.alibaba.intl.ae.module.wsproduct.interceptor.AeProductPersonAuthInterceptor.intercept(AeProductPersonAuthInterceptor.java:
45
)
com.alibaba.intl.ae.morina.core.skeleton.publish.impl.ActionHandlerInterceptorChainInvocation.invoke(ActionHandlerInterceptorChainInvocation.java:
63
)
com.alibaba.intl.ae.module.wsproduct.interceptor.AeProductPunishInterceptor.intercept(AeProductPunishInterceptor.java:
32
)
com.alibaba.intl.ae.morina.core.skeleton.publish.impl.ActionHandlerInterceptorChainInvocation.invoke(ActionHandlerInterceptorChainInvocation.java:
63
)
com.alibaba.intl.ae.module.wsproduct.interceptor.AutoValidateProductInterceptor.intercept(AutoValidateProductInterceptor.java:
36
)
com.alibaba.intl.ae.morina.core.skeleton.publish.impl.ActionHandlerInterceptorChainInvocation.invoke(ActionHandlerInterceptorChainInvocation.java:
63
)
com.alibaba.intl.ae.module.wsproduct.interceptor.MemberInfoInitializationInterceptor.intercept(MemberInfoInitializationInterceptor.java:
73
)
com.alibaba.intl.ae.morina.core.skeleton.publish.impl.ActionHandlerInterceptorChainInvocation.invoke(ActionHandlerInterceptorChainInvocation.java:
63
)
com.alibaba.intl.ae.morina.core.skeleton.publish.impl.AeProductPublishController.invoke(AeProductPublishController.java:
50
)
com.alibaba.intl.ae.morina.core.skeleton.publish.impl.AeProductPublishController.invoke(AeProductPublishController.java:
25
)
com.alibaba.intl.ae.morina.core.portal.InvocationControllerAutowirePostProcessor$
1
.intercept(InvocationControllerAutowirePostProcessor.java:
44
)
com.alibaba.intl.ae.morina.core.portal.InvocationControllerPortal$$EnhancerByCGLIB$$276335c.invoke(<generated>)
com.alibaba.intl.ae.module.wsproduct.service.AeProductPublishRemoteServiceImpl.postAeProduct(AeProductPublishRemoteServiceImpl.java:
36
)
sun.reflect.GeneratedMethodAccessor380.invoke(Unknown Source)

5)file

这个命令其实是为了保留btrace原始的监控方式。所以如果你编写了btrace 脚本,那么直接使用:

file script.java

6)druid

该命令没有参数,直接输入druid即可。druid是一个数据库连接池。在性能测试时,时常要监控连接池的饱满情况,以及是否存在性能瓶颈,那么监控连接池的活动情况是很直接的方法。

例子如下:

btrace>druid
StartMonitor druid Connection DataSource
Please enter your option:
        
1
. exit
        
2
. send an event
        
3
. send a named event
2
------------------------------JDBC-URL-------------------------------    avalibleConn  activeConn  totalConn  Allconn  useage%
dbc:mysql:
//10.20.147.144:3306/ws_product_press                          4                 0            4          20      20.0
dbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=
10.20
.
149
.   
8                 
2           
10          
20      
50.0
dbc:mysql:
//10.20.147.144:3306/ws_sku_press                              6                 0            6          50      12.0
dbc:alibaba:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=
10    
7                
2            
9          
10      
90.0

7)sql

该命令没有参数,直接输入sql即可。如果你的应用包含数据库操作,且使用ibatis框架,那么它会列出所有sql的操作时间,但是不显示详细sql,只显示ibatis-id。如果要显示详细sql,使用sqlde命令获取

btrace>sql
StartMonitor Ibtis Sql
Please enter your option:
        
1
. exit
        
2
. send an event
        
3
. send a named event
2
---------------------------------------------
Count
                 
FREIGHT_TEMPLATE.selectByPrimaryKey             
1290
          
WS_PRODUCT_DETAIL_DRAFT.selectByPrimaryKey             
1290
          
WS_PRODUCT_DETAIL_DRAFT.deleteByPrimaryKey             
1290
Min
                 
FREIGHT_TEMPLATE.selectByPrimaryKey                
0
          
WS_PRODUCT_DETAIL_DRAFT.selectByPrimaryKey                
0
          
WS_PRODUCT_DETAIL_DRAFT.deleteByPrimaryKey                
0
Max
 
          
WS_PRODUCT_DETAIL_DRAFT.selectByPrimaryKey               
14
          
WS_PRODUCT_DETAIL_DRAFT.deleteByPrimaryKey               
14
                 
FREIGHT_TEMPLATE.selectByPrimaryKey               
15
Average
                 
FREIGHT_TEMPLATE.selectByPrimaryKey                
0
          
WS_PRODUCT_DETAIL_DRAFT.selectByPrimaryKey                
0
          
WS_PRODUCT_DETAIL_DRAFT.deleteByPrimaryKey                
0
Sum
 
                 
FREIGHT_TEMPLATE.selectByPrimaryKey              
144
          
WS_PRODUCT_DETAIL_DRAFT.deleteByPrimaryKey              
161
          
WS_PRODUCT_DETAIL_DRAFT.selectByPrimaryKey              
186
Histogram
   
WS_CATE_CONSULT_CFGT.selectOldCategoryAttriNumber
                  
value  ------------- Distribution ------------- count
                     
-
1 
|                                         
0
                      
0 
|@@@@@@@@@@@@@@@@@@@@@@@@@@@@@            
469
                      
1 
|@@@@@@@                                  
127
                      
2 
|@                                        
28
                      
4 
|                                         
12
                      
8 
|                                         
7
                     
16 
|                                         
2
                     
32 
|                                         
0
 
     
WHOLESALE_STORE.findWholesaleStoreDOByCompanyId
                  
value  ------------- Distribution ------------- count
                     
-
1 
|                                         
0
                      
0 
|@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@     
587
                      
1 
|@@                                       
37
                      
2 
|                                         
8
                      
4 
|                                         
6
                      
8 
|                                         
6
                     
16 
|                                         
1
                     
32 
|                                         
0
 
Global Count
            
19150

ps.上述内容过于繁多,只选择了部分。

8)sqlde

该命令没有参数,直接输入sqlde即可。该命令不但会聚合所有sql的rt,也会打印sql详情。不过这个sql的排版目前做的还不是很好。会有些乱。建议还是使用sql命令来看。sql少的话,使用sqlde命令。 

btrace拓展工具-java应用性能诊断优化利器的更多相关文章

  1. 阿里数据库性能诊断的利器——SQL执行干预

    概述 在业务数据库性能问题诊断中,如果发现一个业务性能很差跟某个SQL有关,应用连接池几乎被该SQL占满,同时数据库服务器上也不堪重负.此时情况很紧急,业务改SQL重发布已经来不及了,运维能选择的操作 ...

  2. Java项目性能持续优化中……

     尽量使用StringBuilder和StringBuffer进行字符串连接, 参考链接: Java编程中“为了性能”尽量要做到的一些地方

  3. Java 后台性能优化简要

    业务系统性能优化的前提时观察和诊断.观察工具例如以下:前端优化工具:YSlow页面响应时间:Firebug方法对应时间:btraceGC日志分析:JVM 启动參数数据库优化:慢查询系统资源调用:监控 ...

  4. 从虚拟机视角谈 Java 应用性能优化

    从虚拟机视角谈 Java 应用性能优化 周 祥, 软件工程师, IBM 简介:Java 的普及和广泛应用,以及其基于虚拟机运行的机制,使得性能问题越来越重要.本文从 Java 虚拟机的角度,特别是垃圾 ...

  5. Java的性能优化

    http://www.toutiao.com/i6368345864624144897/?tt_from=mobile_qq&utm_campaign=client_share&app ...

  6. Java GC 专家系列5:Java应用性能优化的原则

    本文是GC专家系列中的第五篇.在第一篇理解Java垃圾回收中我们学习了几种不同的GC算法的处理过程,GC的工作方式,新生代与老年代的区别.所以,你应该已经了解了JDK 7中的5种GC类型,以及每种GC ...

  7. [转帖]Java虚拟机(JVM)体系结构概述及各种性能参数优化总结

    Java虚拟机(JVM)体系结构概述及各种性能参数优化总结 2014年09月11日 23:05:27 zhongwen7710 阅读数 1437 标签: JVM调优jvm 更多 个人分类: Java知 ...

  8. Java 程序该怎么优化?(工具篇)

    程序员:为什么程序总是那么慢?时间都花到哪里去了? 面试官:若你写的 Java 程序,出现了性能问题,该怎么去排查呢? 工欲善其事必先利其器,为你呈上一箩筐性能优化工具,必有一款满足你,废话不多说,直 ...

  9. Java程序性能优化技巧

    Java程序性能优化技巧 多线程.集合.网络编程.内存优化.缓冲..spring.设计模式.软件工程.编程思想 1.生成对象时,合理分配空间和大小new ArrayList(100); 2.优化for ...

随机推荐

  1. [原]Unity3D深入浅出 - 物理引擎之刚体部件(Rigidbody)

    在虚拟世界中,任何物体都是没有活力的,要想变的真实,Rigidbody是必不可少的组件,下面介绍Rigidbody的各个属性: Mass:质量 Drag:阻力,对象在运动时遇到的空气阻力,0表示没有空 ...

  2. [Unity3d]小地图的制作

    继续今天的学习心得,unity中小地图的制作,实现了小地图中红色小箭头代表场景中的主角,然后人物方向的转变,小地图中箭头也随之改变方向. 效果图     右上角就是小地图,上面有个红色小箭头就是代表主 ...

  3. SGU 326 Perspective ★(网络流经典构图の竞赛问题)

    [题意]有n(<=20)只队伍比赛, 队伍i初始得分w[i], 剩余比赛场数r[i](包括与这n只队伍以外的队伍比赛), remain[i][j]表示队伍i与队伍j剩余比赛场数, 没有平局, 问 ...

  4. 关于Azure存储账户中存储虚拟机VHD文件的注意事项

     Joy Qiao from MSFT  Thu, Mar 12 2015 3:16 PM 我们在使用Azure时经常都会在Azure存储账户中放一些文件,包括Azure虚机的VHD文件也都是放在存储 ...

  5. 探秘Java虚拟机——内存管理与垃圾回收

    本文主要是基于Sun JDK 1.6 Garbage Collector(作者:毕玄)的整理与总结,原文请读者在网上搜索. 1.Java虚拟机运行时的数据区 2.常用的内存区域调节参数 -Xms:初始 ...

  6. oracle11g dataguard部署指南

    一.Oracle11DB+DG配置 1. 单机环境介绍(PRIMARY DATABASE) 主库 primary public ip                    192.168.0.252 ...

  7. SharePoint 2010 Pop-Up Dialogs

    转:http://kyleschaeffer.com/sharepoint/sharepoint-2010-pop-up-dialogs/ SharePoint 2010 makes it incre ...

  8. 初识NuGet - 概念, 安装和使用

    1. NuGet是什么? NuGet is a Visual Studio 2010 extension that makes it easy to add, remove, and update l ...

  9. ODAC访问oracle时,提示:由于以前的函数求值超时,函数求值被禁用,必须继续执行才能正常返回

    这是因为调试时会自动对Local/Watch等窗口里面(或鼠标停留所在)的变量求值,为了防止用户写的程序错误(比如死循环),系统有一个超时限制,如果某个属性的get中做了很复杂的操作(而不是简单地返回 ...

  10. 在Sql Server 查询分析器里使用事务

    declare @updatecount int begin TRAN DECLARE @tbid INTSELECT @tbid=iMax FROM tbID WHERE strName='ipcc ...