Q: How can I find out how to do performance tuning for Java programs?
A: I asked around and found a useful Web site with information. I also
have a new Java percollator graphing program that I'm tuning that
includes a useful class to help measure its performance. I've been
trying out the profiler feature of Java WorkShop 2.0 on it as well.
Almost two years ago I figured out Java enough to put together a very
simple performance graphing applet. This summer I helped a student
intern rewrite and extend it. The result is a much more useful tool that
runs as both an applet and as an application, and which browses the
files generated by my percollator Web server performance monitor script.
When graphing large amounts of data the program can slow down, so code
was added to time critical operations and report a summary. Because this
is a useful code fragment in itself, that's where I'll start.
The Metrognome Class
I wanted a simple object that measured a time interval multiple times,
then generated a summary of the results. The name is a joke, as it's a
kind of small helpful personalized metronome. The main design idea was
to create a separate object that had a simple collection interface but
could also be upgraded and extended without changing the interface. That
way a program using the Metrognome class could be instrumented in more
detail by just replacing the class. No recompilation or interface
changes should be necessary.
A Metrognome is constructed by giving it a text label that identifies
it.
Metrognome redrawTimer = new Metrognome("Display redraw");
At some point in the code, let's say during the paint method that
redraws the display, we call the start method then call the stop method.
paint() {
redrawTimer.start();
// lots of display update code
redrawTimer.stop();
}
The Metrognome is now collecting the duration of all display updates.
Somewhere else in the code we need to have a way to show the results.
The getSummary method returns a String that can be displayed in a GUI,
written on exit to the Java console or whatever else you like. Some
individual performance data can also be read from the Metrognome and
displayed, but it is easy to update the contents of the String if a more
sophisticated Metrognome is used.
The summary string shows the label, count, and min/mean/max times in
seconds. For example,
"Display redraw: count= 5 Latency(s) min= 0.017 mean= 0.029 max=
0.041"
More ideas
As usual I have more ideas than time to implement them. Some
possibilities for extending the Metrognome class, without changing the
interface to the rest of the program, might include:
- Accumulate time buckets to produce a histogram of the time
distribution
- Log a time stamped record to a buffer on each start
- Update the record on each stop with the duration
- Log the records somewhere
- Make all the Metrognomes share one big buffer -- access locking
would be needed
- Wait until a few times have been counted then compare against the
mean and deviation to report on abnormally large delays
- Open an RMI connection to report back to a central data collection
server