在项目中有时候项目运行上线了碰到了问题,比如想看一个方法运行了多长时间,或者一个方法的返回参数和入参是多少,在不知道btrace的时候,只能更换class加入日志然后重启项目进行问题的定位,但是如果有了btrace 可以再遇到问题的时候在方法上面动态的增加观察 还没必要去在原来代码里面加入代码然后重启项目。
首先要下载btrace的包,这个包可以去btrace的官网下载,然后安装也很简单 是绿色版的,直接把包的bin目录配置到坏境变量里面即可:
1:计算一个类产生对象的个数
package com.djk.demo1;
public class MyCompent
{
}
package com.djk.demo1;
import com.sun.btrace.annotations.BTrace;
import com.sun.btrace.annotations.*;
import static com.sun.btrace.BTraceUtils.*;
import com.sun.btrace.annotations.OnMethod;
@BTrace
public class MyCompentTrace
{
private static volatile long count;
@OnMethod(
clazz="com.djk.MyCompent",
method="<init>"
)
public static void onCreate(@Self MyCompent c)
{
// increment counter on constructor entry
count++;
}
@OnTimer(2000)
public static void toPrint() {
println(Strings.strcat("MyCompent count = ", str(count)));
}
}
这个MyCompentTrace 是看我的MyCompent类被实列化了几次.
2:计算一个方法运行的时间:
package com.djk.demo2;
public class MyTest2 {
public static void main(String[] args) throws InterruptedException
{
Thread.sleep(10000);
MyTest2 a = new MyTest2();
a.test();
}
public void test()
{
try
{
Thread.sleep(1000);
} catch (Exception e) {
}
}
}
package com.djk.demo2;
import com.sun.btrace.annotations.BTrace;
import com.sun.btrace.annotations.OnMethod;
import com.sun.btrace.annotations.TLS;
import com.sun.btrace.annotations.*;
import static com.sun.btrace.BTraceUtils.*;
@BTrace
public class MyTest2Trace
{
@TLS
private static long startTime = 0;
@OnMethod(
clazz="com.djk.demo2.MyTest2",
method="test"
)
public static void startTime()
{
startTime = timeMillis();
}
@OnMethod(
clazz="com.djk.demo2.MyTest2",
method="test",
location=@Location(Kind.RETURN)
)
public static void endTime()
{
println(strcat("the class method execute time=>", str(timeMillis()-startTime)));
println("-------------------------------------------");
}
@OnMethod(clazz = "com.djk.demo2.MyTest2", method = "test", location = @Location(Kind.RETURN))
public static void traceExecute(@ProbeClassName String name,@ProbeMethodName String method){
println(strcat("the class name=>", name));
println(strcat("the class method=>", method));
}
}
3:跟踪一个方法的入参和出参:
package com.djk.demo3;
public class Clalbean
{
private int result=0;
public int add(int i,int j)
{
result = i+j;
return i+j;
}
}
package com.djk.demo3;
import com.sun.btrace.annotations.*;
import static com.sun.btrace.BTraceUtils.*;
@BTrace
public class MyTest3Trace
{
@TLS
private static long startTime=0;
private static Object totalCount = 0;
@OnMethod(clazz="com.djk.demo3.Clalbean",method="add")
public static void startTime()
{
startTime=timeMillis();
}
@OnMethod(clazz="com.djk.demo3.Clalbean",method="add",location=@Location(Kind.RETURN))
public static void endTime()
{
println(strcat("the class method execute time=>", str(timeMillis()-startTime)));
}
@OnMethod(clazz="com.djk.demo3.Clalbean",method="add",location=@Location(Kind.RETURN))
public static void executeDetail(int i,int j,@Return int result)
{
println("====== ");
println(strcat("parameter num1:",str(i)));
println(strcat("parameter num2:",str(j)));
println(strcat("return value:",str(result)));
}
@OnMethod(clazz="com.djk.demo3.Clalbean",method="add",location=@Location(Kind.RETURN))
public static void executetest(@Self Clalbean c)
{
totalCount = get(field("com.djk.demo3.Clalbean","result"), c);
println(strcat("fafda rea:",str(totalCount)));
}
}