线上环境大多为 Linux , 有时候代码执行缓慢,但你却无法定位到底是慢在了什么地方,或者说是比较困难来定位,本文介绍一种方式,方便你的问题定位。
首先,我们来说说一款工具:Arthas ,它主要是通过一款阿里开源的工具,详情请点击 阿里开源的 java 诊断工具—— Arthas 查看。官方网站为 : https://alibaba.github.io/arthas/trace.html
主要是通过这个工具提供的 trace 命令来追踪。
具体的下载安装及使用方式,参考上面链接中提到的。
下载安装完成后,直接输入如下命令:
Connecting to arthas server... current timestamp is 1541128186
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
,---. ,------. ,--------.,--. ,--. ,---. ,---.
/ O \ | .--. ''--. .--'| '--' | / O \ ' .-'
| .-. || '--'.' | | | .--. || .-. |`. `-.
| | | || |\ \ | | | | | || | | |.-' |
`--' `--'`--' '--' `--' `--' `--'`--' `--'`-----'
wiki: https://alibaba.github.io/arthas
version: 3.0.4
pid: 51807
timestamp: 1541128186384
$
$ trace
The argument 'class-pattern' is required
$ trace -h
USAGE:
trace [-h] [-j] [-n <value>] [-p <value>] [-E] class-pattern method-pattern [condition-express]
SUMMARY:
Trace the execution time of specified method invocation.
The express may be one of the following expression (evaluated dynamically):
target : the object
clazz : the object's class
method : the constructor or method
params[0..n] : the parameters of method
returnObj : the returned object of method
throwExp : the throw exception of method
isReturn : the method ended by return
isThrow : the method ended by throwing exception
#cost : the execution time in ms of method invocation
EXAMPLES:
trace -E org\\.apache\\.commons\\.lang\\.StringUtils isBlank
trace org.apache.commons.lang.StringUtils isBlank
trace *StringUtils isBlank
trace *StringUtils isBlank params[0].length==1
trace *StringUtils isBlank '#cost>100'
WIKI:
https://alibaba.github.io/arthas/trace
OPTIONS:
-h, --help this help
-j, --jdkMethodSkip skip jdk method trace
-n, --limits <value> Threshold of execution times
-p, --path <value> path tracing pattern
-E, --regex Enable regular expression to match (wildcard matching by default)
<class-pattern> Class name pattern, use either '.' or '/' as separator
<method-pattern> Method name pattern
<condition-express> Conditional expression in ognl style, for example:
TRUE : 1==1
TRUE : true
FALSE : false
TRUE : 'params.length>=0'
FALSE : 1==2
$
以上为该命令的帮助信息,具体如何使用。我在这里做一个简单的 Demo 来演示一下。
比如我要追踪我的某一个类及类中每个调用的耗时,在命令中输入(为了演示隐藏了部分包名):
$ trace com.TestAction testMethodName params.length=1
Press Ctrl+C to abort.
Affect(class-cnt:2 , method-cnt:2) cost in 303 ms.
$ trace com.TestAction testMethodName
Press Ctrl+C to abort.
Affect(class-cnt:2 , method-cnt:2) cost in 391 ms.
`---ts=2018-11-02 11:14:19;thread_name=http-nio-8383-exec-138;id=b5;is_daemon=true;priority=5;TCCL=org.apache.catalina.loader.WebappClassLoader@4c12c6ec
`---[10.091747ms] com.TestAction$$EnhancerBySpringCGLIB$$4d4515a6:scanQr()
`---[9.739745ms] org.springframework.cglib.proxy.MethodInterceptor:intercept()
`---[6.161854ms] com.TestAction:testMethodName()
+---[0.082935ms] com.ActionResultMap:<init>()
+---[0.026972ms] javax.servlet.http.HttpServletRequest:getHeader()
+---[min=0.001465ms,max=0.003437ms,total=0.011679ms,count=6] java.lang.StringBuilder:<init>()
+---[min=0.001385ms,max=0.003889ms,total=0.02629ms,count=14] java.lang.StringBuilder:append()
.....
.....
.....
以上为输出结果信息。主要最后一行打印的信息:
min=0.001385ms,max=0.003889ms,total=0.02629ms,count=14
简单说明一下:
StringBuilder.append() 这个方式,调用:
最小耗时为: 0.001385ms;
最大耗时为: 0.003889ms;
调用 14 后,共耗时:0.02629ms
好强大的工具~~~~感谢阿里技术人员开源。