如何使用 ByteBuddy 测量通过代理检测的注释标记方法所花费的时间
HowTo use ByteBuddy to measure time taken by annotaion marked methods intrumented via agent
我正在使用 ByteBuddy 对 certian 方法进行一些性能测量。因此,我正在注释有趣的。由于方法签名不稳定,我为我的拦截器选择了通用方式:
public class ChronometryInterception {
@RuntimeType
public Object intercept(@Origin MethodHandle methodHandle, @AllArguments Object[] allArguments, @Origin Method method) throws Exception {
System.out.println("in interceptor");
long startTime = System.currentTimeMillis();
try {
return methodHandle.invoke(allArguments);
} catch (Throwable e) {
System.out.println("ex in interceptor " + e.getMessage());
throw new Exception(e);
} finally {
long elapsedTime = System.currentTimeMillis() - startTime;
System.out.println("took " + elapsedTime;
}
}
}
然后我像这样将它绑定到我的 premain() 中
ChronometryInterception chronometryInterception = new ChronometryInterception();
new AgentBuilder.Default()
.with(AgentBuilder.Listener.StreamWriting.toSystemOut())
.type(declaresMethod(isAnnotatedWith(Timed.class)))
.transform((builder, type, classLoader, module) -> builder
.method(isAnnotatedWith(Timed.class))
.intercept(MethodDelegation.to(chronometryInterception))
).installOn(instrumentation);
在侦听器流中,我可以看到带注释的 类 已被转换并且他们尝试做一些事情,但最终以 NPE 告终。使用调试器,我在 ChronometryInterception 中没有任何进展。有出路吗?谢谢!
我找到了可行的解决方案。最后,我的拦截器的方法签名不对。这个工作正常,即使有一个 ChronometryInterception 的实例:
public class ChronometryInterception {
@RuntimeType
public Object intercept(@SuperCall Callable<?> zuper) throws Exception {
long startTime = System.currentTimeMillis();
try {
return zuper.call();
} catch (Exception e) {
throw e;
} finally {
long elapsedTime = System.currentTimeMillis() - startTime;
System.out.println("took " + elapsedTime);
}
}
}
我正在使用 ByteBuddy 对 certian 方法进行一些性能测量。因此,我正在注释有趣的。由于方法签名不稳定,我为我的拦截器选择了通用方式:
public class ChronometryInterception {
@RuntimeType
public Object intercept(@Origin MethodHandle methodHandle, @AllArguments Object[] allArguments, @Origin Method method) throws Exception {
System.out.println("in interceptor");
long startTime = System.currentTimeMillis();
try {
return methodHandle.invoke(allArguments);
} catch (Throwable e) {
System.out.println("ex in interceptor " + e.getMessage());
throw new Exception(e);
} finally {
long elapsedTime = System.currentTimeMillis() - startTime;
System.out.println("took " + elapsedTime;
}
}
}
然后我像这样将它绑定到我的 premain() 中
ChronometryInterception chronometryInterception = new ChronometryInterception();
new AgentBuilder.Default()
.with(AgentBuilder.Listener.StreamWriting.toSystemOut())
.type(declaresMethod(isAnnotatedWith(Timed.class)))
.transform((builder, type, classLoader, module) -> builder
.method(isAnnotatedWith(Timed.class))
.intercept(MethodDelegation.to(chronometryInterception))
).installOn(instrumentation);
在侦听器流中,我可以看到带注释的 类 已被转换并且他们尝试做一些事情,但最终以 NPE 告终。使用调试器,我在 ChronometryInterception 中没有任何进展。有出路吗?谢谢!
我找到了可行的解决方案。最后,我的拦截器的方法签名不对。这个工作正常,即使有一个 ChronometryInterception 的实例:
public class ChronometryInterception {
@RuntimeType
public Object intercept(@SuperCall Callable<?> zuper) throws Exception {
long startTime = System.currentTimeMillis();
try {
return zuper.call();
} catch (Exception e) {
throw e;
} finally {
long elapsedTime = System.currentTimeMillis() - startTime;
System.out.println("took " + elapsedTime);
}
}
}