使用BTrace 做性能测试
在官网https://kenai.com/projects/btrace/downloads/directory/releases/release-1.2.4
下载对应的jar包
测试时后,将btrace测试类和被测试类放在同一个机器上
将btrace测试类写好,放在btrace解压的bin目录下
调用如下命令进行测试
sh btrace pid BtraceTest.java
pid可通过jps命令来查看,默认端口是2020端口,如果被测试类有多个进程,则端口号累加
2021 2022 2023 ……
sh bin/btrace -p 2021 41479 bin/App.java > exeTime.txt
Btrace类
import com.sun.btrace.annotations.BTrace; import com.sun.btrace.annotations.Kind; import com.sun.btrace.annotations.Location; import com.sun.btrace.annotations.OnMethod; import com.sun.btrace.annotations.ProbeClassName; import com.sun.btrace.annotations.ProbeMethodName; import com.sun.btrace.annotations.TLS;
import static com.sun.btrace.BTraceUtils.*;
@BTrace public class BtraceTest {
@TLS private static long startTime=0; @OnMethod(clazz = "/*/", method = "/.*/", location = @Location(value=Kind.ENTRY, clazz="/.*/", method="/.*/")) public static void startMethod() { startTime =timeMillis(); }
@OnMethod(clazz = "/*/", method = "/.*/", location = @Location(value=Kind.RETURN, clazz="/.*/", method="/.*/")) public static void endMethod(@ProbeClassName String className, @ProbeMethodName String methodName) { println(concat("threadId:", str(threadId(currentThread())))); println(strcat("className:", className)); println(strcat("methodName:", methodName)); println(strcat("exeTime:", str(timeMillis()-startTime)));
}
@OnMethod(clazz = "//", method = "sendHttpRequest", location = @Location(value=Kind.RETURN, clazz="/.*/", method="/.*/")) public static void endMethod2(@ProbeClassName String className, @ProbeMethodName String methodName) { println("CommonBizImpl----sendHttpRequest----------"); //System.out.println("CommonBizImpl----sendHttpRequest----------"); println(concat("threadId:", str(threadId(currentThread())))); println(strcat("className:", className)); println(strcat("methodName:", methodName)); println(strcat("exeTime:", str(timeMillis()-startTime)));
}
} |
以上*为你要监测的类或者方法名,这里注释掉了
threadId:34746
className:*l
methodName:getCacheByTid
exeTime:4
threadId:34754
className:*
methodName:getCacheByTid
exeTime:4
threadId:34746
className:*
methodName:getCacheByTid
exeTime:4
threadId:34746
className:*
methodName:getCacheByTid
exeTime:3