使用byte buddy或某些库进行java评测
我正在尝试构建一个查询日志分析器,它计算每个查询的执行时间,并在查询需要更多时间时记录日志。 与包装器相比,使用AspectJ需要更多的时间。因此,如果有性能改进的余地,我想使用byte buddy或其他库
下面是我当前使用AspectJ的实现
@Aspect
public class QueryLoggerProfiler {
private static final Logger LOGGER = Logger.getLogger(QueryLoggerProfiler.class.getName());
public static final String QUERY_LOGGING_POINTCUT = "execution(* com.abc.PreparedStatement.execute*(..))";
@
Pointcut(QUERY_LOGGING_POINTCUT)
private void queryPointcut() {}
@
Around("queryPointcut()")
public Object profile(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
long start = System.currentTimeMillis();
Object output = joinPoint.proceed();
long elapsedTime = System.currentTimeMillis() - start;
if (elapsedTime >= 5) {
LOGGER.info(">>>>>>>>>>>>>>>>>>>... Going to call the method ... " + method.getName());
LOGGER.info(">>>>>>>>>>>>>>>>>>>... With parameter ... " + method.getParameters());
LOGGER.info(">>>>>>>>>>>>>>>>>>>... Method execution time: " + elapsedTime + " milliseconds.");
}
return output;
}
}
在没有性能瓶颈的情况下,是否有办法记录日志
# 1 楼答案
实际上我的代码如下
然后将拦截器分类为
# 2 楼答案
最简单的方法是将
Advice
组件与AgentBuilder
组件一起使用。这将允许您定义类似以下内容的代理:这将为指定的类型和名称应用
MyAdvice
类中定义的方法。查看javadoc forAdvice
以了解如何获取参数或如何指定要调用的方法有关如何使用Byte Buddy和JavaAgent插入代码的一般信息,请参见this article