使用 Spring AOP 进行方法分析(基本执行时间)

发布于 2024-08-04 04:21:01 字数 578 浏览 8 评论 0原文

我正在寻找一种功能或软件,它可以让我轻松地分析我的方法执行时间并选择通过包过滤器分析的内容。

我知道,它是探查器 101。 我使用 TPTP 分析器。但我对此并不满意。坦率地说,我只是不明白它是如何工作的,当我分析我的应用程序(以分析模式启动服务器)时,它需要永远不执行任何操作。 (好吧,不是我所期望的:执行时间的简单输出)

所以我自己使用系统时间进行分析(在方法的开头和结尾添加一行)。情况还不错。

我的问题是:我想用 Spring AOP 测量方法调用之前和之后的系统时间,你能给我指导吗?这是一个好/坏主意?代码库相当大,而且我们没有很多单元测试,这不是很“危险”吗?

我不是要求代码,我想我可以通过这种链接自己完成: http://static.springsource.org/spring/docs/ 2.5.x/reference/aop.html

但是如果你有一个很好的教程(以前从未做过 AOP,只是知道概念),我会接受。

I'm looking for a feature or software, who will allow me to easily profile my method execution time and choose what to profile by package filter.

I know, it's profiler 101.
I use the TPTP profiler. But I'm not happy with it. To be frank I just don't understand how it works, and when I profile my application (launch the server in profiling mode), it takes forever to do nothing. (well, not what I expect: a simple output of execution time)

So I do the profiling myself with system time (add a line at beginning and at ending of methods). It's not so bad.

My question is : I want to measure system time before and after a method call with Spring AOP, can you give me direction? It's a good / bad idea ? The code base is pretty large, and we don't have many unit tests, can't it be "dangerous" ?

I'm not asking for code, I think I can do it myself with this kind of link :
http://static.springsource.org/spring/docs/2.5.x/reference/aop.html

But if you have a nice tutorial ( never done AOP before, just know the concept), I take it.

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

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

发布评论

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

