KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > pool > test > PoolComparisonProfileAbstract


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package org.apache.avalon.excalibur.pool.test;
20
21 import junit.framework.TestCase;
22
23 import org.apache.avalon.excalibur.pool.HardResourceLimitingPool;
24 import org.apache.avalon.excalibur.pool.ObjectFactory;
25 import org.apache.avalon.excalibur.pool.Pool;
26 import org.apache.avalon.excalibur.pool.Poolable;
27 import org.apache.avalon.excalibur.pool.ResourceLimitingPool;
28 import org.apache.avalon.excalibur.pool.SoftResourceLimitingPool;
29
30 import org.apache.avalon.framework.logger.LogEnabled;
31 import org.apache.avalon.framework.logger.ConsoleLogger;
32 import org.apache.avalon.framework.logger.Logger;
33
34 /**
35  * Used as a basis for the PoolComparisonProfile Tests
36  *
37  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
38  * @version $Id: PoolComparisonProfileAbstract.java,v 1.5 2004/03/29 16:50:37 mcconnell Exp $
39  */

40 public abstract class PoolComparisonProfileAbstract
41     extends TestCase
42 {
43     /**
44      * The TEST_SIZE defines the overall size of the tests. Decreasing this will
45      * decrease the time the test takes to run, but also decrease its efficiency.
46      */

47     protected static final int TEST_SIZE = 50000;
48
49     protected Logger m_logger;
50     protected Logger m_poolLogger;
51
52     /*---------------------------------------------------------------
53      * Constructors
54      *-------------------------------------------------------------*/

55     public PoolComparisonProfileAbstract( String JavaDoc name )
56     {
57         super( name );
58
59         m_logger = new ConsoleLogger( ConsoleLogger.LEVEL_INFO );
60         m_poolLogger = m_logger.getChildLogger( "pool" );
61     }
62
63     /*---------------------------------------------------------------
64      * SoftResourceLimitingPool vs ResourceLimitingPool TestCases
65      *-------------------------------------------------------------*/

66     /**
67      * Compare the SoftResourceLimitingPool and ResourceLimitingPool when the
68      * ResourceLimitingPool is configured to act like a SoftResourceLimitingPool.
69      * <p>
70      * Test will use pools with a max size of 100, while getting up to 100 at a time,
71      * Poolables are small objects.
72      */

73     public void testCompare_SoftResourceLimitingPool_And_ResourceLimitingPool_Max100_Gets100_SmallPoolables()
74         throws Exception JavaDoc
75     {
76         String JavaDoc name = "SoftResourceLimitingPool_And_ResourceLimitingPool_Max10_Gets10_SmallPoolables";
77
78         Class JavaDoc poolableClass = SmallPoolable.class;
79         ObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger );
80         int min = 0;
81         int max = 100;
82         boolean maxStrict = false;
83         boolean blocking = false;
84         long blockTimeout = 0;
85         long trimInterval = 0;
86
87         SoftResourceLimitingPool poolA = new SoftResourceLimitingPool( factory, min, max );
88         poolA.enableLogging( m_poolLogger );
89         poolA.initialize();
90
91         ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max, maxStrict, blocking, blockTimeout, trimInterval );
92         poolB.enableLogging( m_poolLogger );
93
94         generalTest( name, poolA, poolB, 100, factory );
95     }
96
97     /**
98      * Compare the SoftResourceLimitingPool and ResourceLimitingPool when the
99      * ResourceLimitingPool is configured to act like a SoftResourceLimitingPool.
100      * <p>
101      * Test will use pools with a max size of 100, while getting up to 200 at a time,
102      * Poolables are small objects.
103      */

104     public void testCompare_SoftResourceLimitingPool_And_ResourceLimitingPool_Max100_Gets200_SmallPoolables()
105         throws Exception JavaDoc
106     {
107         String JavaDoc name = "SoftResourceLimitingPool_And_ResourceLimitingPool_Max100_Gets200_SmallPoolables";
108
109         Class JavaDoc poolableClass = SmallPoolable.class;
110         ObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger );
111         int min = 0;
112         int max = 100;
113         boolean maxStrict = false;
114         boolean blocking = false;
115         long blockTimeout = 0;
116         long trimInterval = 0;
117
118         SoftResourceLimitingPool poolA = new SoftResourceLimitingPool( factory, min, max );
119         poolA.enableLogging( m_poolLogger );
120         poolA.initialize();
121
122         ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max, maxStrict, blocking, blockTimeout, trimInterval );
123         poolB.enableLogging( m_poolLogger );
124
125         generalTest( name, poolA, poolB, 200, factory );
126     }
127
128     /**
129      * Compare the SoftResourceLimitingPool and ResourceLimitingPool when the
130      * ResourceLimitingPool is configured to act like a SoftResourceLimitingPool.
131      * <p>
132      * Test will use pools with a max size of 100, while getting up to 100 at a time,
133      * Poolables are medium objects.
134      */

