1 22 package org.jboss.test.jca.test; 23 24 import javax.resource.ResourceException ; 25 import javax.resource.spi.ConnectionRequestInfo ; 26 import javax.resource.spi.ManagedConnectionFactory ; 27 import javax.security.auth.Subject ; 28 29 import org.jboss.logging.Logger; 30 import org.jboss.resource.connectionmanager.BaseConnectionManager2; 31 import org.jboss.resource.connectionmanager.CachedConnectionManager; 32 import org.jboss.resource.connectionmanager.ConnectionListener; 33 import org.jboss.resource.connectionmanager.InternalManagedConnectionPool; 34 import org.jboss.resource.connectionmanager.JBossManagedConnectionPool; 35 import org.jboss.resource.connectionmanager.ManagedConnectionPool; 36 import org.jboss.resource.connectionmanager.NoTxConnectionManager; 37 import org.jboss.test.jca.adapter.TestConnectionRequestInfo; 38 import org.jboss.test.jca.adapter.TestManagedConnectionFactory; 39 import org.jboss.test.JBossTestCase; 40 41 50 public class BaseConnectionManagerStressTestCase extends JBossTestCase 51 { 52 53 Logger log = Logger.getLogger(getClass()); 54 55 boolean failed; 56 ResourceException error; 57 int startedThreadCount; 58 final Object startedLock = new Object (); 59 int finishedThreadCount; 60 final Object finishedLock = new Object (); 61 int connectionCount; 62 int errorCount; 63 float elapsed = 0; 64 float held = 0; 65 float getConnection = 0; 66 float returnConnection = 0; 67 68 Subject subject = new Subject (); 69 ConnectionRequestInfo cri = new TestConnectionRequestInfo(); 70 CachedConnectionManager ccm = new CachedConnectionManager(); 71 72 73 78 public BaseConnectionManagerStressTestCase (String name) 79 { 80 super(name); 81 } 82 83 84 private BaseConnectionManager2 getCM( 85 InternalManagedConnectionPool.PoolParams pp) 86 throws Exception 87 { 88 ManagedConnectionFactory mcf = new TestManagedConnectionFactory(); 89 ManagedConnectionPool poolingStrategy = new TestPool(mcf, pp, false, log); 90 BaseConnectionManager2 cm = new NoTxConnectionManager(ccm, poolingStrategy); 91 poolingStrategy.setConnectionListenerFactory(cm); 92 return cm; 93 } 94 95 96 private void shutdown(BaseConnectionManager2 cm) 97 { 98 TestPool pool = (TestPool) cm.getPoolingStrategy(); 99 pool.shutdown(); 100 } 101 102 protected void setUp() 103 { 104 log.debug("================> Start " + getName()); 105 } 106 107 protected void tearDown() 108 { 109 log.debug("================> End " + getName()); 110 } 111 112 public void testShortBlockingNoFill() 113 throws Exception 114 { 115 doShortBlocking(20, 0, 5000); 116 } 117 118 public void testShortBlockingFill() 119 throws Exception 120 { 121 doShortBlocking(20, getBeanCount(), 5000); 122 } 123 124 public void testShortBlockingPartFill() 125 throws Exception 126 { 127 doShortBlocking(20, getBeanCount()/2, 5000); 128 } 129 130 public void testShortBlockingNearlyFill() 131 throws Exception 132 { 133 doShortBlocking(20, getBeanCount() - 1, 5000); 134 } 135 136 public void testShortBlockingAggressiveRemoval() 137 throws Exception 138 { 139 doShortBlocking(20, 0, 10); 140 } 141 142 public void testShortBlockingAggressiveRemovalAndFill() 143 throws Exception 144 { 145 doShortBlocking(20, getBeanCount(), 10); 146 } 147 148 157 public void doShortBlocking(long sleep, int min, long idle) throws Exception 158 { 159 startedThreadCount = 0; 160 finishedThreadCount = 0; 161 connectionCount = 0; 162 errorCount = 0; 163 164 final int reps = getIterationCount(); 165 final int threadsPerConnection = getThreadCount(); 166 final long sleepTime = sleep; 167 failed = false; 168 InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams(); 169 pp.minSize = min; 170 pp.maxSize = getBeanCount(); 171 pp.blockingTimeout = (threadsPerConnection) * ((int)sleepTime + 15); 172 if (pp.blockingTimeout < 1000) 173 pp.blockingTimeout = 1000; 174 pp.idleTimeout = idle; 175 final BaseConnectionManager2 cm = getCM(pp); 176 try 177 { 178 int totalThreads = pp.maxSize * threadsPerConnection; 179 log.info("ShortBlocking test with connections: " + pp.maxSize + " totalThreads: " + totalThreads + " reps: " + reps); 180 for (int i = 0; i < totalThreads; i++) 181 { 182 Runnable t = new Runnable () 183 { 184 int id; 185 public void run() 186 { 187 synchronized (startedLock) 188 { 189 id = startedThreadCount; 190 startedThreadCount++; 191 startedLock.notify(); 192 } 193 long duration = 0; 194 long getConnection = 0; 195 long returnConnection = 0; 196 long heldConnection = 0; 197 for (int j = 0; j < reps; j++) 198 { 199 try 200 { 201 long startGetConnection = System.currentTimeMillis(); 202 ConnectionListener cl = cm.getManagedConnection(null, null); 203 long endGetConnection = System.currentTimeMillis(); 204 BaseConnectionManagerStressTestCase.this.connectionCount++; 206 Thread.sleep(sleepTime); 207 long startReturnConnection = System.currentTimeMillis(); 208 cm.returnManagedConnection(cl, false); 209 long endReturnConnection = System.currentTimeMillis(); 210 211 duration += (endReturnConnection - startGetConnection); 212 getConnection += (endGetConnection - startGetConnection); 213 returnConnection += (endReturnConnection - startReturnConnection); 214 heldConnection += (startReturnConnection - endGetConnection); 215 } 216 catch (ResourceException re) 217 { 218 BaseConnectionManagerStressTestCase.this.log.info("error: iterationCount: " + j + ", connectionCount: " + BaseConnectionManagerStressTestCase.this.connectionCount + " " + re.getMessage()); 219 BaseConnectionManagerStressTestCase.this.errorCount++; 220 BaseConnectionManagerStressTestCase.this.error = re; 221 BaseConnectionManagerStressTestCase.this.failed = true; 222 } catch (InterruptedException ie) 224 { 225 break; 226 } 228 229 } 230 synchronized (BaseConnectionManagerStressTestCase.this) 231 { 232 BaseConnectionManagerStressTestCase.this.elapsed += duration; 233 BaseConnectionManagerStressTestCase.this.getConnection += getConnection; 234 BaseConnectionManagerStressTestCase.this.returnConnection += returnConnection; 235 BaseConnectionManagerStressTestCase.this.held += heldConnection; 236 } 237 synchronized (finishedLock) 238 { 239 finishedThreadCount++; 240 finishedLock.notify(); 241 } 242 } 243 }; 244 new Thread (t).start(); 245 synchronized (startedLock) 246 { 247 while (startedThreadCount < i + 1) 248 { 249 startedLock.wait(); 250 } } 252 } synchronized (finishedLock) 254 { 255 while (finishedThreadCount < totalThreads) 256 { 257 finishedLock.wait(); 258 } } 260 261 TestPool pool = (TestPool) cm.getPoolingStrategy(); 263 pool.shutdownWithoutClear(); 264 265 float expected = totalThreads * reps; 266 float lessWaiting = getConnection - (threadsPerConnection - 1) * held; 267 log.info("completed " + getName() + " with connectionCount: " + connectionCount + ", expected : " + expected); 268 log.info("errorCount: " + errorCount + " %error=" + ((100 * errorCount) / expected)); 269 log.info("Total time elapsed: " + elapsed + ", perRequest: " + (elapsed / connectionCount)); 270 log.info("Total time held : " + held + ", perRequest: " + (held / connectionCount)); 271 log.info("Time getConnection: " + getConnection + ", perRequest: " + (getConnection / connectionCount)); 272 log.info(" lessWaiting : " + lessWaiting + ", perRequest: " + (lessWaiting / connectionCount)); 273 log.info("Time retConnection: " + returnConnection + ", perRequest: " + (returnConnection / connectionCount)); 274 int available = (int) pool.getAvailableConnectionCount(); 275 assertTrue("Wrong number of connections counted: " + available, available == pp.maxSize); 276 assertTrue("Blocking Timeout occurred in ShortBlocking test: " + error, !failed); 277 } 278 finally 279 { 280 shutdown(cm); 281 } 282 } 283 284 public class TestPool extends JBossManagedConnectionPool.OnePool 285 { 286 public TestPool(final ManagedConnectionFactory mcf, final InternalManagedConnectionPool.PoolParams poolParams, 287 final boolean noTxSeparatePools, final Logger log) 288 { 289 super(mcf, poolParams, noTxSeparatePools, log); 290 } 291 292 public void shutdownWithoutClear() 293 { 294 super.shutdownWithoutClear(); 295 } 296 } 297 } | Popular Tags |