I am basically trying to create an annotation that provides me the execution time of any method. While running the following code, I am not sure if it is hitting the method. Also, intelliJ suggests that "advice advises no method".
This is what I want it to do -
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.stereotype.Component;
@Aspect
@Component
@Slf4j
@ConditionalOnExpression("${aspect.enabled:true}")
public class ExecutionTimeTracker {
@Around(value = "@annotation(TrackExecutionTime)")
public Object executionTime(ProceedingJoinPoint point) throws Throwable {
long startTime = System.currentTimeMillis();
Object object = point.proceed();
long endtime = System.currentTimeMillis();
log.info(point.getSignature().getDeclaringTypeName() +
"." + point.getSignature().getName() +
" >> Took : " + (endtime - startTime) + "mills");
return object;
}
}
This is my @interface
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Target({ METHOD })
@Retention(RUNTIME)
public @interface TrackExecutionTime {
}
This is the UT to test this -
import org.junit.jupiter.api.Test;
public class ExecutionTimeTrackerTest {
@Test
@TrackExecutionTime
public void someMethod(){
int[] randomNumbers = {582, 15, 596, 827, 779, 426, 153, 557, 394, 12};
Arrays.sort(randomNumbers);
int index = Arrays.binarySearch(randomNumbers, 827);
Assertions.assertEquals(9, index);
}
}
I tried changing @Around(value = "@annotation(TrackExecutionTime)") to different locations, tried executing with setting up different log levels too. Nothing seem to work.