1、TProfiler下载与安装
第一步先去Github https://github.com/taobao/TProfiler下载源文件,接着将下载好的TProfiler-master.zip解压并打开文件夹,将dist下的TProfiler_1.0.1.zip再解压,最后将TProfiler_1.0.1中的profile.properties、lib/tprofiler-1.0.1.jar 拷贝出来,我们只需要这两个文件即可。
第二步我们看下profile.properties中的每个参数的含义。
第三步,在我们设置好profile.properties相关属性后,将这两个文件拷贝到本地/usr/local/tprofiler/目录下,同时添加jvm启动参数:
-javaagent:/usr/local/tprofiler-1.0.1.jar
-Dprofile.properties=/usr/local/tprofiler/profile.properties
然后启动相应的tomcat、weblogic等服务器,观察输出没有报错即可。
#基础的一些参数配置,一般使用默认即可
startProfTime = 9:00:00 开始profile的时间点
endProfTime = 23:00:00 结束profile的时间点
eachProfUseTime = 5 profile时间长度(单位秒)
eachProfIntervalTime = 50 两次profile的时间间隔(单位秒)
samplerIntervalTime = 20 两次采样的时间间隔(单位秒)
port = 30033 对外提供服务支持查询的端口号
debugMode = false 是否进入调试模式
needNanoTime = false 是否需要用纳秒记录时间
ignoreGetSetMethod = true 是否忽略采集get/set方法
#下面三个日志的存放路径、名称可以自行设置
logFilePath = /usr/tprofiler/logs/logprofiler.log (profile log的存放位置)
methodFilePath = /usr/tprofiler/logs/logmethod.log (method log的存放位置)
samplerFilePath = /usr/tprofiler/logs/logsampler.log (sampler log的存放位置)
#需要、不需要profile的包路径
excludeClassLoader = org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader 不注入的类加载器(分号分割),使用默认即可
includePackageStartsWith = com.alibaba.fastjson;com.fasterxml.jackson.databind;org.quartz;org.springframework (进行profile的类包名(分号分割),这里按需求,将项目中需要profile的包名入进来即可)
excludePackageStartsWith = com.taobao.sketch;org.apache.velocity;com.alibaba;com.taobao.forest.domain.dataobject 不进行profile的类包名(分号分割),使用默认即可
2、TProfiler远程操作
Java -cp /usr/local/tprofiler/tprofiler-1.0.1.jar com.taobao.profile.client.TProfilerClient [ip] [port] [command]
这个命令是用于查看TProfiler的运行情况,ip:TProfiler的机器ip,port:上面配置文件中的port,command:start、stop、status、flushmethod。
其中flushmethod是将运行过程中采集的数据输出到指定目录下。例如输出到/usr/tprofiler/logs/下,如配置文件中的logprofiler.log、logmethod.log、logsampler.log。
3、TProfiler本地数据统计
普通方法、线程统计命令
java -cp /usr/local/tprofiler/tprofiler-1.0.1.jar com.taobao.profile.analysis.SamplerLogAnalysis /usr/tprofiler/logs/logsampler.log /usr/tprofiler/logs/allmethod.log
/usr/tprofiler/logs/allthread.log 这条命令里,logsampler.log的路径在配置文件中指定,后面两个日志文件是分析统计后的日志输出路径,一个是所有方法的调用情况,另一个是所有线程的运行状态,从类名也可看出,就是对生成的sampler日志进行统计分析。
4、top 统计
java -cp /usr/local/tprofiler/tprofiler-1.0.1.jar com.taobao.profile.analysis.ProfilerLogAnalysis
#待分析的日志路径
/usr/tprofiler/logs/logprofiler.log /usr/tprofiler/logs/logmethod.log
#分析后输出日志
/usr/tprofiler/logs/topmethod.log /usr/tprofiler/logs/topobject.log
上面命令中的前两个参数是logprofiler.log、logmethod.log的路径,后面两个参数是分析后所得日志的输出路径。(其中:topmethod.log指调用次数最多的一些方法,topobject指调用次数最多的对象类)
调用该命令后,查看topmethod.log的结构如下:
Tables | 执行次数 | 平均执行时间 | 全部执行时间 |
---|---|---|---|
/lib/tpcache/mongodb/MongodbTemplate:getById:140 | 229 | 390 | 89364 |
/lib/tp/HttpClientTemplate:getForObject:92 | 255 | 39 | 9905 |
/api/module/CacheService:getVideoById:115 | 229 | 0 | 79 |
/api/module/comment/CommentService:getCommentList:130 | 226 | 0 | 74 |
/api/module/comment/dto/CommentListDto::93 | 2 | 3 | 6 |
/api/module/comment/CommentController:getCommentList:46 | 226 | 0 | 0 |
/lib/tpcache/cbase/MemcachedTemplate:getProtobuf:448 | 1 | 2 | 2 |
/lib/tp/comment/CommentTpDao:comment:67 | 255 | 0 | 1 |
参考:
http://blog.csdn.net/chen7253886/article/details/54091666
http://blog.csdn.net/defonds/article/details/52605670
http://blog.kinval.com/open/taobao-profiler.html