Skip to content

Instantly share code, notes, and snippets.

@izgzhen
Last active February 23, 2020 04:43
Show Gist options
  • Save izgzhen/4cce0aab9e9768b7db649c62b02155e3 to your computer and use it in GitHub Desktop.
Save izgzhen/4cce0aab9e9768b7db649c62b02155e3 to your computer and use it in GitHub Desktop.
Timed Annotation for measuring Java method execution time
compile 'org.springframework:spring-aspects:4.1.8.RELEASE'
compile 'org.springframework.boot:spring-boot-starter-aop:1.2.7.RELEASE'
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;
}
}
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