对Java Spring应用程序进行性能分析 [英] Profiling a Java Spring application

查看:258
本文介绍了对Java Spring应用程序进行性能分析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring应用程序,我认为它存在一些瓶颈,因此我想用一个探查器运行它来测量哪些功能需要花费多少时间.关于我应该如何做的任何建议?

I have a Spring application that I believe has some bottlenecks, so I'd like to run it with a profiler to measure what functions take how much time. Any recommendations to how I should do that?

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

I'm running STS, the project is a maven project, and I'm running Spring 3.0.1

推荐答案

我已经使用Spring AOP做到了这一点.

I've done this using Spring AOP.

有时候,我需要有关在项目中执行某些方法(例如,控制器的方法)花费多少时间的信息.

Sometime I need an information about how much time does it take to execute some methods in my project (Controller's method in example).

我放入servlet xml

In servlet xml I put

<aop:aspectj-autoproxy/>

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

Also, I need to create the class for aspects:

@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()));
    }
}

仅此而已. 当我从Web应用程序请求页面时,有关方法执行时间和JVM内存使用情况的信息会在我的Web应用程序的日志文件中打印出来.

That's all. When I request a page from my webapp, the information about method executing time and JVM memory usage prints out in my webapp's log file.

这篇关于对Java Spring应用程序进行性能分析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