评论(4

牵你的手,一向走下去 2024-08-11 04:21:01

Spring 对此有内置支持。

我试图寻找教程,但令人惊讶的是我没有找到教程,所以我将尝试在这里解释它。 (编辑:我将此示例添加到我的博客 这里

基本上,您需要的是像这样扩展CustomizedTraceInterceptor类:

public class MyTraceInterceptor extends CustomizableTraceInterceptor {

  protected void writeToLog(Log logger, String message, Throwable ex) {
    if (ex != null) {
        logger.info(message, ex);
    } else {
        logger.info(message);
    }
  }


  protected boolean isInterceptorEnabled(MethodInvocation invocation, Log logger) {
    return true;
  }
}

该类包装您的bean并输出方法调用信息,包括参数、返回值和执行时间到日志。通过更改 writeToLog() 方法,您可以控制要输出数据的位置以及严重程度。

现在您需要一些 XML 来实际选择要包装的 bean:

    <!-- Tracing -->

<bean name="traceInterceptor" class="MyTraceInterceptor" dependency-check="none">

    <property name="enterMessage" value="ENTER: $[targetClassShortName].$[methodName]($[arguments])"/>

    <property name="exitMessage"

              value="EXIT: $[targetClassShortName].$[methodName]() : $[invocationTime]ms : $[returnValue]"/>

</bean>

<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" dependency-check="none">

    <property name="beanNames" value="*RequestListener,*Notifier"/>

    <property name="proxyTargetClass" value="true"/>

    <property name="interceptorNames">

        <list>

            <value>traceInterceptor</value>

        </list>

    </property>

    <property name="order" value="2"/>

</bean>

基本上,您可以在“beanNames”中使用通配符定义要包装的 bean,并且“order”控制包装的顺序 - 如果您没有其他 AOP类,您可以将其删除。如果更改 EnterMessage 和 exitMessage 属性,您还可以更改输出格式。

这应该足以让您开始。如果您需要澄清,请随时询问。

There is a built in support for that in Spring.

I tried to look for tutorial but surprisingly I have not found one so I will try to explain it here. (EDIT: I added this example to my blog here)

Basically what you need is to extend CustomizableTraceInterceptor class like this:

public class MyTraceInterceptor extends CustomizableTraceInterceptor {

  protected void writeToLog(Log logger, String message, Throwable ex) {
    if (ex != null) {
        logger.info(message, ex);
    } else {
        logger.info(message);
    }
  }


  protected boolean isInterceptorEnabled(MethodInvocation invocation, Log logger) {
    return true;
  }
}

This class wraps around your beans and outputs method call information, including parameters, return values and execution time to a log. By changing writeToLog() method you control where you want to output the data and at what severity.

Now you need some XML to actually select which beans you are going to wrap:

    <!-- Tracing -->

<bean name="traceInterceptor" class="MyTraceInterceptor" dependency-check="none">

    <property name="enterMessage" value="ENTER: $[targetClassShortName].$[methodName]($[arguments])"/>

    <property name="exitMessage"

              value="EXIT: $[targetClassShortName].$[methodName]() : $[invocationTime]ms : $[returnValue]"/>

</bean>

<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" dependency-check="none">

    <property name="beanNames" value="*RequestListener,*Notifier"/>

    <property name="proxyTargetClass" value="true"/>

    <property name="interceptorNames">

        <list>

            <value>traceInterceptor</value>

        </list>

    </property>

    <property name="order" value="2"/>

</bean>

Basically you define the beans you want to wrap with a wildcard in "beanNames" and "order" controls the ordering of wrapping - if you don't have other AOP classes you can remove it. You can also change the format of output if you change enterMessage and exitMessage properties.

That should be enough to get you started. If you need clarifications don't hesitate to ask.

北斗星光 2024-08-11 04:21:01

AOP 方法可以工作,但取决于您计划如何记录信息,其本身可能会影响性能 - 只需要注意这一点,确保日志记录尽可能高效,并确保您的错误在您的方面进行正确处理。

您可能还想查看 Visual VM - 这个工具给我留下了深刻的印象,它很容易使用并且能够为我提供我上次使用它时所需的信息。

The AOP approach would work but depending on how you're planning to log the information could itself effect performance - just be aware of that, make sure the logging is efficient as possible and make sure your error handling it on-point in your aspect.

You might also want to look as Visual VM - I was impressed by this tool, it's easy to use and was able to provide me with just the information I needed when I last used it.

∝单色的世界 2024-08-11 04:21:01

除了 VisualVM(Nick 提到的另一个优秀的(并且免费开发)软件)是 Oracle JRockit 任务控制。其管理控制台有能力 对某些方法进行简单的分析调用(另外还有更多的分析选项,并且肯定比 TPTP 更快)。

与测量方法调用之前/之后的系统时间一样:基本上它可以工作,但有一些小“缺陷”(例如后台应用程序可以“改变”结果)。

就我个人而言,我会首先选择 VisualVM 或 JRockit Mission Control。

Beside VisualVM which Nick mentioned another good (and free for development) piece of software is Oracle JRockit Mission Control. Its Management Console has the ability to simple profile calls of some methods (plus there are more profiling options and definitely faster than TPTP).

As with measuring system time before / after method calls: basically it works but has some minor "flaws" (for example background applications can 'alter' the result).

Personally I would go with VisualVM or JRockit Mission Control first.

人间☆小暴躁 2024-08-11 04:21:01

“Profiler 101”的问题在于它体现了许多想法,其理由更多的是流行程度而不是合理的想法。

最重要的想法是发现性能问题的最佳方法是测量性能。

这是自上而下的思维,就像试图通过查看每个部门的预算来发现政府中的浪费一样。另一种方法是自下而上的方法,例如随机选择几个金钱或时间单位,并(最重要的是)完全确定花费每个单位的原因。

如果有浪费,这会很快找到它。原因很简单,如果有一定百分比(例如 40%)被浪费,那么该百分比的样本(平均)将准确地向您显示它是如何被浪费的。

这是我与语言无关的方法

添加:你可能会认为像 40% 这样的大比例是不现实的,因为你无法想象它,但这完全有可能

The problem with "profiler 101" is that it embodies lots of ideas whose justification is more a matter of popularity than sound thinking.

The biggest such idea is that the best way to find performance problems is by measuring performance.

That is top-down thinking, and it is like trying to find waste in government by looking at each department's budget. An alternative is a bottom-up approach, such as picking several random units of either money or time and (most important) determining fully why each one is being spent.

If there is waste, this will quickly find it. The reason is simple, if some percent (say 40%) is being wasted, then that percent of samples (on average) will show you precisely how it is being wasted.

This is the language-agnostic method I use.

ADDED: You may think a large fraction like 40% is unrealistic, because you can't imagine it, but it's entirely possible.

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