views:

424

answers:

5

Hi guys,

I have a Spring application that I believe has some bottlenecks, so I'd like to run it with a profiler to measure what functions take how much time. Any recommendations to how I should do that?

I'm running STS, the project is a maven project, and I'm running Spring 3.0.1

Cheers

Nik

A: 

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.

Neal Donnan
Thanks for the tip. It seems that the last beta of Profiler4J was posted in 2006, is this still an active project?
niklassaers
Looks like it may not still be actively developed, but when I used it a few months ago it worked well. I guess it's not going to be deployed with your project so if it works as a once off tool you don't need to worry. There are loads of others which are commercially available, otherwise just use the one that comes with eclipse.
Neal Donnan
Hmm, I tried it out, but it turns out that neither the Call Graph nor the Call Tree are populated even though I've instrumented all my classes and the proxy classes. Memory and Threads works well, though
niklassaers
Did you push the little snapshot button? That should populate the graph. In a web app you can clear the registers using the button on the toolbar, perform the action you want to profile and then click the snapshot button to populate the graph for that action.
Neal Donnan
+2  A: 

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.

Yuri.Bulkin
Hehe, this is actually the same work-around I've used so far. :-) Not quite a profiler, but gets you a long way :-)
niklassaers
Speaking of, for some reason this works well on my business methods, but not on my controllers. Any idea why this might be?
niklassaers
Sorry, you did not say this in your question :)If your question is about web application and if you use tomcat servlet container, you may use SpringSource tc server with spring-insight
Yuri.Bulkin
For 2nd comment:is your applicationContext.xml contains<context:component-scan/>
Yuri.Bulkin
Hi, sorry I didn't say this in my question, I think your answer is important to have here. :-) I do have a component-scan in the applicationContext.xml, but the minute I add this aspect to the controllers, the controllers don't show up anymore. Perhaps I've got them in the wrong order
niklassaers
Or can it be that my business component is tagged @Service, while my controller is annotated @Controller? Because if I do the same on my entities that are annotated @Repository or @Entity, I don't get to weave them in either?
niklassaers
It does not matter what annotation do you use. You may weave in object with no spring annotation at all. Maybe you have an inaccuracy in pointcut description.
Yuri.Bulkin
A: 

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

laura
It looks really simple to use, but when I tried it out, I got Nullpointer errors from the aspect weaving. No idea why, when I disabled JRat they went away. Although I wouldn't be surprised if they are my errors, but I don't know what triggered them or where they came from. :-I
niklassaers
+1  A: 

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 with newer STS versions as well). It has interesting monitoring capabilities.

Csaba_H
Thanks for the suggestion. I'm working on OS X, and for some reason TPTP doesn't seem to exist there. I'd forgotten about tcServer, so I'll be sure to give that a whirl :-)
niklassaers
A: 

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.

Mike Dunlavey