[TIL] 메소드 호출 시간 측정 with. AOP

YJin·2025년 5월 17일

[내배캠 Spring 6기_TIL]

목록 보기
36/56

메소드 호출 시간 측정 with. AOP

커스텀 어노테이션

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExecutionLoggable {

}

특정 메소드에만 AOP를 적용할 수 있도록 커스텀 어노테이션을 정의한다.

AOP

@Slf4j
@Aspect
@Component
public class ExecutionTimeLoggingAspect {
  @Around("@annotation(org.example.expert.domain.common.annotation.ExecutionLoggable)")
  public Object logAroundExecution(ProceedingJoinPoint joinPoint) throws Throwable{
    long start = System.currentTimeMillis();
    log.info("START EXECUTION: "+joinPoint.toString());
    try {
      return joinPoint.proceed();
    } finally {
      long finish = System.currentTimeMillis();
      long executionTime = finish-start;
      log.info("END EXECUTION: "+joinPoint.toString()+" === TIME: "+executionTime+" ms");
    }
  }
}

메소드 호출 전과 후에 System.currentTimeMillis()을 사용해 시간 차를 측정하고, 메소드 호출에 걸린 시간을 log 로 출력한다.

@Around의 포인트컷에 커스텀 어노테이션 @ExecutionLoggable을 지정하여, 해당 어노테이션이 붙은 메소드에만 AOP 가 적용되도록 한다.

profile
백엔드 개발도 락이다

0개의 댓글