001: package com.jamonapi;
002:
003: /** Class used to test all classes in JAMon. It is only used during testing. Mostly it calls other classes Main method. JAMon
004: * places test code in main() methods. TestClass also adds test code of its own
005: **/
006:
007: public class TestClass extends java.lang.Object implements Runnable {
008:
009: static final int THREADS = 250;
010:
011: private int threadNum;
012:
013: public TestClass(int threadNum, long lobits, long hibits,
014: AccumulateMonitor mon) {
015: this .threadNum = threadNum;
016: this .lobits = lobits;
017: this .hibits = hibits;
018: this .mon = mon;
019: }
020:
021: protected TestClass() {
022: }
023:
024: private static void log(Object obj) {
025: System.out.println("It took " + obj);
026: }
027:
028: public void run() {
029: // Alternating threads are either setting the high or lo bit. The idea is that if a thread
030: // is interrupted before mon.increase(...) we may get a different value in mon than expected.
031: // The expected value is compared to the actual value in main(...).
032: try {
033: long incr = 0;
034: if (threadNum % 2 == 0) {
035: incr = lobits;
036: Thread.sleep(10);
037: } else
038: incr = hibits;
039:
040: mon.increase(incr);
041: } catch (Exception e) {
042: throw new RuntimeException(e.getMessage());
043: }
044:
045: }
046:
047: long lobits, hibits;
048:
049: AccumulateMonitor mon;
050:
051: static private class TimingMonitorThreads implements Runnable {
052: Monitor mon;
053:
054: TimingMonitorThreads(Monitor mon) {
055: this .mon = mon;
056: }
057:
058: public void run() {
059: mon.start().stop();
060: }
061: }
062:
063: public static void main(String[] args) throws Exception {
064:
065: System.out.println("***** Class unit tests");
066:
067: System.out
068: .println("\nAccumulateMonitor.main() test code - all numbers should be equal");
069: AccumulateMonitor.main(null);
070:
071: System.out.println("\nTimeStatsMonitor.main()");
072: TimeStatsMonitor.main(null);
073:
074: System.out.println("\nTimeStatsDistMonitor.main()");
075: TimeStatsDistMonitor.main(null);
076:
077: System.out.println("\nActiveStatsMonitor.main()");
078: ActiveStatsMonitor.main(null);
079:
080: System.out
081: .println("\nTimingMonitor.main() test code - all numbers should be equal");
082: TimingMonitor.main(null);
083:
084: System.out.println("\nLastAccessMonitor.main()");
085: LastAccessMonitor.main(null);
086:
087: System.out.println("\nMonitorFactory.main()");
088: MonitorFactory.main(null);
089:
090: TestClass test = new TestClass();
091:
092: Monitor timingMon;
093:
094: System.out.println("\n***** MonitorFactory.getData():");
095:
096: Monitor m1 = MonitorFactory.start("pages.purchase.test");
097: Monitor m2 = MonitorFactory.start("steve.souza.test");
098:
099: Thread.sleep(350);
100: m1.stop();
101: Thread.sleep(650);
102: m2.stop();
103:
104: String[][] rows = MonitorFactory.getRootMonitor().getData();
105:
106: for (int i = 0; i < rows.length; i++) {
107: String rowData = "row" + i + "=[";
108: for (int j = 0; j < rows[0].length; j++) {
109: rowData += rows[i][j] + ", ";
110: }
111:
112: System.out.println(rowData + "]");
113: }
114:
115: System.out.println("\ncomposite tostring()="
116: + MonitorFactory.getRootMonitor());
117: System.out.println("pages composite tostring()="
118: + MonitorFactory.getComposite("pages"));
119:
120: System.out.println("\n***** Multi-threaded test");
121:
122: long LOBIT = 0x00000001;//1
123: long HIBIT = 0x10000000;//268,435,456
124:
125: timingMon = MonitorFactory.start();
126: ThreadGroup threadGroup = new ThreadGroup("threadGroup");
127:
128: //Note mon1 is shared by all instances of the thread and so will test concurrent access.
129: AccumulateMonitor mon1 = new AccumulateMonitor();
130: for (int i = 0; i < THREADS; i++)
131: // mon1 should be THREADS*1
132: new Thread(threadGroup,
133: new TestClass(i, LOBIT, LOBIT, mon1)).start();
134:
135: AccumulateMonitor mon2 = new AccumulateMonitor();
136: for (int i = 0; i < THREADS; i++)
137: //THREADS*HIBIT
138: new Thread(threadGroup,
139: new TestClass(i, HIBIT, HIBIT, mon2)).start();
140:
141: AccumulateMonitor mon3 = new AccumulateMonitor();
142: for (int i = 0; i < THREADS; i++)
143: //(THREADS/2)*LOBIT + (THREADS/2)*HIBIT
144: new Thread(threadGroup,
145: new TestClass(i, LOBIT, HIBIT, mon3)).start();
146:
147: while (threadGroup.activeCount() != 0)
148: ;
149:
150: System.out.println("Threads have finished processing. It took "
151: + timingMon.stop());
152: System.out.println(THREADS + "=" + mon1);
153: System.out.println(THREADS * HIBIT + "=" + mon2);
154: System.out.println((THREADS / 2) * LOBIT + (THREADS / 2)
155: * HIBIT + "=" + mon3);
156:
157: Monitor mon4 = MonitorFactory.start("TimingMonitorThreadTest")
158: .stop();
159: threadGroup = new ThreadGroup("timingMonitorThreads");
160: for (int i = 0; i < THREADS; i++)
161: new Thread(threadGroup, new TimingMonitorThreads(mon4))
162: .start();
163:
164: while (threadGroup.activeCount() != 0)
165: ;
166:
167: System.out.println("hits should be " + (THREADS + 1) + "= "
168: + mon4.stop());
169:
170: System.out.println("\n***** MonitorComposite.getHeader():");
171: String[] header = MonitorComposite.getHeader();
172:
173: for (int i = 0; i < header.length; i++)
174: System.out.println(header[i]);
175:
176: System.out.println("\n***** MonitorFactory.getReport() 1:");
177: System.out.println(MonitorFactory.getReport());
178:
179: System.out
180: .println("\n***** MonitorFactory.getReport() 2 (after setting admin page to 'JAMon/JAMonAdmin.jsp':");
181: MonitorFactory.setJAMonAdminPage("JAMon/JAMonAdmin.jsp");
182: System.out.println(MonitorFactory.getReport());
183:
184: TestClassPerformance.main(args);
185:
186: }
187: }
|