A colleague of mine recently asked about profiling and bottleneck-troubleshooting in his Java project.
It has been quite a while since I'd used a profiler or similar analysis tool. The landscape of profiling tools, particularly those that are free or nearly free, has changed dramatically over the past decade. If one uses Auntie Google to look for tools, one finds many sites with reviews, aggregations, and lots of sell-some-ads-while-we-regurgitate-the-work-of-others hits... It took a good bit of work to filter through to find a few good resources on the subject - particularly resources that are easy to understand and actually work.
I read about several of the low/no cost profilers and performance tools and found a couple that worked OK. But, as luck would have it, I happened upon JRat. The project hasn't been updated for a while on SourceForge, but the 1 Beta 1 jar works great in my current (OSX 10.6, JDK 1.6.x) dev command environment, as well as direct use and embedded at runtime in Eclipse Ganymede. NOTE: the bleeding edge/latest jar does not work on any machine I tried... use the 1 Beta 1 jar!
JRat works for what I need. It's not a comprehensive soup-to-nuts solution, but it works reliably, repeatably, is nicely performant, and is quite easy to use. Five minutes of download and hookup, then I'm profilin'.
So, how do we use it? Well, the Quick Start guide at the JRat SourceForge site is pretty much what you need. Skip the spammy regurgitation aggregation make-money-on-everyone-elses-writing pages and go here first. Let's repeat a bit: Skip the interweb tubes junk that's designed to smarm other people's work and go to the quick start on the JRat SourceForge page.
Now, with that said: You've got Ganymede running, you've got a runnable project, and you've read the instructions on SourceForge... here's a little bit of an example: with your project selected, choose to edit your run/debug configuration(s), and put in the following:
java -javaagent:shiftone-jrat.jar [if your program needs options, put them here, separated by spaces] [put the name of your main class here... remember: don't use the .class extension. For example: com.tigris.developus.JimsKewlApp]
The important takeaway is that the -javaagent argument is passed to the Java Virtual Machine and says, "hey, use this tool for a java agent", and the :shiftone-jrat.jar part says, "use this particular jar as the bytecode to run when agent callbacks are made.
Something of note for n00bs: the :shiftone-jrat.jar part means this - the shiftone-jrat.jar file is in the current directory. If your JRat jar is elsewhere, you'll need to use the full path...
How do you look at the results, anyway? It's really kind of easy. The quickstart guide gives us the syntax... just make sure you have a working command prompt (where you can type in java and it will run a JVM), CD to the directory where your shiftone-jrat.jar file is and:
java -Xmx256M -jar shiftone-jrat.jar
If all is well with your environment, a small window will open, and you can use it to open a JRat output file and browse it for all kinds of great runtime profile information.
Questions? Drop me a note.
jimp
Thank you so much for reporting about the 1-beta1 bit. Their default "latest" package is the 1-alpha2 (even though it is older than the 1-beta1), which doesn't seem to work in Java 1.6 at least. I guess they never updated the download link after making the 1-beta1 version.
ReplyDeleteHere's where I got the 1-beta1 to work:
root@[hostname ommitted] default> uname -a
Linux [hostname ommitted] 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:34:28 EST 2008 x86_64 x86_64 x86_64 GNU/Linux
root@[hostname ommitted] default> java -version
java version "1.6.0_14"
Java(TM) SE Runtime Environment (build 1.6.0_14-b08)
Java HotSpot(TM) 64-Bit Server VM (build 14.0-b16, mixed mode)
Hi jimp,
ReplyDeleteThank you for your wonderful explanation about JRAT tool. Is it possible to specify which type of output do we need in JRAT ? what does following line mean ? (I found this somewhere on internet)
java -Djrat.factory=org.shiftone.jrat.provider.tree.TreemethodHandlerFactory myprogram
Can I make some changes to class name to have different output formats ? I was kind of expecting call graph view of the program. Hierarchy does not show the sequence in which the methoda were called.
I would appreciate your any help..
Thanks