KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.avalon.excalibur.pool.ObjectFactory;
22 import org.apache.avalon.excalibur.pool.Pool;
23 import org.apache.avalon.excalibur.pool.Poolable;
24 import org.apache.avalon.excalibur.pool.ResourceLimitingPool;
25 import org.apache.avalon.excalibur.pool.SingleThreadedPool;
26 import org.apache.avalon.framework.activity.Disposable;
27
28 /**
29  * This is used to profile and compare various pool implementations
30  * given a single access thread.
31  *
32  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
33  * @version $Id: SingleThreadedPoolComparisonProfile.java,v 1.5 2004/03/29 16:50:37 mcconnell Exp $
34  */

35 public class SingleThreadedPoolComparisonProfile
36     extends PoolComparisonProfileAbstract
37 {
38     /*---------------------------------------------------------------
39      * Constructors
40      *-------------------------------------------------------------*/

41     public SingleThreadedPoolComparisonProfile( String JavaDoc name )
42     {
43         super( name );
44     }
45
46     /*---------------------------------------------------------------
47      * SingleThreadedPool vs ResourceLimitingPool TestCases
48      *-------------------------------------------------------------*/

49     /**
50      * Compare the SingleThreadedPool and ResourceLimitingPool when the
51      * ResourceLimitingPool is configured to act like a SingleThreadedPool.
52      * <p>
53      * Test will use pools with a max size of 10, while getting up to 10 at a time,
54      * Poolables are small objects.
55      */

56     public void testCompare_SingleThreadedPool_And_ResourceLimitingPool_Max10_Gets10_SmallPoolables()
57         throws Exception JavaDoc
58     {
59         String JavaDoc name = "SingleThreadedPool_And_ResourceLimitingPool_Max10_Gets10_SmallPoolables";
60
61         Class JavaDoc poolableClass = SmallPoolable.class;
62         ObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger );
63         int min = 0;
64         int max = 10;
65         boolean maxStrict = false;
66         boolean blocking = false;
67         long blockTimeout = 0;
68         long trimInterval = 0;
69
70         SingleThreadedPool poolA = new SingleThreadedPool( factory, min, max );
71         poolA.enableLogging( m_poolLogger );
72         poolA.initialize();
73
74         ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max, maxStrict, blocking, blockTimeout, trimInterval );
75         poolB.enableLogging( m_poolLogger );
76
77         generalTest( name, poolA, poolB, 10, factory );
78     }
79
80     /**
81      * Compare the SingleThreadedPool and ResourceLimitingPool when the
82      * ResourceLimitingPool is configured to act like a SingleThreadedPool.
83      * <p>
84      * Test will use pools with a max size of 10, while getting up to 20 at a time,
85      * Poolables are small objects.
86      */

87     public void testCompare_SingleThreadedPool_And_ResourceLimitingPool_Max10_Gets20_SmallPoolables()
88         throws Exception JavaDoc
89     {
90         String JavaDoc name = "SingleThreadedPool_And_ResourceLimitingPool_Max10_Gets20_SmallPoolables";
91
92         Class JavaDoc poolableClass = SmallPoolable.class;
93         ObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger );
94         int min = 0;
95         int max = 10;
96         boolean maxStrict = false;
97         boolean blocking = false;
98         long blockTimeout = 0;
99         long trimInterval = 0;
100
101         SingleThreadedPool poolA = new SingleThreadedPool( factory, min, max );
102         poolA.enableLogging( m_poolLogger );
103         poolA.initialize();
104
105         ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max, maxStrict, blocking, blockTimeout, trimInterval );
106         poolB.enableLogging( m_poolLogger );
107
108         generalTest( name, poolA, poolB, 20, factory );
109     }
110
111     /**
112      * Compare the SingleThreadedPool and ResourceLimitingPool when the
113      * ResourceLimitingPool is configured to act like a SingleThreadedPool.
114      * <p>
115      * Test will use pools with a max size of 10, while getting up to 10 at a time,
116      * Poolables are medium objects.
117      */

118     public void testCompare_SingleThreadedPool_And_ResourceLimitingPool_Max10_Gets10_MediumPoolables()
119         throws Exception JavaDoc
120     {
121         String JavaDoc name = "SingleThreadedPool_And_ResourceLimitingPool_Max10_Gets10_MediumPoolables";
122
123         Class JavaDoc poolableClass = MediumPoolable.class;
124         ObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger );
125         int min = 0;
126         int max = 10;
127         boolean maxStrict = false;
128         boolean blocking = false;
129         long blockTimeout = 0;
130         long trimInterval = 0;
131
132         SingleThreadedPool poolA = new SingleThreadedPool( factory, min, max );
133         poolA.enableLogging( m_poolLogger );
134         poolA.initialize();
135
136         ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max, maxStrict, blocking, blockTimeout, trimInterval );
137         poolB.enableLogging( m_poolLogger );
138
139         generalTest( name, poolA, poolB, 10, factory );
140     }
141
142     /**
143      * Compare the SingleThreadedPool and ResourceLimitingPool when the
144      * ResourceLimitingPool is configured to act like a SingleThreadedPool.
145      * <p>
146      * Test will use pools with a max size of 10, while getting up to 20 at a time,
147      * Poolables are medium objects.
148      */

