分析Java Spring应用程序

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

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

我用Spring AOP完成了这个。

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

在servlet xml中我放了

 

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

 @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日志文件中。

我建议使用VisualVM进行常规应用程序分析。 它在版本1.6_10的JDK中可用,并且比Eclipse TPTP快得多且可用。

如果您的Spring应用程序在应用程序服务器(例如Tomcat)中工作,您可以尝试将其部署到tc Server开发人员版本(可在STS下载中使用 )。 它具有有趣的监控function。

您可以使用开源Java分析器,例如Profiler4J:

http://profiler4j.sourceforge.net/

或者Netbeans带有一个内置的分析器,Eclipse也有分析function,但我发现Profiler4J更容易使用,因为它有一个很好的图表显示你最耗时的方法。

这在STS(eclipse)中运行良好,只需按照网站上的说明操作即可。

我们开发了一个基于JMX和Spring AOP的@Profiled注释,它可以进行生产监控(主动调用,调用计数,调用期​​间花费的时间,exception计数等)。 度量标准通过JMX公开,可以通过Visual VM / JConsole和监控系统收集; 我们开发了Hyperic HQ插件。

这个@profiled注释与许多其他JMX附加组件一起打包,以便于监视常见组件(dbcp,util.concurrent,cxf,jms等),并在http://code.google.com/上的商业友好Apache软件许可证下提出。 p / xebia-france / wiki / XebiaManagementExtras 。

希望这可以帮助,

Cyrille(Xebia)

我喜欢JRat ,虽然像profiler4j似乎没有积极开发。 无论如何,它使用起来很简单。

以下是推荐工具和技术的一般性讨论 。

基本上,假设您想要了解如何更快地创建应用程序,那么您应该首先考虑function需要多长时间。 这是一种自上而下的方法。

有一种自下而上的方法,当你想到它同样自然。 这不是要问时间,而是要问问它在做什么,主要是什么,以及它为什么这样做。

Yuri的答案在顶部(选定的答案)的一个修改版本自动计算持续时间的总和并安排他们desc。 总计仅在最后打印。 可能会为你节省10分钟。

  @Component @Aspect public class SystemArchitecture { @Pointcut("execution(* erp..*.*(..))") public void businessController() { } @Pointcut("execution(* TestMain..*.*(..))") public void theEnd() { } } @Component @Aspect public class TimeExecutionProfiler { static Hashtable ht = new Hashtable(); @Around("profiler.SystemArchitecture.businessController()") public Object profile(ProceedingJoinPoint pjp) throws Throwable { long start = System.nanoTime(); Object output = pjp.proceed(); long elapsedTime = System.nanoTime() - start; String methodName = pjp.getSignature().toString(); if (ht.get(methodName) == null) { ht.put(methodName, elapsedTime); } else { ht.put(methodName, ht.get(methodName) + elapsedTime); } // System.out.println(methodName + " : " + elapsedTime + " milliseconds."); return output; } @After("profiler.SystemArchitecture.theEnd()") public void profileMemory() { List keys = Arrays.asList(ht.keySet().toArray()); java.util.Collections.sort(keys, new Comparator() { @Override public int compare(Object arg0, Object arg1) { return ht.get(arg1).compareTo(ht.get(arg0)); } }); System.out.println("totals Used:"); for (Object name : keys) { System.out.println("--" + name + " : " + (ht.get(name) / 1000000)); } System.out.println("JVM memory in use = " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())); } }