使用阿里开源工具 TProfiler 在海量业务代码中精确定位性能代码

1. 下载

访问 TProfiler 的 GitHub 主页,点击 Clone or download 按钮的打开下载选项,复制git地址

# cd /usr/local/src
# https://github.com/alibaba/TProfiler.git

2. 安装

登录需要监控的远程服务器主机,为 TProfiler 新建安装路径:

# mkdir /usr/local/tprofiler

进入 /usr/local/src/TProfiler目录,将dist 目录下的 profile.properties 以及 dist/lib 目录下的 tprofiler-1.0.1.jar 移动到/usr/local/tprofiler 目录下。
最后将远程服务器 /usr/local/tprofiler 目录及其下所有文件的所有者改为启动 Tomcat 进程的用户及其所在用户组。

3.TProfiler 配置

编辑服务器 /opt/tprofiler/profile.properties 文件内容如下:

#log file name
logFileName = tprofiler.log
methodFileName = tmethod.log
samplerFileName = tsampler.log

#basic configuration items
startProfTime = 9:00:00
endProfTime = 23: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
excludePackageStartsWith = com.taobao.sketch;org.apache.velocity;com.alibaba;com.taobao.forest.domain.dataobject

红色部分是我们修改后的内容,其它部分使用默认值。

4. Tomcat 启动参数配置

在 Tomcat catalina.sh 启动参数里加入:
JAVA_OPTS=”-javaagent:/usr/local/tprofiler/tprofiler-1.0.1.jar -Dprofile.properties=/usr/local/tprofiler/profile.properties”
之后重启 Tomcat。