JAMon gathers statistics for any code that comes between start() and stop() methods. In our first somewhat artificial example we will be timing how long our program sleeps when calling the Java Thread.sleep() method ten times from within a "for" loop.
import com.jamonapi.*; public class MonitorTest { public static void main(String[] args) throws Exception { Monitor mon=null; for (int i=1; i<=10; i++) { mon = MonitorFactory.start("myFirstMonitor"); Thread.sleep(100+i); mon.stop(); } System.out.println(mon); // toString() method called } }
MonitorFactory.start("myFirstMonitor") both creates a monitor and begins gathering monitoring statistics. The start() method takes a monitor name/label as an argument. Summary statistics are gathered for all monitors that are passed identical labels. In our example the start() method is called 10 times with the same label, so the summary statistics will include data from all 10 calls.
The next line in the example is the sleep() method. The sleep() method takes one argument, which is the number of milliseconds the active thread should sleep. The loop has the effect of sleeping the thread for 101,102,...,and 110 ms. Finally comes the stop() method which will stop gathering statistics.
After the loop we print the statistics to the console, taking advantage of the monitor's toString() method. Let's take a look at what these statistics mean:
110 ms. - The execution time (in milliseconds) of the last monitor that was stopped. (There are 1,000 milliseconds in one second.)
The rest of the line contains summary statistics for the ten monitor calls. Considering our logic timed the sleep method 10 times with values ranging from 101 to 110 ms. the following results seem reasonable.
Hits=10 - A hit occurs whenever the start() method is called with an identical label. In this case "myFirstMonitor" is the monitor label.
Avg =106 ms. - Average is the total execution time divided by hits (i.e., Total/Hits).
Total=1,062 ms. - Total represents the total accumulated time for all 10 monitors that were executed. If we add 101 through 110 we get 1,055 ms., which is close to the total time the monitor calculated. The sleep() method is approximate, so our total is not exactly 1,055 ms.
Min=100 ms. - Min is the minimum execution time for the 10 hits.
Max =111 ms. - Max is the maximum execution time for the 10 hits.
Active=0 - The "Active" statistics have meaning in a multi-threaded environment. It indicates the number of current simultaneously executing monitors with identical monitor labels. This information could be used in a JSP to tell how many users are currently executing a page. In our example all monitors have been stopped prior to calling toString(), so no monitors are active.
Avg Active=1 - Average active indicates the average number of simultaneously executing monitors with identical monitor labels. In our single threaded example we would expect this to be one.
Max Active=1 - Max active indicates the maximum number of simultaneously executing monitors with identical monitor labels.
First Access=5/2/02 10:11:48 AM - Indicates when the monitor with the given monitor label was first executed.
Last Access=5/2/02 10:11:49 AM - Indicates when the monitor with the given monitor label was last executed.