TProfiler介绍
losetowin 发布于:2019-1-15 10:04 分类:技术 有 3796 人浏览,获得评论 0 条 标签: tprofile 性能分析
除非注明,文章均为 www.dutycode.com 原创,欢迎转载!转载请注明本文地址,谢谢。
TProfiler介绍
0. 简单说明
TProfiler是阿里巴巴开源的一个用于线上性能分析的工具,且是一个可以在生产环境长期使用的工具
我们虽说没有长期在生产环境使用,但一般会在压测前一段时间在线上的某个节点上进行监测,用于发现性能的瓶颈
1. 下载
1.1下载地址
Github地址:https://github.com/alibaba/TProfiler
我们线上所用的安装包在dist目录下的TProfiler_1.0.1.zip,链接:https://github.com/alibaba/TProfiler/blob/master/dist/TProfiler_1.0.1.zip
点击Download即可下载
1.2安装部署
将TProfiler_1.0.1.zip上传到服务器上,我们这边放到了/opt/tprofiler目录下,将其解压,如下图:
几个关键的文件:
lib目录下的tprofiler-1.0.1.jar,主要的依赖库
根目录下的profile.properties,tprofiler的配置文件
bin目录下是一些常见的shell脚本,方便使用。
2. 配置
profile.properties配置文件说明:
一个简单的例子:
#log file name
logFileName= tprofiler.log
methodFileName = tmethod.log
samplerFileName = tsampler.log
#basic configuration items
startProfTime = 9:00:00
endProfTime = 16:00:00
eachProfUseTime = 5
eachProfIntervalTime = 50
samplerIntervalTime = 20
port = 50000
debugMode = false
needNanoTime = false
ignoreGetSetMethod = true
#file paths
logFilePath = ${user.home}/logs/${logFileName}
methodFilePath = ${user.home}/logs/${methodFileName}
samplerFilePath = ${user.home}/logs/${samplerFileName}
#include & excludes items
excludeClassLoader = org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader
includePackageStartsWith = com.taobao;com.taobao.common;com.bj58
excludePackageStartsWith = com.taobao.sketch;org.apache.velocity;com.alibaba;com.taobao.forest.domain.dataobject
配置项 | 说明 |
---|---|
logFileName | 日志文件名称 |
methodFileName | 日志文件名称(方法) |
samplerFileName | 日志文件民称(采样文件) |
startProfTime | 开始profile的时间点 |
endProfTime | 结束profile的时间 |
eachProfUseTime | profile时间长度(单位秒) |
eachProfIntervalTime | 两次profile的时间间隔(单位秒) |
samplerIntervalTime | 两次采样的时间间隔(单位秒) |
port | 端口,用于远程管理用 |
debugMode | 是否进入调试模式 |
needNanoTime | 是否需要用纳秒记录时间 |
ignoreGetSetMethod | 是否忽略采集get/set方法 |
logFilePath | log的存放位置 |
methodFilePath | method log的存放位置 |
samplerFilePath | sampler log的存放位置 |
excludeClassLoader | 不注入的类加载器(分号分割) |
includePackageStartsWith | 进行profile的类包名(分号分割) |
excludePackageStartsWith | 不进行profile的类包名(分号分割) |
表格中,标红的是我们经常需要配置的参数,其余的采用默认值即可。
3. 使用
3.1启动
在Jvm的启动参数中增加 -javaagent:/opt/tprofiler/lib/tprofiler-1.0.1.jar -Dprofile.properties=/opt/tprofiler/profile.properties 启动应用即可
启动成功之后,会按照logFilePath/methodFilePath/samplerFilePath的配置生成相应的日志文件
3.2远程管理
远程查看状态操作:
java -cp /opt/tprofiler/lib/tprofiler-1.0.1.jar com.taobao.profile.client.TProfilerClient IP 端口号 status
远程开始操作:
java -cp /opt/tprofiler/lib/tprofiler-1.0.1.jar com.taobao.profile.client.TProfilerClient IP 端口号 start
远程停止操作:
java -cp /opt/tprofiler/lib/tprofiler-1.0.1.jar com.taobao.profile.client.TProfilerClient IP 端口号 stop
远程刷出方法数据:
java -cp /opt/tprofiler/lib/tprofiler-1.0.1.jar com.taobao.profile.client.TProfilerClient IP 端口号 flushmethod
以上几个,也可以使用bin目录下的脚本操作。对应的脚本是
sh tprofiler-client IP 端口 指令(status/start/stop/flushmethod)
eg:
sh tprofiler-client 127.0.0.1 40000 status
3.3常用的性能分析操作
3.3.1普通方法、线程统计
java -cp /opt/tprofiler/lib/tprofiler-1.0.1.jar com.taobao.profile.analysis.SamplerLogAnalysis ~/logs/tsampler.log ~/logs/method.log ~/logs/thread.log
同样,这个在bin目录下也有相应的脚本
sh sampler-log-analysis 日志路径
3.3.2top统计
java -cp /opt/tprofiler/lib/tprofiler-1.0.1.jar com.taobao.profile.analysis.ProfilerLogAnalysis ~/logs/tprofiler.log ~/logs/tmethod.log ~/logs/topmethod.log ~/logs/topobject.log
同样,在bin目录下也有相应的脚本
sh tprofiler-log-analysis 日志路径
这个命令很实用,统计方法的执行时间,能够帮助我们发现在线上环境中,执行耗时top的方法,帮助我们优化。
以我们一台线上服务为例,执行之后,在topmethod.log中得到如下内容
格式说明:
方法信息 执行次数 平均执行时间(单位:ms) 全部执行时间(单位:ms)
我们就可以根据上述结果分析得到代码中可优化的地方,然后做相应的优化。
3.4卸载
在 jvm 启动参数中去掉相关参数 -javaagent:/opt/tprofiler/lib/tprofiler-1.0.1.jar -Dprofile.properties=/opt/tprofiler/profile.properties, 重启应用即可卸载此工具