Spring AOP Method Profiler

This is a poor man’s application profiler. Note, that this profiler is going to be limited, since it is running inside the application, but also nice that it is running inside the application. Anyway, I find it helpful to measure individual service calls against other service calls. One could even add a limit into this method, if it takes longer than x, send me an email because something is wrong… Though, I’m pretty sure this code is not to great for production builds.

Enough babble;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class TimeExecutionProfiler {
private static final Logger log = LoggerFactory.getLogger(TimeExecutionProfiler.class);

@Around(“SystemArchitecture.serviceOperation()”)
public Object profile(ProceedingJoinPoint pjp) throws Throwable {
long start = System.currentTimeMillis();
Object output = pjp.proceed();
long elapsedTime = System.currentTimeMillis() - start;
log.info(“ServicesProfiler.profile(): Method execution time: “ + elapsedTime + ” milliseconds.“);
return output;
}

@After(“SystemArchitecture.serviceOperation()”)
public void profileMemory() {
log.info(“JVM memory in use = “
+ ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1048576) + “ MB”);
}
}

Published by and tagged Code using 169 words.