我有一个 Spring 应用程序,我认为它存在一些瓶颈,所以我想使用分析器来运行它,以测量哪些功能需要多少时间。对我应该如何做有什么建议吗?
我正在运行 STS,该项目是一个 maven 项目,我正在运行 Spring 3.0.1
最佳答案
我已经使用 Spring AOP 完成了这项工作。
有时我需要有关在我的项目中执行某些方法(例如 Controller 的方法)需要多少时间的信息。
在 servlet xml 中我放了
<aop:aspectj-autoproxy/>
另外,我需要为方面创建类:
@Component
@Aspect
public class SystemArchitecture {
@Pointcut("execution(* org.mywebapp.controller..*.*(..))")
public void businessController() {
}
}
和探查器方面:
@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()));
}
}
就是这样。 当我从我的 webapp 请求页面时,有关方法执行时间和 JVM 内存使用情况的信息会打印在我的 webapp 的日志文件中。
关于java - 分析 Java Spring 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2475682/