使用Spring AOP进行方法分析(基本执行时间) [英] Do method profiling ( Basic execution time ) With Spring AOP

查看:121
本文介绍了使用Spring 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.

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

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.

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

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" ?

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

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

但如果你有一个不错的教程(之前从未完成AOP,只知道概念),我接受它。

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

推荐答案

内置支持在春天。

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

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

基本上你需要的是像这样扩展CustomizableTraceInterceptor类:

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;
  }
}

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

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.

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

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>

基本上你在beanNames和order控件中定义要用通配符包装的bean包装的顺序 - 如果你没有其他AOP类,你可以删除它。如果更改enterMessage和exitMessage属性,也可以更改输出格式。

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.

这篇关于使用Spring AOP进行方法分析(基本执行时间)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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