1 21 22 package org.apache.derbyTesting.unitTests.harness; 23 24 import org.apache.derby.iapi.services.monitor.Monitor; 25 import org.apache.derby.iapi.services.property.PropertyUtil; 26 27 import java.util.Properties ; 28 29 44 public abstract class T_MultiThreadedIterations extends T_MultiIterations implements Runnable 45 { 46 protected int threadNumber = 0; 47 48 static volatile boolean inError = false; 49 50 static int numThreads = 1; 51 static int iterations = 1; 52 53 Throwable error = null; 54 static Thread [] TestThreads; 55 static T_MultiThreadedIterations[] TestObjects; 56 57 protected T_MultiThreadedIterations() 58 { 59 super(); 60 } 61 62 68 protected void runTests() throws T_Fail 69 { 70 76 String myClass = this.getClass().getName(); 77 String noPackage = myClass.substring(myClass.lastIndexOf('.') + 1); 78 String propertyName = "derby." + noPackage + ".numThreads"; 79 80 String nthread = PropertyUtil.getSystemProperty(propertyName); 81 if (nthread != null) { 82 try { 83 numThreads = Integer.parseInt(nthread); 84 } catch (NumberFormatException nfe) { 85 numThreads = 1; 86 } 87 if (numThreads <= 0) 88 numThreads = 1; 89 } 90 91 if (numThreads == 1) super.runTests(); else 94 { 95 TestThreads = new Thread [numThreads]; 97 TestObjects = new T_MultiThreadedIterations[numThreads]; 98 99 inError = false; 100 101 for (int i = 0; i < numThreads; i++) 102 { 103 TestObjects[i] = newTestObject(); 104 TestObjects[i].out = this.out; 105 106 TestThreads[i] = new Thread (TestObjects[i], "Thread_" + i); 107 } 108 109 TestObjects[0].setupTest(); 111 TestObjects[0].threadNumber = 0; 112 113 for (int i = 1; i < numThreads; i++) 115 { 116 TestObjects[i].threadNumber = i; 117 TestObjects[i].joinSetupTest(); 118 } 119 120 propertyName = "derby." + noPackage + ".iterations"; 122 123 String iter = PropertyUtil.getSystemProperty(propertyName); 124 if (iter != null) { 125 try { 126 iterations = Integer.parseInt(iter); 127 } catch (NumberFormatException nfe) { 128 } 130 if (iterations <= 0) 131 iterations = 1; 132 } 133 134 for (int i = 0; i < numThreads; i++) 135 { 136 TestThreads[i].start(); 137 } 138 139 try 141 { 142 for (int i = 0; i < numThreads; i++) 143 { 144 TestThreads[i].join(); 145 } 146 } 147 catch (InterruptedException ie) { 148 throw T_Fail.exceptionFail(ie); 149 } 150 151 for (int i = 0; i < numThreads; i++) 153 { 154 if (TestObjects[i].error != null) 155 throw T_Fail.exceptionFail(TestObjects[i].error); 156 } 157 } 158 } 159 160 163 public void run() 164 { 165 String threadName = "[" + Thread.currentThread().getName() + "] "; 166 167 out.println(threadName + "started"); 168 169 try 170 { 171 172 for (int i = 0; i < iterations; i++) 173 { 174 Runtime.getRuntime().gc(); 175 long btm = Runtime.getRuntime().totalMemory(); 176 long bfm = Runtime.getRuntime().freeMemory(); 177 long bum = btm - bfm; 178 179 long start = System. currentTimeMillis(); 180 181 runTestSet(); 182 long end = System. currentTimeMillis(); 183 184 Runtime.getRuntime().gc(); 185 long atm = Runtime.getRuntime().totalMemory(); 186 long afm = Runtime.getRuntime().freeMemory(); 187 long aum = atm - afm; 188 189 out.println(threadName + "Iteration " + i + " took " + (end - start) + "ms"); 190 out.println(threadName + "Total memory increased by " + (atm - btm) + " is " + atm); 191 out.println(threadName + "Used memory increased by " + (aum - bum) + " is " + aum); 192 } 193 } 194 catch (ThreadDeath death) { 196 out.println(threadName + "caught thread death, printing stack"); 197 death.printStackTrace(out.getPrintWriter()); 198 Thread.dumpStack(); 199 200 throw death; 201 } 202 catch (Throwable t) 203 { 204 error = t; 205 } 206 207 if (error == null) 208 out.println(threadName + "finished with no error"); 209 else if (!inError) 210 { 211 inError = true; 212 213 error.printStackTrace(out.getPrintWriter()); 214 for (int i = 0; i < numThreads; i++) 215 { 216 if (this != TestObjects[i]) TestThreads[i].interrupt(); 218 } 219 } 220 } 221 222 225 226 232 protected abstract void joinSetupTest() throws T_Fail; 233 234 237 protected abstract T_MultiThreadedIterations newTestObject(); 238 239 240 243 protected int getNumThreads() 244 { 245 return numThreads; 246 } 247 } 248 | Popular Tags |