分析Java Spring应用程序

nik*_*ers 15 java spring profiling

我有一个Spring应用程序,我认为它有一些瓶颈,所以我想用一个分析器来运行它来测量哪些函数花了多少时间.有关我应该如何做的任何建议?

我正在运行STS,该项目是一个maven项目,我正在运行Spring 3.0.1

Yur*_*kin 17

我用Spring AOP完成了这个.

有时我需要一个关于在我的项目中执行某些方法需要多长时间的信息(例子中的Controller方法).

在servlet xml中我放了

<aop:aspectj-autoproxy/>
Run Code Online (Sandbox Code Playgroud)

另外,我需要为方面创建类:

@Component
@Aspect
public class SystemArchitecture {

    @Pointcut("execution(* org.mywebapp.controller..*.*(..))")
    public void businessController() {
    }
}
Run Code Online (Sandbox Code Playgroud)

和探查器方面:

@Component
@Aspect
public class TimeExecutionProfiler {

    private static final Logger logger = LoggerFactory.getLogger(TimeExecutionProfiler.class);

    @Around("org.mywebapp.util.aspects.SystemArchitecture.businessController()")
    public Object profile(ProceedingJoinPoint pjp) throws Throwable {
        long start = System.currentTimeMillis();
        logger.info("ServicesProfiler.profile(): Going to call the method: {}", pjp.getSignature().getName());
        Object output = pjp.proceed();
        logger.info("ServicesProfiler.profile(): Method execution completed.");
        long elapsedTime = System.currentTimeMillis() - start;
        logger.info("ServicesProfiler.profile(): Method execution time: " + elapsedTime + " milliseconds.");

        return output;
    }

    @After("org.mywebapp.util.aspects.SystemArchitecture.businessController()")
    public void profileMemory() {
        logger.info("JVM memory in use = {}", (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()));
    }
}
Run Code Online (Sandbox Code Playgroud)

就这样.当我从webapp请求页面时,有关方法执行时间和JVM内存使用情况的信息打印在我的webapp日志文件中.