Last active
January 26, 2024 22:27
-
-
Save susimsek/94e9be3886a447447f22d9aea6f6371e to your computer and use it in GitHub Desktop.
Spring Boot Measure Execution Time using Spring AOP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
aspect.enabled=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Aspect | |
@Component | |
@Slf4j | |
@ConditionalOnExpression("${aspect.enabled:true}") | |
public class ExecutionTimeAdvice { | |
@Around("@annotation(TrackExecutionTime)") | |
public Object executionTime(ProceedingJoinPoint point) throws Throwable { | |
long startTime = System.currentTimeMillis(); | |
Object object = point.proceed(); | |
long endtime = System.currentTimeMillis(); | |
log.info("Class Name: "+ point.getSignature().getDeclaringTypeName() +". Method Name: "+ point.getSignature().getName() + ". Time taken for Execution is : " + (endtime-startTime) +"ms"); | |
return object; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Service | |
public class MessageService { | |
@TrackExecutionTime | |
@Override | |
public List<AlbumEntity> getMessage() { | |
return "Hello message"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-aop</artifactId> | |
</dependency> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Target(ElementType.METHOD) | |
@Retention(RetentionPolicy.RUNTIME) | |
public @interface TrackExecutionTime { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a lot