使用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 ……
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
BTrace簡單實用教程 http://www.linuxidc.com/Linux/2012-12/76155.htm