Arthas - 线上某个接口忽然变慢了怎么查

咱们模拟一个简单的线上环境,有一个接口,有时候调用很慢,因为调用过程当中涉及到多个方法的调用,因此比较难肯定究竟是哪一个方法比较慢,咱们能够借助Arthas来看看。java

示例代码

这段代码中,trace方法会依次调用fun一、fun二、fun3方法。trace的入参是number,通过一系列伪装很复杂的计算并传到下一个方法。这部分代码是基于springboot的。web

package com.dajun.arthas.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
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);
    }
}

查看哪一个方法

首先咱们经过阿里云下载arthas,并运行jar包。spring

curl -O https://arthas.aliyun.com/arthas-boot.jar
java -jar arthas-boot.jar

运行后会出现下面的结果,而后咱们选择1,就能够进行监听咱们的应用程序。
若是不知道对应的PID,能够用jps或者其余方式查。浏览器

[root@ecs-4fbd Arthas]# java -jar arthas-boot.jar 
[INFO] arthas-boot version: 3.5.0
[INFO] Found existing java process, please choose one and input the serial number of the process, eg : 1. Then hit ENTER.
* [1]: 15679 arthas-1.0-SNAPSHOT.jar

输入1后,咱们用trace命令,格式是trace+空格+类名的全路径+空格+方法。执行结果以下,此时开始监听咱们的方法。tomcat

[arthas@15679]$ trace com.dajun.arthas.controller.TestController trace 
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 65 ms, listenerId: 1

咱们在浏览器上输入地址http://192.168.0.101:8080/trace?number=2并entrer。咱们能够看到,trace方法执行了330.746895毫秒,fun1执行了330.40442毫秒。springboot

[arthas@15976]$ trace com.dajun.arthas.controller.TestController trace
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 76 ms, listenerId: 1
`---ts=2021-03-18 17:29:22;thread_name=http-nio-8080-exec-1;id=11;is_daemon=true;priority=5;TCCL=org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedWebappClassLoader@27ae2fd0
    `---[330.746895ms] com.dajun.arthas.controller.TestController:trace()
        `---[330.40442ms] com.dajun.arthas.controller.TestController:fun1() #13

因为trace命令只会匹配当前的方法,以及下一级方法。因此咱们上面的命令并无很清楚的知道具体哪一个方法比较慢,因此咱们能够继续监听fun1方法,没发现问题再监听fun2,这样的话就比较麻烦。
Arthas提供了正则表匹配路径上的多个类和函数,因此咱们执行如下的命令,在trace后面加-E,注意是大写,而后方法后面用|分隔,固然多个类也能够这样。app

trace -E com.dajun.arthas.controller.TestController trace|fun1|fun2|fun3 
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 4) cost in 68 ms, listenerId: 2

运行结果以下,咱们能够看到fun2的耗时比较长。curl

[arthas@15976]$ trace -E com.dajun.arthas.controller.TestController trace|fun1|fun2|fun3
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 4) cost in 74 ms, listenerId: 2
`---ts=2021-03-18 17:29:57;thread_name=http-nio-8080-exec-2;id=12;is_daemon=true;priority=5;TCCL=org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedWebappClassLoader@27ae2fd0
    `---[330.932104ms] com.dajun.arthas.controller.TestController:trace()
        `---[330.826788ms] com.dajun.arthas.controller.TestController:fun1() #13
            `---[330.757684ms] com.dajun.arthas.controller.TestController:fun1()
                `---[320.577832ms] com.dajun.arthas.controller.TestController:fun2() #20
                    `---[320.489582ms] com.dajun.arthas.controller.TestController:fun2()
                        `---[20.301173ms] com.dajun.arthas.controller.TestController:fun3() #26
                            `---[20.191794ms] com.dajun.arthas.controller.TestController:fun3()

查看入参

watch函数

watch com.dajun.arthas.controller.TestController fun2  "{params,returnObj}"   -x 2 -b

当咱们从新调用接口时,看到传入了4。这个时候,咱们就能够经过这个参数的值查看为何这个方法执行这么慢了。阿里云

[arthas@15976]$ watch com.dajun.arthas.controller.TestController fun2  "{params,returnObj}"   -x 2 -b
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 28 ms, listenerId: 10
method=com.dajun.arthas.controller.TestController.fun2 location=AtEnter
ts=2021-03-18 21:06:19; [cost=0.012014ms] result=@ArrayList[
    @Object[][
        @Integer[4],
    ],
    null,
]
相关文章
相关标签/搜索