使用arthas检测Java程序方法执行时间

本文介绍了如何下载和启动Arthas工具,以便在不修改代码的情况下监控Java程序的运行状态,跟踪方法执行时间和异常,提升线上问题排查效率。它提供了一个详细的步骤指南,包括使用`trace`命令以及查看和退出dashboard的操作。
摘要由CSDN通过智能技术生成


一、简介

Arthas 是一款线上监控诊断产品,通过全局视角实时查看应用 load、内存、gc、线程的状态信息,并能在不修改应用代码的情况下,对业务问题进行诊断,包括查看方法调用的出入参、异常,监测方法执行耗时,类加载信息等,大大提升线上问题排查效率。

arthas文档地址:https://arthas.aliyun.com/doc/

二、下载与启动arthas

1.下载

下载地址:https://github.com/alibaba/arthas/releases

下载bin版本:https://github.com/alibaba/arthas/releases/download/arthas-all-3.7.2/arthas-bin.zip

2.解压

unzip arthas-bin.zip

三、使用

./as.sh

#执行上面的./as.sh后,会出现java进程列表,选中一个需要自己监控的进程编号进入

[sendi@localhost tmpDir]$ ./as.sh 
Arthas script version: 3.7.2
[INFO] JAVA_HOME: /usr/local/jdk1.8.0_202
Found existing java process, please choose one and input the serial number of the process, eg : 1. Then hit ENTER.
* [1]: 29059 chatbot-0.0.1-SNAPSHOT.jar
  [2]: 15145 cn.gzsendi.stcp.control.ControlClientStart
  [3]: 28955 chatbot-0.0.1-SNAPSHOT.jar
  [4]: 26381 myChatGpt-0.0.1-SNAPSHOT.jar
  
比如,选中1继续进入控制台

执行后如下:

[testuser@localhost tmpDir]$ ./as.sh 
Arthas script version: 3.7.2
[INFO] JAVA_HOME: /usr/local/jdk1.8.0_202
Found existing java process, please choose one and input the serial number of the process, eg : 1. Then hit ENTER.
* [1]: 29059 chatbot-0.0.1-SNAPSHOT.jar
  [2]: 15145 cn.gzsendi.stcp.control.ControlClientStart
  [3]: 28955 chatbot-0.0.1-SNAPSHOT.jar
  [4]: 26381 myChatGpt-0.0.1-SNAPSHOT.jar

Arthas home: /home/sendi/tmpDir
Calculating attach execution time...
Attaching to 29059 using version /home/sendi/tmpDir...

real    0m1.407s
user    0m0.362s
sys     0m0.054s
Attach success.
telnet connecting to arthas server... current timestamp is 1709013696
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
  ,---.  ,------. ,--------.,--.  ,--.  ,---.   ,---.                           
 /  O  \ |  .--. ''--.  .--'|  '--'  | /  O  \ '   .-'                          
|  .-.  ||  '--'.'   |  |   |  .--.  ||  .-.  |`.  `-.                          
|  | |  ||  |\  \    |  |   |  |  |  ||  | |  |.-'    |                         
`--' `--'`--' '--'   `--'   `--'  `--'`--' `--'`-----'                          

wiki       https://arthas.aliyun.com/doc                                        
tutorials  https://arthas.aliyun.com/doc/arthas-tutorials.html                  
version    3.7.2                                                                
main_class                                                                      
pid        29059                                                                
time       2024-02-27 14:01:35                                                  

[arthas@29059]$

跟踪方法的执行时间

tract 类名全路径 方法名

[arthas@28955]$ trace cn.test.modules.web.service.impl.ChatSessionDetailServiceImpl askQuetionStream

通过trace可以跟踪到这个方法的调用情况和执行时间,方便你定位问题
在这里插入图片描述

其他示例

##
trace 函数: trace demo.MathGame run

##
指定 Class 匹配的最大数量:trace demo.MathGame run -m 1

##
trace 次数限制:trace demo.MathGame run -n 1

##
包含 jdk 的函数:trace --skipJDKMethod false demo.MathGame run

##
根据调用耗时过滤(大于10ms的):trace demo.MathGame run '#cost > 10'

##
trace 多个类或者多个函数:
trace 命令只会 trace 匹配到的函数里的子调用,并不会向下 trace 多层。因为 trace 是代价比较贵的,多层 trace 可能会导致最终要 trace 的类和函数非常多。
可以用正则表匹配路径上的多个类和函数,一定程度上达到多层 trace 的效果。

trace -E com.test.ClassA|org.test.ClassB method1|method2|method3

##排除掉指定的类
使用 --exclude-class-pattern 参数可以排除掉指定的类,比如:

trace javax.servlet.Filter * --exclude-class-pattern com.demo.TestFilter

查看 dashboard

输入dashboard,按回车/enter,会展示当前进程的信息,按ctrl+c可以中断执行。

在这里插入图片描述

退出

在控制台命令行下,执行stop就可

[arthas@29059]$ 
[arthas@29059]$ stop
``
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值