001: package com.jamonapi;
002:
003: /**
004: * Class used to test performance of JAMon. It is only used for testing purposes.
005: *
006: * @author steve souza
007: */
008:
009: import java.util.*;
010:
011: public class TestClassPerformance {
012:
013: /** Creates a new instance of TestClassPerformance */
014: public TestClassPerformance() {
015: this (100000);
016: }
017:
018: private int testIterations;
019:
020: public TestClassPerformance(int testIterations) {
021: this .testIterations = testIterations;
022: }
023:
024: private Monitor testMon;
025:
026: public void timingNoMonitor() throws Exception {
027: // The null monitor has the best possible performance
028: System.out
029: .println("\ntimingNoMonitor() - timing the old fashioned way with System.currentTimeMillis() (i.e.no monitors)");
030: System.out.println("System.currentTimeMillis() - startTime");
031: long startTime = 0;
032:
033: // note the assignment to startTime is meaningless. The calculation for elapsed time would really be
034: // as follows, however being as most implementations of this timing method would not assign the results
035: // to an endTime variable, I thought it would be a better comparison to also leave it out in the performance
036: // test.
037: // startTime = System.currentTimeMillis();
038: // endTime = System.currentTimeMillis() - startTime;
039: for (int i = 0; i < testIterations; i++) {
040: startTime = System.currentTimeMillis()
041: - System.currentTimeMillis() - startTime;
042: }
043: }
044:
045: public void basicTimingMonitor() throws Exception {
046: // The null monitor has the best possible performance
047: System.out
048: .println("\nbasicTimingMonitor() - this is the most lightweight of the Monitors");
049: System.out
050: .println("\tBasicTimingMonitor mon=new BasicTimingMonitor();");
051: System.out.println("\tmon.start()");
052: System.out.println("\tmon.stop()");
053: BasicTimingMonitor mon = new BasicTimingMonitor();
054:
055: for (int i = 0; i < testIterations; i++) {
056: mon.start();
057: mon.stop();
058: }
059: }
060:
061: public void nullMonitor() throws Exception {
062: // The null monitor has the best possible performance, however it doesn't do anything so is only appropriate when monitoring
063: // is disabled.
064: System.out
065: .println("\nNullMonitor() - Factory disabled so a NullMonitor is returned");
066: System.out.println("\tMonitorFactory.setEnabled(false);");
067: System.out.println("\tMonitor mon=MonitorFactory.start();");
068: System.out.println("\tmon.stop()");
069:
070: MonitorFactory.setEnabled(false);
071:
072: for (int i = 0; i < testIterations; i++) {
073: testMon = MonitorFactory.start();
074: testMon.stop();
075: }
076:
077: MonitorFactory.setEnabled(true);
078: }
079:
080: // public void testAdd() {
081: // long loop=testIterations;
082: // for (int i=0;i<loop;i++)
083: // MonitorFactory.add("delme","test",1);
084: // }
085:
086: public void nullMonitor2() throws Exception {
087: // The null monitor has the best possible performance
088: System.out
089: .println("\nNullMonitor2() - Factory disabled so a NullMonitor is returned");
090: System.out.println("\tMonitorFactory.setEnabled(false);");
091: System.out
092: .println("\tMonitor mon=MonitorFactory.start('pages.admin');");
093: System.out.println("\tmon.stop()");
094:
095: MonitorFactory.setEnabled(false);
096:
097: for (int i = 0; i < testIterations; i++) {
098: testMon = MonitorFactory.start("pages.admin");
099: testMon.stop();
100: }
101:
102: MonitorFactory.setEnabled(true);
103: }
104:
105: public void factoryBasicMonitor() throws Exception {
106: System.out.println("\nbasic Factory TimingMonitor()");
107: System.out.println("\tMonitor mon=MonitorFactory.start();");
108: System.out.println("\tmon.stop();");
109:
110: //testMon=new TimingMonitor(); the following has the same performance characteristics but you also
111: // have the added option of disabling the service and returning a null monitor
112: for (int i = 0; i < testIterations; i++) {
113: testMon = MonitorFactory.start();
114: testMon.stop();
115: }
116:
117: }
118:
119: public void factoryMonitor() throws Exception {
120: System.out
121: .println("\nFull Factory TimingMonitor()- uses cached version so doesn't create child monitors");
122: System.out
123: .println("\tMonitor mon=MonitorFactory.start('pages.admin');");
124: System.out.println("\tmon.stop();");
125:
126: for (int i = 0; i < testIterations; i++) {
127: testMon = MonitorFactory.start("pages.admin");
128: testMon.stop();
129: }
130:
131: System.out.println(testMon);
132:
133: }
134:
135: public void addMonitor() throws Exception {
136: System.out.println("Calling add...\n");
137: System.out.println("\tMonitorFactory.add('label','units',1);");
138:
139: for (int i = 0; i < testIterations; i++) {
140: MonitorFactory.add("label", "units", 1);
141: }
142:
143: System.out.println(MonitorFactory.getMonitor("label", "units"));
144:
145: }
146:
147: /*
148:
149: public void debugFactoryMonitor() throws Exception {
150: System.out.println("\nFull Factory TimingMonitor() using debug factory - uses cached version so doesn't create child monitors");
151: System.out.println("\tMonitor mon=MonitorFactory.getDebugFactory().start('pages.admin');");
152: System.out.println("\tmon.stop();");
153:
154: for (int i=0; i<testIterations; i++) {
155: testMon=MonitorFactory.getDebugFactory().start("pages.admin"); // not executed if debug disabled.
156: testMon.stop();
157: }
158:
159: System.out.println(testMon);
160:
161: }*/
162:
163: private static void log(Monitor mon) {
164: System.out.println("It took " + mon);
165: }
166:
167: /** Test class for performance numbers of JAMon. You can execute the test code in the following 2 ways:
168: *
169: * To execute with the default number of iterations (currently 100,000). This takes about .5 seconds on my Pentium IV.
170: * java -cp JAMon.jar com.jamonapi.TestClassPerformance
171: *
172: * To execute with a different number of iterations pass the number after the class name.
173: * java -cp JAMon.jar com.jamonapi.TestClassPerformance 500000
174: **/
175: public static void main(String[] args) throws Exception {
176: TestClassPerformance test;
177:
178: if (args.length == 0)
179: test = new TestClassPerformance();
180: else {
181: int testIterations = Integer.parseInt(args[0]);
182: test = new TestClassPerformance(testIterations);
183: }
184:
185: System.out.println("\n***** Performance Tests:");
186: System.out.println("All performance code loops "
187: + test.testIterations + " times");
188:
189: Monitor totalTime = MonitorFactory.start();
190:
191: Monitor timingMon = MonitorFactory.start();
192: test.timingNoMonitor();
193: log(timingMon.stop());
194:
195: timingMon.start();
196: test.basicTimingMonitor();
197: log(timingMon.stop());
198:
199: timingMon.start();
200: test.nullMonitor();
201: log(timingMon.stop());
202:
203: timingMon.start();
204: test.nullMonitor2();
205: log(timingMon.stop());
206:
207: timingMon.start();
208: test.factoryBasicMonitor();
209: log(timingMon.stop());
210:
211: timingMon.start();
212: test.factoryMonitor();
213: log(timingMon.stop());
214:
215: System.out
216: .println("\nExecuting full factory monitors a second time. The second time reflects performance characteristics more accurately");
217: // test.testAdd();
218: timingMon.start();
219: test.factoryMonitor();
220: log(timingMon.stop());
221:
222: timingMon.start();
223: test.addMonitor();
224: log(timingMon.stop());
225:
226: Monitor delme = MonitorFactory.start("/jamon/jamonadmin.jsp");
227: JAMonBufferListener jbl = new JAMonBufferListener();
228: delme.getListenerType("max").addListener(jbl);
229: MonitorFactory.getMonitor("/jamon/jamonadmin.jsp", "ms.").add(
230: 100);
231: jbl = (JAMonBufferListener) MonitorFactory.getMonitor(
232: "/jamon/jamonadmin.jsp", "ms.").getListenerType("max")
233: .getListener();
234: Object[][] data = jbl.getDetailData().getData();
235:
236: for (int i = 0; i < data.length; i++)
237: for (int j = 0; j < data[i].length; j++)
238: System.out.println(data[i][j]);
239:
240: System.out
241: .println("\n***** Total time for performance tests were: "
242: + totalTime.stop());
243:
244: }
245:
246: }
|