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 = 25000;
010: private int threadNum;
011:
012: public TestClass(int threadNum, long lobits, long hibits,
013: Monitor mon) {
014: this .threadNum = threadNum;
015: this .lobits = lobits;
016: this .hibits = hibits;
017: this .mon = mon;
018: }
019:
020: public void run() {
021: // Alternating threads are either setting the high or lo bit. The idea is that if a thread
022: // is interrupted before mon.increase(...) we may get a different value in mon than expected.
023: // The expected value is compared to the actual value in main(...).
024: try {
025: long incr = 0;
026: if (threadNum % 2 == 0) {
027: incr = lobits;
028: } else
029: incr = hibits;
030:
031: mon.add(incr);
032: } catch (Exception e) {
033: throw new RuntimeException(e.getMessage());
034: }
035: }
036:
037: long lobits, hibits;
038: Monitor mon;
039:
040: static private class TimingMonitorThreads implements Runnable {
041: Monitor mon;
042:
043: TimingMonitorThreads(Monitor mon) {
044: this .mon = mon;
045: }
046:
047: public void run() {
048: mon.start().stop();
049: MonitorFactory.start("multi-threaded test").stop();
050: MonitorFactory.start("multi-threaded test").stop();
051: MonitorFactory.start("multi-threaded test").stop();
052: MonitorFactory.start("multi-threaded test").stop();
053: }
054: }
055:
056: public static void testMonKey(String label) {
057: System.out
058: .println("\n***** Testing getLabel, getUnits, getMonKey, getRange for "
059: + label);
060: Monitor mon = MonitorFactory.start(label).stop();
061: System.out.println(mon.getMonKey());
062: System.out.println(mon.getLabel());
063: System.out.println(mon.getUnits());
064: System.out.println(mon.getMonKey().getValue("label"));
065: System.out.println(mon.getMonKey().getValue("Units"));
066: System.out.println(mon.getRange());
067:
068: }
069:
070: public static void main(String[] args) throws Exception {
071: Monitor totalMon = MonitorFactory.start();
072: System.out.println("***** Class unit tests");
073: System.out.println("\nMonitorFactory.main()");
074: MonitorFactory.main(null);
075: System.out.println("\nTestClassPerformance.main()");
076: TestClassPerformance.main(args);
077:
078: Monitor timingMon;
079: System.out.println("\n***** MonitorFactory.getData():");
080:
081: Monitor m1 = MonitorFactory.start("pages.purchase.test");
082: Monitor m2 = MonitorFactory.start("steve.souza.test");
083: Thread.sleep(350);
084: m1.stop();
085: Thread.sleep(650);
086: m2.stop();
087: Object[][] rows = MonitorFactory.getData();
088: for (int i = 0; i < rows.length; i++) {
089: String rowData = "row" + i + "=[";
090: for (int j = 0; j < rows[0].length; j++) {
091: rowData += rows[i][j] + ", ";
092: }
093: System.out.println(rowData + "]");
094: }
095:
096: System.out.println("\n***** Multi-threaded test");
097:
098: long LOBIT = 0x00000001;//1
099: long HIBIT = 0x10000000;//268,435,456
100: timingMon = MonitorFactory.start();
101: ThreadGroup threadGroup = new ThreadGroup("threadGroup");
102: //Note mon1 is shared by all instances of the thread and so will test concurrent access.
103: Monitor mon1 = MonitorFactory.getTimeMonitor("mon1");
104: for (int i = 0; i < THREADS; i++)
105: // mon1 should be THREADS*1
106: new Thread(threadGroup,
107: new TestClass(i, LOBIT, LOBIT, mon1)).start();
108: Monitor mon2 = MonitorFactory.getTimeMonitor("mon2");
109: for (int i = 0; i < THREADS; i++)
110: //THREADS*HIBIT
111: new Thread(threadGroup,
112: new TestClass(i, HIBIT, HIBIT, mon2)).start();
113: Monitor mon3 = MonitorFactory.getTimeMonitor("mon3");
114: for (int i = 0; i < THREADS; i++)
115: //(THREADS/2)*LOBIT + (THREADS/2)*HIBIT
116: new Thread(threadGroup,
117: new TestClass(i, LOBIT, HIBIT, mon3)).start();
118: while (threadGroup.activeCount() != 0)
119: ;
120: System.out.println("Threads have finished processing. It took "
121: + timingMon.stop());
122: System.out.println("Total should equal " + THREADS + " - "
123: + mon1);
124: System.out.println("Total should equal " + THREADS * HIBIT
125: + " - " + mon2);
126: double threadCount = THREADS / 2;
127: System.out.println("Total should equal "
128: + (threadCount * LOBIT + threadCount * HIBIT) + " - "
129: + mon3);
130: Monitor mon4 = MonitorFactory.start("mon4");
131: System.out.println("\nStarting mon4 thread test");
132: Monitor mon4ThreadTest = MonitorFactory.start();
133: threadGroup = new ThreadGroup("timingMonitorThreads");
134: for (int i = 0; i < THREADS; i++)
135: new Thread(threadGroup, new TimingMonitorThreads(mon4))
136: .start();
137: while (threadGroup.activeCount() != 0)
138: ;
139: System.out.println("hits should be " + (THREADS + 1) + "= "
140: + mon4.stop());
141: System.out.println("'multi-threaded test' hits should equal "
142: + THREADS
143: * 4
144: + " - "
145: + MonitorFactory.getMonitor("multi-threaded test",
146: "ms."));
147: System.out.println("Time for mon4 thread test="
148: + mon4ThreadTest.stop());
149: System.out.println("\n***** MonitorFactory.getHeader():");
150: String[] header = MonitorFactory.getHeader();
151:
152: for (int i = 0; i < header.length; i++)
153: System.out.println(header[i]);
154:
155: testMonKey("TestingMonKey");
156: MonitorFactory.disable();
157: testMonKey("DisableTestingMonKey");
158: MonitorFactory.enable();
159:
160: JAMonListenerFactory.main(null);
161: ListenerType.main(null);
162:
163: System.out.println("\n***** MonitorFactory.getReport() 1:");
164: System.out.println(MonitorFactory.getReport());
165: System.out.println("\n***** Total Execution Time="
166: + totalMon.stop());
167: }
168: }
|