tags:

views:

117

answers:

5

Hi

I want to run my Java app and for a given workload be able to see:

  • how many times a given function was called
  • how expensive each function call is in relative terms (i.e. how long each takes to execute)

I know broadly where the bottle neck is in my application but I need a much more fine grained view in order to narrow it down.

Thanks

Edit jvisualvm looks like the tool - it identified the problem in about 30 seconds. I just need to know what 'selftime' means in the context of a method profile. Thanks

+2  A: 

Take a look at Eclipse TPTP. They can provide exactly that and much more for any application started from Eclipse.

Thomas Lötzer
+1  A: 

Easiest way is to use -prof, e.g: java -prof -jar yourjar.jar

That will print a file called java.prof after the program has finished running.

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

In my application I use: -Xrunhprof:cpu=samples,thread=y,doe=y

This prints a report that contains, amongst other things, this:

CPU SAMPLES BEGIN (total = 55110) Sun Feb  7 17:02:51 2010
rank   self   accum   count  trace  method
1      69.68% 69.68%   38399 300361 java.net.SocketInputStream.socketRead0
2      24.40% 94.08%   13448 300386 java.net.SocketInputStream.socketRead0
3      0.20%  94.28%     108 300425 java.io.FileOutputStream.writeBytes
4      0.19%  94.47%     107 300976 java.net.PlainDatagramSocketImpl.receive0
5      0.19%  94.65%     102 300414 package.BlockingSampleBuffer.addSample
6      0.16%  94.82%      90 300365 java.net.SocketOutputStream.socketWrite0
7      0.16%  94.98%      89 300412 package.BlockingSampleBuffer.addSample
8      0.15%  95.13%      84 300430 java.lang.Object.wait
9      0.14%  95.27%      77 300592 java.io.FileOutputStream.writeBytes
10     0.14%  95.41%      76 300566 java.lang.AbstractStringBuilder.<init>

So you can see the total time (in seconds) spent in various methods. In mine the app spends most of its time waiting for data from a remote host (not unlikely over an internet connection).

jgubby
+5  A: 

Simplest approach for a program running in java 6 from Sun is to use the jvisualvm program in the jdk. Allows you to attach and profile without any special setup.

Thorbjørn Ravn Andersen
jvisualvm is the best free tool in my opinion. Eclipse TPTP is quite heavy and not very easy to use.
Lauri
this is a cracking little tool, thanks. Just one question, what does 'self time' mean in the context of a thread profile?
MalcomTucker
+1  A: 

If you are willing to spend a little money,

JProfiler: http://www.ej-technologies.com/products/jprofiler/overview.html

is very good, it shows you % of time used, absolute time used, and # of invocations down to the method level. It understands EJB calls, web service calls, and will even show the SQL of jdbc calls. I use it frequently to find performance issues.

It has memory profiling too, but I find the cpu profiling much more useful.

karoberts
Worth noting that this is essentially an interface that shows you the output to hprof (see above), there are free ones available but these do less interpretation/less pretty.
jgubby
+1  A: 

There have been a couple profilers listed (The eclipse one and JProfiler). I just want to HIGHLY RECOMMEND that a profiler is one of the tools in your programming toolchest.

This is something most programmers pass over, but a profiler can solve entire classes of problems that are very difficult to solve otherwise.

I'm just saying (to everyone, not just the questioner) that if you haven't used a profiler go find one, download it and run it.

By the way, they are much more powerful than the static output of the java tools--although the java tools might be enough in this specific case. A profiler can tell you what each thread is doing and can make some pretty cool call graphics (flow diagram style) that will help you analyze code you didn't write.

Just find one, and use it for a week or two so that you know what it offers.

Bill K