Spring/Spring boot

시간 측정하는 간단한 aop

원2 2022. 12. 22. 12:01
728x90
반응형

시스템이 느리거나 각 로직별 시간 측정이 필요한 경우

aop 를 사용하여 측정 가능

노가다 안해도 됌

package com.example.whystd.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

// Aspect 를 적어줘야 aop로 사용가능
// Component 사용하면 Component 스캔되어 빈 등록
@Aspect
@Component
public class TimeTraceAop {

    @Around("execution(* com.example.whystd..*(..))")
    public Object execute(ProceedingJoinPoint joinPoint) throws Throwable {

        long start = System.currentTimeMillis();
        System.out.println("START : " + joinPoint.toString());
        try {
            //joinPoint.proceed() 다음 메소드로 진행
            return joinPoint.proceed();

        } finally {
            long finish = System.currentTimeMillis();
            long timeMs = finish - start;
            System.out.println("END : " + joinPoint.toString() + " " + timeMs + "ms");
        }
    }
}

around 

 

안에 있는 건 패키지 하위의 모든 것을 말함

예를 들어 현재 상태에서 service 만 체크 하려면 

@Around("execution(* com.example.whystd.service.*(..))")

이런식으로 하면 된다,


 

 

직접 빈을 등록한다면 아래처럼 하면 끝

/*    // aop 직접등록법
    @Bean
    public TimeTraceAop timeTraceAop() {
        return new TimeTraceAop();
    }*/

 

그 후 어떤 로직을 실행하면 아래처럼 뜬다

시간이 오래걸리는 것을 찾아 병목현상을 해결하자ㅇㅇ

 

728x90
반응형