java - 分析 Java Spring 应用程序

标签 java spring profiling

我有一个 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/

相关文章:

java - 如何编写使用匿名内部类(例如PreparedStatementSetter)的类的JUnit测试?

java - 在代码中多次访问相同的属性(对象的成员)

java - Java Web 应用程序中的搜索集成

java - 云应用程序使用多个架构还是单个架构?

node.js - 如何可视化 NodeJS .cpuprofile

java - 如何获取日志文件中的 hibernate 日志

java - setAdapter() 使应用程序崩溃

spring - 未使用 jpaFlowExecutionListener 关闭数据库连接

java - 为在远程 Linux 机器上运行的 Java 应用程序推荐一个分析器?

java - JProfiler 挂起并显示 "waiting for a JProfiler GUI to connect"消息