1 25 package testsuite.regression; 26 27 import java.sql.Connection ; 28 import java.sql.PreparedStatement ; 29 import java.sql.ResultSet ; 30 import java.sql.SQLException ; 31 import java.sql.Statement ; 32 import java.sql.Timestamp ; 33 import java.util.ArrayList ; 34 import java.util.Collections ; 35 import java.util.Date ; 36 import java.util.List ; 37 import java.util.Properties ; 38 39 import testsuite.BaseTestCase; 40 41 48 public class StressRegressionTest extends BaseTestCase { 49 private int numThreadsStarted; 50 51 57 public StressRegressionTest(String name) { 58 super(name); 59 60 } 62 63 68 public static void main(String [] args) { 69 junit.textui.TestRunner.run(StressRegressionTest.class); 70 } 71 72 78 public synchronized void testContention() throws Exception { 79 if (false) { 80 System.out.println("Calculating baseline elapsed time..."); 81 82 long start = System.currentTimeMillis(); 83 84 contentiousWork(this.conn, this.stmt, 0); 85 86 long singleThreadElapsedTimeMillis = System.currentTimeMillis() 87 - start; 88 89 System.out.println("Single threaded execution took " 90 + singleThreadElapsedTimeMillis + " ms."); 91 92 int numThreadsToStart = 95; 93 94 System.out.println("\nStarting " + numThreadsToStart + " threads."); 95 96 this.numThreadsStarted = numThreadsToStart; 97 98 ContentionThread[] threads = new ContentionThread[this.numThreadsStarted]; 99 100 for (int i = 0; i < numThreadsToStart; i++) { 101 threads[i] = new ContentionThread(i); 102 threads[i].start(); 103 } 104 105 for (;;) { 106 try { 107 wait(); 108 109 if (this.numThreadsStarted == 0) { 110 break; 111 } 112 } catch (InterruptedException ie) { 113 } 115 } 116 117 System.out.println("Done!"); 119 120 double avgElapsedTimeMillis = 0; 121 122 List elapsedTimes = new ArrayList (); 123 124 for (int i = 0; i < numThreadsToStart; i++) { 125 elapsedTimes.add(new Long (threads[i].elapsedTimeMillis)); 126 127 avgElapsedTimeMillis += ((double) threads[i].elapsedTimeMillis / numThreadsToStart); 128 } 129 130 Collections.sort(elapsedTimes); 131 132 System.out.println("Average elapsed time per-thread was " 133 + avgElapsedTimeMillis + " ms."); 134 System.out.println("Median elapsed time per-thread was " 135 + elapsedTimes.get(elapsedTimes.size() / 2) + " ms."); 136 System.out.println("Minimum elapsed time per-thread was " 137 + elapsedTimes.get(0) + " ms."); 138 System.out.println("Maximum elapsed time per-thread was " 139 + elapsedTimes.get(elapsedTimes.size() - 1) + " ms."); 140 } 141 } 142 143 149 public void testCreateConnections() throws Exception { 150 new CreateThread().run(); 151 } 152 153 159 public void testCreateConnectionsUnderLoad() throws Exception { 160 new CreateThread(new BusyThread()).run(); 161 } 162 163 void contentiousWork(Connection threadConn, Statement threadStmt, 164 int threadNumber) { 165 Date now = new Date (); 166 167 try { 168 for (int i = 0; i < 1000; i++) { 169 ResultSet threadRs = threadStmt.executeQuery("SELECT 1, 2"); 170 171 while (threadRs.next()) { 172 threadRs.getString(1); 173 threadRs.getString(2); 174 } 175 176 threadRs.close(); 177 178 PreparedStatement pStmt = threadConn 179 .prepareStatement("SELECT ?"); 180 pStmt.setTimestamp(1, new Timestamp (now.getTime())); 181 182 threadRs = pStmt.executeQuery(); 183 184 while (threadRs.next()) { 185 threadRs.getTimestamp(1); 186 } 187 188 threadRs.close(); 189 pStmt.close(); 190 } 191 } catch (Exception ex) { 192 throw new RuntimeException (ex.toString()); 193 } 194 } 195 196 synchronized void reportDone() { 197 this.numThreadsStarted--; 198 notify(); 199 } 200 201 public class BusyThread extends Thread { 202 boolean stop = false; 203 204 public void run() { 205 while (!this.stop) { 206 } 207 } 208 } 209 210 class ContentionThread extends Thread { 211 Connection threadConn; 212 213 Statement threadStmt; 214 215 int threadNumber; 216 217 long elapsedTimeMillis; 218 219 public ContentionThread(int num) throws SQLException { 220 this.threadNumber = num; 221 this.threadConn = getConnectionWithProps(new Properties ()); 222 this.threadStmt = this.threadConn.createStatement(); 223 224 System.out.println(this.threadConn); 225 } 226 227 public void run() { 228 long start = System.currentTimeMillis(); 229 230 try { 231 contentiousWork(this.threadConn, this.threadStmt, 232 this.threadNumber); 233 this.elapsedTimeMillis = System.currentTimeMillis() - start; 234 235 System.out 236 .println("Thread " + this.threadNumber + " finished."); 237 } finally { 238 if (this.elapsedTimeMillis == 0) { 239 this.elapsedTimeMillis = System.currentTimeMillis() - start; 240 } 241 242 reportDone(); 243 244 try { 245 this.threadStmt.close(); 246 this.threadConn.close(); 247 } catch (SQLException ex) { 248 } 250 } 251 } 252 } 253 254 class CreateThread extends Thread { 255 BusyThread busyThread; 256 257 int numConnections = 15; 258 259 public CreateThread() { 260 } 261 262 public CreateThread(BusyThread toStop) { 263 this.busyThread = toStop; 264 } 265 266 public CreateThread(int numConns) { 267 this.numConnections = numConns; 268 } 269 270 public void run() { 271 try { 272 Connection [] connList = new Connection [this.numConnections]; 273 274 long maxConnTime = Long.MIN_VALUE; 275 long minConnTime = Long.MAX_VALUE; 276 double averageTime = 0; 277 278 Properties nullProps = new Properties (); 279 280 for (int i = 0; i < this.numConnections; i++) { 281 long startTime = System.currentTimeMillis(); 282 connList[i] = getConnectionWithProps(nullProps); 283 284 long endTime = System.currentTimeMillis(); 285 long ellapsedTime = endTime - startTime; 286 287 if (ellapsedTime < minConnTime) { 288 minConnTime = ellapsedTime; 289 } 290 291 if (ellapsedTime > maxConnTime) { 292 maxConnTime = ellapsedTime; 293 } 294 295 averageTime += ((double) ellapsedTime / this.numConnections); 296 } 297 298 if (this.busyThread != null) { 299 this.busyThread.stop = true; 300 } 301 302 for (int i = 0; i < this.numConnections; i++) { 303 connList[i].close(); 304 } 305 306 System.out.println(minConnTime + "/" + maxConnTime + "/" 307 + averageTime); 308 } catch (Exception ex) { 309 throw new RuntimeException (ex); 310 } 311 } 312 } 313 } 314 | Popular Tags |