分析 Java Spring 应用程序

发布于 2024-08-25 23:15:49 字数 1539 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

北陌 2024-09-01 23:15:49

我已经使用 Spring AOP 完成了此操作。

有时我需要有关执行项目中某些方法(示例中的控制器方法)需要多少时间的信息。

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

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

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).

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() {
    }
}

And profiler aspect:

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

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.

和影子一齐双人舞 2024-09-01 23:15:49

我建议使用 VisualVM 进行一般应用程序分析。它在 JDK 1.6_10 版本中可用,恕我直言,它比 Eclipse TPTP 更快、更可用。

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

更新 2019.02.22。 :更新了 VisualVM url(感谢@Jeff)和 tc 服务器信息。就我个人而言,我目前使用 Glowroot 来监视在应用程序服务器中运行的 Spring 应用程序。

I recommend VisualVM for general application profiling. It is available in the JDK from version 1.6_10, and imho much faster and usable than Eclipse TPTP.

(If your Spring application works in an application server (e.g. Tomcat) you could try to deploy it to the tc Server developer edition (available in the STS downloads). It has interesting monitoring capabilities. It seems that tc Server developer edition is not maintained anymore.)

UPDATE 2019.02.22.: updated VisualVM url (thanks for @Jeff) and tc Server information. Personally I currently use Glowroot for monitoring Spring applications running in an application server.

变身佩奇 2024-09-01 23:15:49

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

http://profiler4j.sourceforge.net/

或Netbeans 附带了一个内置的分析器,Eclipse 也具有分析功能,但是我发现 Profiler4J 更容易使用,因为它有一个很好的图表,向您显示最耗时的方法。

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

You can use an open source java profiler such as Profiler4J:

http://profiler4j.sourceforge.net/

or Netbeans comes with a built in profiler and Eclipse also has profiling capabilities, however I found Profiler4J easier to use since it has a nice graph showing you the most time consuming methods.

This works well in STS (eclipse), just follow the instructions on the site.

秋凉 2024-09-01 23:15:49

以下是一般讨论,其中包含推荐的工具和工具。技术。

基本上,如果您想了解如何使应用程序更快,那么您应该从测量函数需要多长时间开始,这是完全自然的假设。
这是一种自上而下的方法。

有一种自下而上的方法,如果你仔细想想,它也是很自然的。这不是问时间,而是主要问它在做什么,以及为什么这样做。

Here's a general discussion with recommended tools & techniques.

Basically, it is entirely natural to assume if you want to find out how to make an app faster, that you should start by measuring how long functions take.
That's a top-down approach.

There's a bottom-up approach that when you think about it is just as natural. That is not to ask about time, but to ask what it is doing, predominantly, and why it is doing it.

半步萧音过轻尘 2024-09-01 23:15:49

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

此 @profiled 注释与许多其他 JMX 附加功能打包在一起,以简化对常见组件(dbcp、util.concurrent、cxf、jms 等)的监控,并在商业友好的 Apache 软件许可证下建议,网址为 http://code.google.com/p/xebia-france/wiki/XebiaManagementExtras

希望这有帮助,

Cyrille (Xebia)

We developed a JMX & Spring AOP based @Profiled annotation which does production monitoring (active invocations, invocations count, time spent during invocations, exceptions count, etc) . Metrics are exposed via JMX and can be collected via Visual VM / JConsole and by monitoring systems ; we developed an Hyperic HQ Plugin.

This @profiled annotation is packaged with many other JMX extras to ease monitoring of common components (dbcp, util.concurrent, cxf, jms, etc) and proposed under a business friendly Apache Software License at http://code.google.com/p/xebia-france/wiki/XebiaManagementExtras .

Hope this helps,

Cyrille (Xebia)

吃素的狼 2024-09-01 23:15:49

我喜欢 JRat,尽管与 profiler4j 一样,它似乎并未得到积极开发。无论如何,它使用起来很简单。

I liked JRat, although like profiler4j it doesn't seem to be actively developed. In any case, it was simple to use.

我要还你自由 2024-09-01 23:15:49

尤里(Yuri)的答案(所选答案)的稍微修改版本,自动计算持续时间的总数并按顺序排列它们。总计仅在最后打印。
可能会为您节省 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<String, Long>  ht  = new Hashtable<String, Long>();

        @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<Object> keys = Arrays.asList(ht.keySet().toArray());
            java.util.Collections.sort(keys, new Comparator<Object>()
            {

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

A bit modified version of Yuri's answer on top (the selected answer) that auto calculates the totals of durations and arranges them desc. The totals gets printed in the end only.
Might save you 10 mns.

    @Component
    @Aspect
    public class SystemArchitecture
    {

         @Pointcut("execution(* erp..*.*(..))")
         public void businessController()
         {
         }

         @Pointcut("execution(* TestMain..*.*(..))")
         public void theEnd()
         {
         }
    }



    @Component
    @Aspect
    public class TimeExecutionProfiler
    {

        static Hashtable<String, Long>  ht  = new Hashtable<String, Long>();

        @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<Object> keys = Arrays.asList(ht.keySet().toArray());
            java.util.Collections.sort(keys, new Comparator<Object>()
            {

                @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()));
        }
    }
笑饮青盏花 2024-09-01 23:15:49

您始终可以使用与 Java 捆绑在一起的 Java Mission Controls Flight Recorder 来分析代码执行情况

You could always use Java Mission Controls Flight Recorder that is bundled with Java, to profile your code execution

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文