首页  :: 新随笔  :: 管理

btrace

Posted on 2014-04-24 23:31  季枫  阅读(346)  评论(0编辑  收藏  举报

jps

D:\soft\btrace-bin\java>btrace 29228 PrintTimes.java D:\soft\btrace-bin\java\log.log

 

 

 

import static com.sun.btrace.BTraceUtils.name;
import static com.sun.btrace.BTraceUtils.print;
import static com.sun.btrace.BTraceUtils.println;
import static com.sun.btrace.BTraceUtils.probeClass;
import static com.sun.btrace.BTraceUtils.probeMethod;
import static com.sun.btrace.BTraceUtils.str;
import static com.sun.btrace.BTraceUtils.strcat;
import static com.sun.btrace.BTraceUtils.timeMillis;

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.TLS;

/**
* 监控方法耗时
*
* @author jerry
*/
@BTrace
public class PrintTimes {

/**
* 开始时间
*/
@TLS
private static long startTime = 0;

/**
* 方法开始时调用
*/
@OnMethod(clazz = "/.*/", method = "/.+/")
public static void startMethod() {
startTime = timeMillis();
}

/**
* 方法结束时调用<br>
* Kind.RETURN这个注解很重要
*/
@SuppressWarnings("deprecation")
@OnMethod(clazz = "/.*/", method = "/.+/", location = @Location(Kind.RETURN))
public static void endMethod() {

print(strcat(strcat(name(probeClass()), "."), probeMethod()));
print(" [");
print(strcat("Time taken : ", str(timeMillis() - startTime)));
println("]");
}
}

智读 | 成都会领科技有限公司官网 | 智读App下载 | 每天听本书的博客 | |