135     public void testCompare_SoftResourceLimitingPool_And_ResourceLimitingPool_Max100_Gets100_MediumPoolables()
136         throws Exception JavaDoc
137     {
138         String JavaDoc name = "SoftResourceLimitingPool_And_ResourceLimitingPool_Max100_Gets100_MediumPoolables";
139
140         Class JavaDoc poolableClass = MediumPoolable.class;
141         ObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger );
142         int min = 0;
143         int max = 100;
144         boolean maxStrict = false;
145         boolean blocking = false;
146         long blockTimeout = 0;
147         long trimInterval = 0;
148
149         SoftResourceLimitingPool poolA = new SoftResourceLimitingPool( factory, min, max );
150         poolA.enableLogging( m_poolLogger );
151         poolA.initialize();
152
153         ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max, maxStrict, blocking, blockTimeout, trimInterval );
154         poolB.enableLogging( m_poolLogger );
155
156         generalTest( name, poolA, poolB, 100, factory );
157     }
158
159     /**
160      * Compare the SoftResourceLimitingPool and ResourceLimitingPool when the
161      * ResourceLimitingPool is configured to act like a SoftResourceLimitingPool.
162      * <p>
163      * Test will use pools with a max size of 100, while getting up to 200 at a time,
164      * Poolables are medium objects.
165      */

166     public void testCompare_SoftResourceLimitingPool_And_ResourceLimitingPool_Max100_Gets200_MediumPoolables()
167         throws Exception JavaDoc
168     {
169         String JavaDoc name = "SoftResourceLimitingPool_And_ResourceLimitingPool_Max100_Gets200_MediumPoolables";
170
171         Class JavaDoc poolableClass = MediumPoolable.class;
172         ObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger );
173         int min = 0;
174         int max = 100;
175         boolean maxStrict = false;
176         boolean blocking = false;
177         long blockTimeout = 0;
178         long trimInterval = 0;
179
180         SoftResourceLimitingPool poolA = new SoftResourceLimitingPool( factory, min, max );
181         poolA.enableLogging( m_poolLogger );
182         poolA.initialize();
183
184         ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max, maxStrict, blocking, blockTimeout, trimInterval );
185         poolB.enableLogging( m_poolLogger );
186
187         generalTest( name, poolA, poolB, 200, factory );
188     }
189
190     /**
191      * Compare the SoftResourceLimitingPool and ResourceLimitingPool when the
192      * ResourceLimitingPool is configured to act like a SoftResourceLimitingPool.
193      * <p>
194      * Test will use pools with a max size of 100, while getting up to 100 at a time,
195      * Poolables are large objects.
196      */

197     public void testCompare_SoftResourceLimitingPool_And_ResourceLimitingPool_Max100_Gets100_LargePoolables()
198         throws Exception JavaDoc
199     {
200         String JavaDoc name = "SoftResourceLimitingPool_And_ResourceLimitingPool_Max100_Gets100_LargePoolables";
201
202         Class JavaDoc poolableClass = LargePoolable.class;
203         ObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger );
204         int min = 0;
205         int max = 100;
206         boolean maxStrict = false;
207         boolean blocking = false;
208         long blockTimeout = 0;
209         long trimInterval = 0;
210
211         SoftResourceLimitingPool poolA = new SoftResourceLimitingPool( factory, min, max );
212         poolA.enableLogging( m_poolLogger );
213         poolA.initialize();
214
215         ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max, maxStrict, blocking, blockTimeout, trimInterval );
216         poolB.enableLogging( m_poolLogger );
217
218         generalTest( name, poolA, poolB, 100, factory );
219     }
220
221     /**
222      * Compare the SoftResourceLimitingPool and ResourceLimitingPool when the
223      * ResourceLimitingPool is configured to act like a SoftResourceLimitingPool.
224      * <p>
225      * Test will use pools with a max size of 100, while getting up to 200 at a time,
226      * Poolables are large objects.
227      */

228     public void testCompare_SoftResourceLimitingPool_And_ResourceLimitingPool_Max100_Gets200_LargePoolables()
229         throws Exception JavaDoc
230     {
231         String JavaDoc name = "SoftResourceLimitingPool_And_ResourceLimitingPool_Max100_Gets200_LargePoolables";
232
233         Class JavaDoc poolableClass = LargePoolable.class;
234         ObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger );
235         int min = 0;
236         int max = 100;
237         boolean maxStrict = false;
238         boolean blocking = false;
239         long blockTimeout = 0;
240         long trimInterval = 0;
241
242         SoftResourceLimitingPool poolA = new SoftResourceLimitingPool( factory, min, max );
243         poolA.enableLogging( m_poolLogger );
244         poolA.initialize();
245
246         ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max, maxStrict, blocking, blockTimeout, trimInterval );
247         poolB.enableLogging( m_poolLogger );
248
249         generalTest( name, poolA, poolB, 200, factory );
250     }
251
252     /*---------------------------------------------------------------
253      * HardResourceLimitingPool vs ResourceLimitingPool TestCases
254      *-------------------------------------------------------------*/

255     /**
256      * Compare the HardResourceLimitingPool and ResourceLimitingPool when the
257      * ResourceLimitingPool is configured to act like a HardResourceLimitingPool.
258      * <p>
259      * Test will use pools with a max size of 100, while getting up to 100 at a time,
260      * Poolables are small objects.
261      */

262     public void testCompare_HardResourceLimitingPool_And_ResourceLimitingPool_Max100_Gets100_SmallPoolables()
263         throws Exception JavaDoc
264     {
265         String JavaDoc name = "HardResourceLimitingPool_And_ResourceLimitingPool_Max100_Gets100_SmallPoolables";
266
267         Class JavaDoc poolableClass = SmallPoolable.class;
268         ObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger );
269         int min = 0;
270         int max = 100;
271         boolean maxStrict = true;
272         boolean blocking = false;
273         long blockTimeout = 0;
274         long trimInterval = 0;
275
276         HardResourceLimitingPool poolA = new HardResourceLimitingPool( factory, min, max );
277         poolA.enableLogging( m_poolLogger );
278         poolA.initialize();
279
280         ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max, maxStrict, blocking, blockTimeout, trimInterval );
281         poolB.enableLogging( m_poolLogger );
282
283         generalTest( name, poolA, poolB, 100, factory );
284     }
285
286     /**
287      * Compare the HardResourceLimitingPool and ResourceLimitingPool when the
288      * ResourceLimitingPool is configured to act like a HardResourceLimitingPool.
289      * <p>
290      * Test will use pools with a max size of 100, while getting up to 100 at a time,
291      * Poolables are medium objects.
292      */

293     public void testCompare_HardResourceLimitingPool_And_ResourceLimitingPool_Max10_Gets10_MediumPoolables()
294         throws Exception JavaDoc
295     {
296         String JavaDoc name = "HardResourceLimitingPool_And_ResourceLimitingPool_Max100_Gets100_MediumPoolables";
297
298         Class JavaDoc poolableClass = MediumPoolable.class;
299         ObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger );
300         int min = 0;
301         int max = 100;
302         boolean maxStrict = true;
303         boolean blocking = false;
304         long blockTimeout = 0;
305         long trimInterval = 0;
306
307         HardResourceLimitingPool poolA = new HardResourceLimitingPool( factory, min, max );
308         poolA.enableLogging( m_poolLogger );
309         poolA.initialize();
310
311         ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max, maxStrict, blocking, blockTimeout, trimInterval );
312         poolB.enableLogging( m_poolLogger );
313
314         generalTest( name, poolA, poolB, 100, factory );
315     }
316
317     /**
318      * Compare the HardResourceLimitingPool and ResourceLimitingPool when the
319      * ResourceLimitingPool is configured to act like a HardResourceLimitingPool.
320      * <p>
321      * Test will use pools with a max size of 100, while getting up to 100 at a time,
322      * Poolables are large objects.
323      */

324     public void testCompare_HardResourceLimitingPool_And_ResourceLimitingPool_Max100_Gets100_LargePoolables()
325         throws Exception JavaDoc
326     {
327         String JavaDoc name = "HardResourceLimitingPool_And_ResourceLimitingPool_Max100_Gets100_LargePoolables";
328
329         Class JavaDoc poolableClass = LargePoolable.class;
330         ObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger );
331         int min = 0;
332         int max = 100;
333         boolean maxStrict = true;
334         boolean blocking = false;
335         long blockTimeout = 0;
336         long trimInterval = 0;
337
338         HardResourceLimitingPool poolA = new HardResourceLimitingPool( factory, min, max );
339         poolA.enableLogging( m_poolLogger );
340         poolA.initialize();
341
342         ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max, maxStrict, blocking, blockTimeout, trimInterval );
343         poolB.enableLogging( m_poolLogger );
344
345         generalTest( name, poolA, poolB, 100, factory );
346     }
347
348     /*---------------------------------------------------------------
349      * Test Classes
350      *-------------------------------------------------------------*/

351     public static class SmallPoolable
352         implements Poolable
353     {
354         int a;
355     }
356
357     public static class MediumPoolable
358         implements Poolable
359     {
360         int[] a = new int[ 100 ];
361     }
362
363     public static class LargePoolable
364         implements Poolable
365     {
366         int[][] a = new int[ 10 ][ 100 ];
367     }
368
369     /**
370      * Dummy class used for timing test cases where no pooling is done.
371      */

372     public static class NoPoolingPool
373         implements Pool, LogEnabled
374     {
375         private ObjectFactory m_factory;
376         private Logger m_logger;
377
378         public NoPoolingPool( ObjectFactory factory )
379         {
380             m_factory = factory;
381         }
382
383         public void enableLogging( Logger logger )
384         {
385             m_logger = logger;
386         }
387
388         public Poolable get() throws Exception JavaDoc
389         {
390             return (Poolable)m_factory.newInstance();
391         }
392
393         public void put( Poolable poolable )
394         {
395             try
396             {
397                 m_factory.decommission( poolable );
398             }
399             catch( Exception JavaDoc e )
400             {
401                 m_logger.debug( "Error decommissioning object", e );
402             }
403         }
404     }
405
406     /*---------------------------------------------------------------
407      * Utility Methods
408      *-------------------------------------------------------------*/

409     protected void resetMemory()
410     {
411         System.gc();
412         System.gc();
413
414         // Let the system settle down.
415
try
416         {
417             Thread.sleep( 50 );
418         }
419         catch( InterruptedException JavaDoc e )
420         {
421         }
422         Runtime JavaDoc runtime = Runtime.getRuntime();
423         m_logger.debug( "Memory: " + ( runtime.totalMemory() - runtime.freeMemory() ) );
424     }
425
426     protected String JavaDoc getShortClassName( Object JavaDoc o )
427     {
428         String JavaDoc name = o.getClass().getName();
429         int pos = name.lastIndexOf( '.' );
430         if( pos > 0 )
431         {
432             name = name.substring( pos + 1 );
433         }
434         return name;
435     }
436
437     protected abstract long getPoolRunTime( Pool pool, int gets )
438         throws Exception JavaDoc;
439
440     /**
441      * The guts of the various test cases. Will dispose the pools
442      */

443     protected void generalTest( String JavaDoc name, Pool poolA, Pool poolB, int gets, ObjectFactory factory )
444         throws Exception JavaDoc
445     {
446         m_logger.info( "Test Case: " + name );
447
448         // Get the short class names
449
final String JavaDoc poolAName = getShortClassName( poolA );
450         final String JavaDoc poolBName = getShortClassName( poolB );
451
452         // Start clean
453
resetMemory();
454
455         // Get a baseline speed for object creation
456
NoPoolingPool poolBase = new NoPoolingPool( factory );
457         poolBase.enableLogging( m_poolLogger );
458         final long noPoolDuration = getPoolRunTime( poolBase, gets );
459         m_logger.info( " Unpooled time = " + noPoolDuration + "ms. to use " + TEST_SIZE + " objects." );
460         resetMemory();
461
462
463         // Get the time for poolA
464
final long poolADuration = getPoolRunTime( poolA, gets );
465         m_logger.info( " " + poolAName + " time = " + poolADuration + "ms. to use " + TEST_SIZE + " objects, " + gets + " at a time." );
466         resetMemory();
467
468
469         // Get the time for poolB
470
final long poolBDuration = getPoolRunTime( poolB, gets );
471         m_logger.info( " " + poolBName + " time = " + poolBDuration + "ms. to use " + TEST_SIZE + " objects, " + gets + " at a time." );
472         resetMemory();
473
474         // Show a summary
475
if( m_logger.isInfoEnabled() )
476         {
477             double mult;
478             mult = ( poolADuration > 0 ? ( noPoolDuration * 100 / poolADuration ) / 100.0 : Float.POSITIVE_INFINITY );
479             m_logger.info( " => " + poolAName + " is " + mult + " X as fast as not pooling." );
480
481             mult = ( poolBDuration > 0 ? ( noPoolDuration * 100 / poolBDuration ) / 100.0 : Float.POSITIVE_INFINITY );
482             m_logger.info( " => " + poolBName + " is " + mult + " X as fast as not pooling." );
483
484             mult = ( poolBDuration > 0 ? ( poolADuration * 100 / poolBDuration ) / 100.0 : Float.POSITIVE_INFINITY );
485             m_logger.info( " => " + poolBName + " is " + mult + " X as fast as " + poolAName + "." );
486         }
487     }
488 }
489
Popular Tags