views:

249

answers:

9

Is there any Java profiler that allows profiling short-lived applications? The profilers I found so far seem to work with applications that keep running until user termination. However, I want to profile applications that work like command-line utilities, it runs and exits immediately. Tools like visualvm or NetBeans Profiler do not even recognize that the application was ran.

I am looking for something similar to Python's cProfile, in that the profiler result is returned when the application exits.

+3  A: 

Turn on hprof in the Java VM which can provide the profiling data.

http://java.sun.com/developer/technicalArticles/Programming/HPROF.html

The data produced can be read by most tools including JVisualVM.

VHF
I have tried hprof but it seems that it can only dump heap profile. Do you know if it is possible to also dump cpu time data to a file as well?
ejel
-agentlib:hprof=cpu=samples
Dave Griffiths
+2  A: 

If it doesn't take long enough, just wrap a loop around it, an infinite loop if you like. That will have no effect on the inclusive time percentages spent either in functions or in lines of code. Then, given that it's taking plenty of time, I just rely on this technique. That tells which lines of code, whether they are function calls or not, are costing the highest percentage of time and would therefore gain the most if they could be avoided.

Mike Dunlavey
A: 

I suggest you try yourkit. It can profile from the start and dump the results when the program finishes. You have to pay for it but you can get an eval license or use the EAP version without one. (Time limited)

Peter Lawrey
A: 

YourKit can take a snapshot of a profile session, which can be later analyzed in the YourKit GUI. I use this to feature to profile a command-line short-lived application I work on. See my answer to this question for details.

binil
+3  A: 

Profiling a short running Java applications has a couple of technical difficulties:

  • Profiling tools typically work by sampling the processor's SP or PC register periodically to see where the application is currently executing. If your application is short-lived, insufficient samples may be taken to get an accurate picture.

You can address this by modifying the application to run a number of times in a loop, as suggested by @Mike. You'll have problems if your app calls System.exit(), but the main problem is ...

  • The performance characteristics of a short-lived Java application are likely to be distorted by JVM warm-up effects. A lot of time will be spent in loading the classes required by your app. Then your code (and library code) will be interpreted for a bit, until the JIT compiler has figured out what needs to be compiled to native code. Finally, the JIT compiler will spend time doing its work.

I don't know if profilers attempt to compensate to for JVM warmup effects. But even if they do, these effects influence your applications real behavior, and there is not a great deal that the application developer can do to mitigate them.

Returning to my previous point ... if you run a short lived app in a loop you are actually doing something that modifies its normal execution pattern and removes the JVM warmup component. So when you optimize the method that takes (say) 50% of the execution time in the modified app, that is really 50% of the time excluding JVM warmup. If JVM warmup is using (say) 80% of the execution time when the app is executed normally, you are actually optimizing 50% of 20% ... and that is not worth the effort.

Stephen C
+1 Good comments. I would only point out, regarding your first point about profiling tools, that 1) sampling the PC register is nearly useless (compared to sampling the stack) in all but the simplest programs, and 2) not many samples are needed, because accuracy of time measurement is not needed (assuming the goal is to find fruitful points to optimize). Here's more on that subject: http://stackoverflow.com/questions/1777556/alternatives-to-gprof/1779343#1779343
Mike Dunlavey
+1  A: 

The SD Java Profiler can capture statement block execution-count data no matter how short your run is. Relative execution counts will tell you where the time is spent.

Ira Baxter
+2  A: 

Sun Java 6 has the java -Xprof switch that'll give you some profiling data.

-Xprof            output cpu profiling data
McDowell
This is a very quick way to get a rough info. Good to know!
ejel
+2  A: 

start your application with profiling turned on, waiting for profiler to attach. Any profiler that conforms to Java profiling architecture should work. i've tried this with NetBeans's profiler.

basically, when your application starts, it waits for a profiler to be attached before execution. So, technically even line of code execution can be profiled.

with this approach, you can profile all kinds of things from threads, memory, cpu, method/class invocation times/duration...

http://profiler.netbeans.org/

koss
+3  A: 

A program running 30 seconds is not shortlived. What you want is a profiler which can start your program instead of you having to attach to a running system. I believe most profilers can do that, but you would most likely like one integrated in an IDE the best. Have a look at Netbeans.

Thorbjørn Ravn Andersen