使用arthas
下载arthas
curl -O https://arthas.aliyun.com/arthas-boot.jar
启动arthas
java -jar arthas-boot.jar
选择3,进行监听我们的应用程序。
模拟CPU高的场景
@GetMapping("/cpu")
public void cpu() {
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 5, 1000, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(100));
threadPoolExecutor.execute(new Runnable() {
public void run() {
while (true) {
log.info(System.currentTimeMillis() + "");
}
}
});
}
dashboard
thread -n {number}
- 按照CPU使用率排序,并展示前n个线程
thread [pid]
- 展示指定线程的线程栈
模拟查看方法调用耗时
@RestController
@Slf4j
public class TestController {
@RequestMapping("/trace")
public String trace(int number) throws InterruptedException {
number++;
fun1(number);
return "Hello World!";
}
private void fun1(int number) throws InterruptedException {
TimeUnit.MILLISECONDS.sleep(10);
number++;
fun2(number);
}
private void fun2(int number) throws InterruptedException {
TimeUnit.MILLISECONDS.sleep(300);
number++;
fun3(number);
}
private void fun3(int number) throws InterruptedException {
TimeUnit.MILLISECONDS.sleep(20);
}
}
trace
trace com.charles.web.TestController trace
,监听TestController
的trace
方法。
访问wget http://localhost:7077/trace?number=1
,可以看出trace
和fun1
方法的耗时。
trace命令只会匹配当前的方法,以及下一级方法。
trace -E com.charles.web.TestController trace|fun1|fun2|fun3
可以看出fun2
方法耗时比较长。
watch
watch com.charles.web.TestController fun2 "{params,returnObj}" -x 2 -b
,watch方法可以观察到入参,此时入参是3。