Last active
September 12, 2024 10:07
-
-
Save izgzhen/4cce0aab9e9768b7db649c62b02155e3 to your computer and use it in GitHub Desktop.
Timed Annotation for measuring Java method execution time
This file contains hidden or 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
compile 'org.springframework:spring-aspects:4.1.8.RELEASE' | |
compile 'org.springframework.boot:spring-boot-starter-aop:1.2.7.RELEASE' |
This file contains hidden or 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
package research.nomad; | |
import org.aspectj.lang.ProceedingJoinPoint; | |
import org.aspectj.lang.annotation.Around; | |
import org.aspectj.lang.annotation.Aspect; | |
import org.aspectj.lang.reflect.MethodSignature; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.stereotype.Component; | |
@Aspect | |
@Component | |
public class Logger { | |
private static org.slf4j.Logger logger = LoggerFactory.getLogger(Logger.class); | |
@Around("execution(* *(..)) && @annotation(research.nomad.MethodStats)") | |
public Object log(ProceedingJoinPoint point) throws Throwable | |
{ | |
long start = System.currentTimeMillis(); | |
Object result = point.proceed(); | |
logger.info("className={}, methodName={}, timeMs={},threadId={}", | |
(point.getSignature()).getDeclaringTypeName(), | |
((MethodSignature) point.getSignature()).getMethod().getName(), | |
System.currentTimeMillis() - start, | |
Thread.currentThread().getId()); | |
return result; | |
} | |
} |
This file contains hidden or 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
package research.nomad;/* Created at 2/22/20 by zhen */ | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
@Retention(RetentionPolicy.RUNTIME) | |
@Target(ElementType.METHOD) | |
public @interface Timed { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment