KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jca > test > BaseConnectionManagerStressTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.jca.test;
23
24 import javax.resource.ResourceException JavaDoc;
25 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
26 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
27 import javax.security.auth.Subject JavaDoc;
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 /**
42  * Unit Test for class ManagedConnectionPool
43  *
44  *
45  * Created: Wed Jan 2 00:06:35 2002
46  *
47  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
48  * @version
49  */

50 public class BaseConnectionManagerStressTestCase extends JBossTestCase
51 {
52
53    Logger log = Logger.getLogger(getClass());
54
55    boolean failed;
56    ResourceException JavaDoc error;
57    int startedThreadCount;
58    final Object JavaDoc startedLock = new Object JavaDoc();
59    int finishedThreadCount;
60    final Object JavaDoc finishedLock = new Object JavaDoc();
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 JavaDoc subject = new Subject JavaDoc();
69    ConnectionRequestInfo JavaDoc cri = new TestConnectionRequestInfo();
70    CachedConnectionManager ccm = new CachedConnectionManager();
71
72
73    /**
74     * Creates a new <code>BaseConnectionManagerStressTestCase</code> instance.
75     *
76     * @param name test name
77     */

78    public BaseConnectionManagerStressTestCase (String JavaDoc name)
79    {
80       super(name);
81    }
82
83
84    private BaseConnectionManager2 getCM(
85       InternalManagedConnectionPool.PoolParams pp)
86       throws Exception JavaDoc
87    {
88       ManagedConnectionFactory JavaDoc 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 JavaDoc
114    {
115       doShortBlocking(20, 0, 5000);
116    }
117
118    public void testShortBlockingFill()
119       throws Exception JavaDoc
120    {
121       doShortBlocking(20, getBeanCount(), 5000);
122    }
123
124    public void testShortBlockingPartFill()
125       throws Exception JavaDoc
126    {
127       doShortBlocking(20, getBeanCount()/2, 5000);
128    }
129
130    public void testShortBlockingNearlyFill()
131       throws Exception JavaDoc
132    {
133       doShortBlocking(20, getBeanCount() - 1, 5000);
134    }
135
136    public void testShortBlockingAggressiveRemoval()
137       throws Exception JavaDoc
138    {
139       doShortBlocking(20, 0, 10);
140    }
141
142    public void testShortBlockingAggressiveRemovalAndFill()
143       throws Exception JavaDoc
144    {
145       doShortBlocking(20, getBeanCount(), 10);
146    }
147
148    /**
149     * The testShortBlocking test tries to simulate extremely high load on the pool,
150     * with a short blocking timeout. It tests fairness in scheduling servicing
151     * requests. The work time is modeled by sleepTime. Allowing overhead of
152     * 15 ms/pool request, the blocking is calculated at
153     * (worktime + overhead) * (threadsPerConnection)
154     *
155     * @exception Exception if an error occurs
156     */

157    public void doShortBlocking(long sleep, int min, long idle) throws Exception JavaDoc
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 JavaDoc t = new Runnable JavaDoc()
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                         //maybe should be synchronized
205
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 JavaDoc 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                       } // end of try-catch
223
catch (InterruptedException JavaDoc ie)
224                       {
225                          break;
226                       } // end of catch
227

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 JavaDoc(t).start();
245             synchronized (startedLock)
246             {
247                while (startedThreadCount < i + 1)
248                {
249                   startedLock.wait();
250                } // end of while ()
251
}
252          } // end of for ()
253
synchronized (finishedLock)
254          {
255             while (finishedThreadCount < totalThreads)
256             {
257                finishedLock.wait();
258             } // end of while ()
259
}
260          
261          // Stop the pool/idle remover, otherwise the following checks will be random
262
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 JavaDoc 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 }//
298
Popular Tags