001: /*
002:
003: Derby - Class org.apache.derbyTesting.unitTests.harness.T_MultiThreadedIterations
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.unitTests.harness;
023:
024: import org.apache.derby.iapi.services.monitor.Monitor;
025: import org.apache.derby.iapi.services.property.PropertyUtil;
026:
027: import java.util.Properties;
028:
029: /**
030: Abstract class which executes T_MultiIterations. This allows multiple
031: threads running T_MultiIterations.
032:
033: This allows the setup to be performed once, and then the
034: test itself to be run with multiple threads for a number of iterations.
035: The number of threads and iterations are set by the property
036: derby.unittests.numThreads and derby.unittests.iterations
037: and default to 1.
038: <P>
039: Statistics are provided about each iteration in the error log. The statistics
040: are time for each iteration, used and total memory changes per iteration.
041:
042: @see T_Generic
043: */
044: public abstract class T_MultiThreadedIterations extends
045: T_MultiIterations implements Runnable {
046: protected int threadNumber = 0;
047:
048: static volatile boolean inError = false;
049:
050: static int numThreads = 1;
051: static int iterations = 1;
052:
053: Throwable error = null;
054: static Thread[] TestThreads;
055: static T_MultiThreadedIterations[] TestObjects;
056:
057: protected T_MultiThreadedIterations() {
058: super ();
059: }
060:
061: /**
062: Run the test. The test should raise an exception if it
063: fails. runTests should return if the tests pass.
064:
065: @exception T_Fail Test code throws these
066: */
067: protected void runTests() throws T_Fail {
068: /*
069: ** The property name for the number of iterations is
070: ** derby.className.numThreads. For example, if the test
071: ** class is derby.com.package.to.test.T_Tester,
072: ** the property name is derby.T_Tester.numThreads.
073: */
074: String myClass = this .getClass().getName();
075: String noPackage = myClass
076: .substring(myClass.lastIndexOf('.') + 1);
077: String propertyName = "derby." + noPackage + ".numThreads";
078:
079: String nthread = PropertyUtil.getSystemProperty(propertyName);
080: if (nthread != null) {
081: try {
082: numThreads = Integer.parseInt(nthread);
083: } catch (NumberFormatException nfe) {
084: numThreads = 1;
085: }
086: if (numThreads <= 0)
087: numThreads = 1;
088: }
089:
090: if (numThreads == 1) // just use this thread
091: super .runTests(); // use T_MultiIterations runtest
092: else {
093: // start numThreads new threads, each with its own test object
094: TestThreads = new Thread[numThreads];
095: TestObjects = new T_MultiThreadedIterations[numThreads];
096:
097: inError = false;
098:
099: for (int i = 0; i < numThreads; i++) {
100: TestObjects[i] = newTestObject();
101: TestObjects[i].out = this .out;
102:
103: TestThreads[i] = new Thread(TestObjects[i], "Thread_"
104: + i);
105: }
106:
107: // use the first test object to setup the test
108: TestObjects[0].setupTest();
109: TestObjects[0].threadNumber = 0;
110:
111: // make the other test objects to join in the setup
112: for (int i = 1; i < numThreads; i++) {
113: TestObjects[i].threadNumber = i;
114: TestObjects[i].joinSetupTest();
115: }
116:
117: // now run them
118: propertyName = "derby." + noPackage + ".iterations";
119:
120: String iter = PropertyUtil.getSystemProperty(propertyName);
121: if (iter != null) {
122: try {
123: iterations = Integer.parseInt(iter);
124: } catch (NumberFormatException nfe) {
125: // leave at one
126: }
127: if (iterations <= 0)
128: iterations = 1;
129: }
130:
131: for (int i = 0; i < numThreads; i++) {
132: TestThreads[i].start();
133: }
134:
135: // wait for the threads to end
136: try {
137: for (int i = 0; i < numThreads; i++) {
138: TestThreads[i].join();
139: }
140: } catch (InterruptedException ie) {
141: throw T_Fail.exceptionFail(ie);
142: }
143:
144: // report error
145: for (int i = 0; i < numThreads; i++) {
146: if (TestObjects[i].error != null)
147: throw T_Fail.exceptionFail(TestObjects[i].error);
148: }
149: }
150: }
151:
152: /*
153: * run each worker test thread
154: */
155: public void run() {
156: String threadName = "[" + Thread.currentThread().getName()
157: + "] ";
158:
159: out.println(threadName + "started");
160:
161: try {
162:
163: for (int i = 0; i < iterations; i++) {
164: Runtime.getRuntime().gc();
165: long btm = Runtime.getRuntime().totalMemory();
166: long bfm = Runtime.getRuntime().freeMemory();
167: long bum = btm - bfm;
168:
169: long start = System.currentTimeMillis();
170:
171: runTestSet();
172: long end = System.currentTimeMillis();
173:
174: Runtime.getRuntime().gc();
175: long atm = Runtime.getRuntime().totalMemory();
176: long afm = Runtime.getRuntime().freeMemory();
177: long aum = atm - afm;
178:
179: out.println(threadName + "Iteration " + i + " took "
180: + (end - start) + "ms");
181: out.println(threadName + "Total memory increased by "
182: + (atm - btm) + " is " + atm);
183: out.println(threadName + "Used memory increased by "
184: + (aum - bum) + " is " + aum);
185: }
186: } catch (ThreadDeath death) // some other thread has died and want to see my stack
187: {
188: out.println(threadName
189: + "caught thread death, printing stack");
190: death.printStackTrace(out.getPrintWriter());
191: Thread.dumpStack();
192:
193: throw death;
194: } catch (Throwable t) {
195: error = t;
196: }
197:
198: if (error == null)
199: out.println(threadName + "finished with no error");
200: else if (!inError) {
201: inError = true;
202:
203: error.printStackTrace(out.getPrintWriter());
204: for (int i = 0; i < numThreads; i++) {
205: if (this != TestObjects[i]) // don't kill myself again
206: TestThreads[i].interrupt();
207: }
208: }
209: }
210:
211: /*
212: * multi threaded test abstract methods
213: */
214:
215: /*
216: * joins an existing setup - do whatever remaining setup the test may need
217: * to do given that setupTest has already been run by another test object
218: *
219: * This call will be executed in the main (parent) thread
220: */
221: protected abstract void joinSetupTest() throws T_Fail;
222:
223: /*
224: * make a new test object instance
225: */
226: protected abstract T_MultiThreadedIterations newTestObject();
227:
228: /*
229: * class specific method
230: */
231: protected int getNumThreads() {
232: return numThreads;
233: }
234: }
|