views:

1094

answers:

5

Hi,

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 franck, i just dont understand how it's work, and when i profiling my application ( launch the server in profiling mode ) , it's take forever to do nothing. ( well, not what i expect : a simple output of execution time )

So, i do the profiling my myself my system time. ( add a line a the begginning and the ending of methods. ) It's not so bad.

My question is : i want to mesure 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 dont have some much unit test, cant 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.

Thanks,

Antoine

+1  A: 

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.

Nick Holt
+4  A: 

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

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.

Gregory Mostizky
Word of warning here, this is tied to the Spring StopWatch which specifies execution time in ms. You can't change it to ns AFAIK.
Jon
+1  A: 

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.

Csaba_H
+1  A: 

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.

Mike Dunlavey
A: 

The right way to do this is to use JXInsight, ;-), which out performs all of the above, is much more extensible and powerful, uses AOP that can be easily extended without any coding, and comes with over 700+ pre-built technology extensions including numerous ones for Spring.

Please check out my blog http://williamlouth.wordpress.com and company site http://www.jinspired.com

William Louth