Home » Java » java – Is it possible to measure time performance of the functions that are called inside jar of some library?-Exceptionshub

java – Is it possible to measure time performance of the functions that are called inside jar of some library?-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am using some library in my maven based project. The jar of this library may/may not contain some other dependent libraries.

I am calling xyz function of this library. There is a requirement where I need to check time taken by each of the functions that are called inside xyz function. These functions may/ may not be publicly accessible.

Any idea how to do this ? Would aspect work in such scenario ?

How to&Answers:

Yes, aspects should work. Here is the small example for spring package

private final Logger log = LoggerFactory.getLogger(this.getClass());


@Pointcut("within(@org.springframework.stereotype.Repository *)")
public void springBeanPointcut() {
}


@Around("springBeanPointcut()")
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
    if (log.isDebugEnabled()) {
        log.debug("Enter: {}.{}()",
                joinPoint.getSignature().getDeclaringTypeName(),
                joinPoint.getSignature().getName());
    }
    try {
        Object result = joinPoint.proceed();
        if (log.isDebugEnabled()) {
            log.debug("Exit: {}.{}()",
                    joinPoint.getSignature().getDeclaringTypeName(),
                    joinPoint.getSignature().getName());
        }
        return result;
    } catch (IllegalArgumentException e) {
        log.error("Error:{}.{}()",
                joinPoint.getSignature().getDeclaringTypeName(),
                joinPoint.getSignature().getName());

        throw e;
    }
}

Configure your logger format for logging millisecond time log, or just put your logic to compute the time taken.