149     public void testCompare_SingleThreadedPool_And_ResourceLimitingPool_Max10_Gets20_MediumPoolables()
150         throws Exception JavaDoc
151     {
152         String JavaDoc name = "SingleThreadedPool_And_ResourceLimitingPool_Max10_Gets20_MediumPoolables";
153
154         Class JavaDoc poolableClass = MediumPoolable.class;
155         ObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger );
156         int min = 0;
157         int max = 10;
158         boolean maxStrict = false;
159         boolean blocking = false;
160         long blockTimeout = 0;
161         long trimInterval = 0;
162
163         SingleThreadedPool poolA = new SingleThreadedPool( factory, min, max );
164         poolA.enableLogging( m_poolLogger );
165         poolA.initialize();
166
167         ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max, maxStrict, blocking, blockTimeout, trimInterval );
168         poolB.enableLogging( m_poolLogger );
169
170         generalTest( name, poolA, poolB, 20, factory );
171     }
172
173     /**
174      * Compare the SingleThreadedPool and ResourceLimitingPool when the
175      * ResourceLimitingPool is configured to act like a SingleThreadedPool.
176      * <p>
177      * Test will use pools with a max size of 10, while getting up to 10 at a time,
178      * Poolables are large objects.
179      */

180     public void testCompare_SingleThreadedPool_And_ResourceLimitingPool_Max10_Gets10_LargePoolables()
181         throws Exception JavaDoc
182     {
183         String JavaDoc name = "SingleThreadedPool_And_ResourceLimitingPool_Max10_Gets10_LargePoolables";
184
185         Class JavaDoc poolableClass = LargePoolable.class;
186         ObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger );
187         int min = 0;
188         int max = 10;
189         boolean maxStrict = false;
190         boolean blocking = false;
191         long blockTimeout = 0;
192         long trimInterval = 0;
193
194         SingleThreadedPool poolA = new SingleThreadedPool( factory, min, max );
195         poolA.enableLogging( m_poolLogger );
196         poolA.initialize();
197
198         ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max, maxStrict, blocking, blockTimeout, trimInterval );
199         poolB.enableLogging( m_poolLogger );
200
201         generalTest( name, poolA, poolB, 10, factory );
202     }
203
204     /**
205      * Compare the SingleThreadedPool and ResourceLimitingPool when the
206      * ResourceLimitingPool is configured to act like a SingleThreadedPool.
207      * <p>
208      * Test will use pools with a max size of 10, while getting up to 20 at a time,
209      * Poolables are large objects.
210      */

211     public void testCompare_SingleThreadedPool_And_ResourceLimitingPool_Max10_Gets20_LargePoolables()
212         throws Exception JavaDoc
213     {
214         String JavaDoc name = "SingleThreadedPool_And_ResourceLimitingPool_Max10_Gets20_LargePoolables";
215
216         Class JavaDoc poolableClass = LargePoolable.class;
217         ObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger );
218         int min = 0;
219         int max = 10;
220         boolean maxStrict = false;
221         boolean blocking = false;
222         long blockTimeout = 0;
223         long trimInterval = 0;
224
225         SingleThreadedPool poolA = new SingleThreadedPool( factory, min, max );
226         poolA.enableLogging( m_poolLogger );
227         poolA.initialize();
228
229         ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max, maxStrict, blocking, blockTimeout, trimInterval );
230         poolB.enableLogging( m_poolLogger );
231
232         generalTest( name, poolA, poolB, 20, factory );
233     }
234
235     /*---------------------------------------------------------------
236      * PoolComparisonProfileAbstract Methods
237      *-------------------------------------------------------------*/

238     protected long getPoolRunTime( Pool pool, int gets )
239         throws Exception JavaDoc
240     {
241         // Start clean
242
resetMemory();
243
244         final long startTime = System.currentTimeMillis();
245         final Poolable[] poolTmp = new Poolable[ gets ];
246         final int loops = TEST_SIZE / gets;
247         for( int i = 0; i < loops; i++ )
248         {
249             // Get some Poolables
250
for( int j = 0; j < gets; j++ )
251             {
252                 poolTmp[ j ] = pool.get();
253             }
254
255             // Put the Poolables back
256
for( int j = 0; j < gets; j++ )
257             {
258                 pool.put( poolTmp[ j ] );
259                 poolTmp[ j ] = null;
260             }
261         }
262         final long duration = System.currentTimeMillis() - startTime;
263
264         // Dispose if necessary
265
if( pool instanceof Disposable )
266         {
267             ( (Disposable)pool ).dispose();
268         }
269
270         return duration;
271     }
272 }
273
Popular Tags