0

I want to measure execution time of a method which is present in a client library. My application is a spring boot app and I am calling a method present in a class coming from some library. I tried running below code but it does not working. In my case, client class object is not managed by spring container which could be a reason but is there any way to get hold of that method.

@Aspect
@Component
public class LoggingAspect {
    public static final Logger logger = LoggerFactory.getLogger(LoggingAspect.class);

    @Around("execution(public * Invokers.ApiClient.execute(..))") //okhttp3.Call,
    public Object methodTimeLogger(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();

        // Get intercepted method details
        String className = methodSignature.getDeclaringType().getSimpleName();
        String methodName = methodSignature.getName();

        // Measure method execution time
        StopWatch stopWatch = new StopWatch(className + "->" + methodName);
        stopWatch.start(methodName);
        Object result = proceedingJoinPoint.proceed();
        stopWatch.stop();
        System.out.println("----------Start----------");
        // Log method execution time
        if (logger.isInfoEnabled()) {
            logger.info(stopWatch.prettyPrint());
        }
        System.out.println("----------End----------");
        return result;
    }

}
dreamcrash
  • 47,137
  • 25
  • 94
  • 117
Ashish Sharma
  • 574
  • 7
  • 18
  • Have a look at https://stackoverflow.com/questions/31416302/pointcut-expression-abcinstring-contains-unsupported-pointcut-primitive-call – dreamcrash Dec 23 '20 at 12:35
  • if you are using Spring AOP then you cannot use call joinpoint since it is not supported. Nonetheless, you can get around it by using load-time or compile-time weaving. – dreamcrash Dec 23 '20 at 12:35
  • https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#aop-aj-ltw – dreamcrash Dec 23 '20 at 12:35
  • I think this is not an easy to understand solution. If you find any example then let me know please. Thanks – Ashish Sharma Dec 24 '20 at 05:56
  • 1
    What is wrong with the Spring manual? What specifically do you not understand? If reading a manual is too much for you and you just want to copy & paste an existing solution, then maybe better do not use AOP at all, especially not in production code. You should not use something you do not understand in order to avoid the "fool with a tool" effect. Having said this, there should be plenty of sample projects out there, though. – kriegaex Dec 30 '20 at 03:48

0 Answers0