jps
D:softtrace-binjava>btrace 29228 PrintTimes.java D:softtrace-binjavalog.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("]");
}
}