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