Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test] 메서드 실행 시간 측정 AOP 구현 #48

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/main/java/gdsc/cau/puangbe/common/annotation/ExeTimer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package gdsc.cau.puangbe.common.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ExeTimer {
}
Comment on lines +8 to +11
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ANNOTATION_TYPE이 추가로 붙은 이유가 있을까요??

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

메서드만 넣을까 하다가 @PuangUser에 대해 적용하고 싶은 경우가 생길까봐 추가했습니다!

37 changes: 37 additions & 0 deletions src/main/java/gdsc/cau/puangbe/common/util/ExecutionTimer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package gdsc.cau.puangbe.common.util;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

@Slf4j
@Aspect
@Component
public class ExecutionTimer {
// 조인포인트를 어노테이션으로 설정
@Pointcut("@annotation(gdsc.cau.puangbe.common.annotation.ExeTimer)")
private void timer(){};

// 메서드 실행 전,후로 시간을 공유해야 하기 때문
@Around("timer()")
public void AssumeExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {

StopWatch stopWatch = new StopWatch();

stopWatch.start();
joinPoint.proceed(); // 조인포인트의 메서드 실행
stopWatch.stop();

long totalTimeMillis = stopWatch.getTotalTimeMillis();

MethodSignature signature = (MethodSignature) joinPoint.getSignature();
String methodName = signature.getMethod().getName();

log.info("실행 메서드: {}, 실행시간 = {}ms", methodName, totalTimeMillis);
}
Comment on lines +16 to +36
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

범용성 있게 쓸 수 있겠네요.

}
Loading