Do method profiling ( Basic execution time ) With Spring AOP

房东的猫 提交于 2019-12-03 06:57:45
Gregory Mostizky

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.

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.

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.

Mike Dunlavey

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!