示例地址 : http://kenai.com/projects/btrace/sources/hg/show/samples
1. 示例代码
示例代码定义了Counter计数器,有一个add()方法,每次增加随机值,总数保存在totalCount属性中。
- package com.learnworld;
- import java.util.Random;
- public class BTraceTest {
- public static void main(String[] args) throws Exception {
- Random random = new Random();
- // 计数器
- Counter counter = new Counter();
- while (true) {
- // 每次增加随机值
- counter.add(random.nextInt(10));
- Thread.sleep(1000);
- }
- }
- }
- package com.learnworld;
- public class Counter {
- // 总数
- private static int totalCount = 0;
- public int add(int num) throws Exception {
- totalCount += num;
- sleep();
- return totalCount;
- }
- public void sleep() throws Exception {
- Thread.sleep(1000);
- }
- }
2. 常见使用场景
下面通过几个常见使用场景演示如何使用BTrace脚本。
1) 获取add()方法参数值和返回值。
- import com.sun.btrace.annotations.*;
- import static com.sun.btrace.BTraceUtils.*;
- @BTrace
- public class TracingScript {
- @OnMethod(
- clazz="com.learnworld.Counter",
- method="add",
- location=@Location(Kind.RETURN)
- )
- public static void traceExecute(int num,@Return int result){
- println("====== ");
- println(strcat("parameter num: ",str(num)));
- println(strcat("return value:",str(result)));
- }
- }
2) 定时获取Counter类的属性值totalCount。
- import com.sun.btrace.annotations.*;
- import static com.sun.btrace.BTraceUtils.*;
- @BTrace
- public class TracingScript {
- private static Object totalCount = 0;
- @OnMethod(
- clazz="com.learnworld.Counter",
- method="add",
- location=@Location(Kind.RETURN)
- )
- public static void traceExecute(@Self com.learnworld.Counter counter){
- totalCount = get(field("com.learnworld.Counter","totalCount"), counter);
- }
- @OnTimer(1000)
- public static void print(){
- println("====== ");
- println(strcat("totalCount: ",str(totalCount)));
- }
- }
3) 获取add方法执行时间。
- import com.sun.btrace.annotations.*;
- import static com.sun.btrace.BTraceUtils.*;
- @BTrace
- public class TracingScript {
- @TLS private static long startTime = 0;
- @OnMethod(
- clazz="com.learnworld.Counter",
- method="add"
- )
- public static void startExecute(){
- startTime = timeNanos();
- }
- @OnMethod(
- clazz="com.learnworld.Counter",
- method="add",
- location=@Location(Kind.RETURN)
- )
- public static void endExecute(@Duration long duration){
- long time = timeNanos() - startTime;
- println(strcat("execute time(nanos): ", str(time)));
- println(strcat("duration(nanos): ", str(duration)));
- }
- }
4) 获取add()方法调用方法sleep()次数。
- import com.sun.btrace.annotations.*;
- import static com.sun.btrace.BTraceUtils.*;
- @BTrace
- public class TracingScript {
- private static long count;
- @OnMethod(
- clazz="/.*/",
- method="add",
- location=@Location(value=Kind.CALL, clazz="/.*/", method="sleep")
- )
- public static void traceExecute(@ProbeClassName String pcm, @ProbeMethodName String pmn,
- @TargetInstance Object instance, @TargetMethodOrField String method){
- println("====== ");
- println(strcat("ProbeClassName: ",pcm));
- println(strcat("ProbeMethodName: ",pmn));
- println(strcat("TargetInstance: ",str(classOf(instance))));
- println(strcat("TargetMethodOrField : ",str(method)));
- count++;
- }
- @OnEvent
- public static void getCount(){
- println(strcat("count: ", str(count)));
- }
- }