KickJava   Java API By Example, From Geeks To Geeks.

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


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

32 public class MultiThreadedPoolComparisonProfile
33     extends PoolComparisonProfileAbstract
34 {
35     protected static final int THREADS = 100;
36
37     protected Object JavaDoc m_semaphore = new Object JavaDoc();
38     protected int m_getCount;
39     protected Throwable JavaDoc m_throwable;
40
41     /*---------------------------------------------------------------
42      * Constructors
43      *-------------------------------------------------------------*/

44     public MultiThreadedPoolComparisonProfile( String JavaDoc name )
45     {
46         super( name );
47     }
48
49     /*---------------------------------------------------------------
50      * PoolComparisonProfileAbstract Methods
51      *-------------------------------------------------------------*/

52     protected long getPoolRunTime( final Pool pool, final int gets )
53         throws Exception JavaDoc
54     {
55         if( gets % THREADS != 0 )
56         {
57             fail( "gets must be evenly divisible by THREADS" );
58         }
59
60         m_getCount = 0;
61         m_throwable = null;
62
63         // Create the runnable
64
Runnable JavaDoc runnable = new Runnable JavaDoc()
65         {
66             public void run()
67             {
68                 // Perform this threads part of the test.
69
final int cnt = gets / THREADS;
70                 final Poolable[] poolTmp = new Poolable[ cnt ];
71                 final int loops = ( TEST_SIZE / THREADS ) / cnt;
72                 for( int i = 0; i < loops; i++ )
73                 {
74                     // Get some Poolables
75
for( int j = 0; j < cnt; j++ )
76                     {
77                         try
78                         {
79                             poolTmp[ j ] = pool.get();
80                             synchronized( m_semaphore )
81                             {
82                                 m_getCount++;
83                             }
84                         }
85                         catch( Throwable JavaDoc t )
86                         {
87                             m_poolLogger.error( "Unexpected error", t );
88
89                             synchronized( m_semaphore )
90                             {
91                                 if( m_throwable == null )
92                                 {
93                                     m_throwable = t;
94                                 }
95                             }
96                             return;
97                         }
98                     }
99
100                     // Make the loops hold the poolables longer than they are released, but only slightly.
101
Thread.yield();
102
103                     // Put the Poolables back
104
for( int j = 0; j < cnt; j++ )
105                     {
106                         pool.put( poolTmp[ j ] );
107                         synchronized( m_semaphore )
108                         {
109                             m_getCount--;
110                         }
111                         poolTmp[ j ] = null;
112                     }
113                 }
114             }
115         };
116
117         LatchedThreadGroup group = new LatchedThreadGroup( runnable, THREADS );
118         group.enableLogging( m_logger );
119
120         long duration;
121         try
122         {
123             duration = group.go();
124         }
125         catch( Throwable JavaDoc t )
126         {
127             // Throwable could have been thrown by one of the tests.
128
if( m_throwable == null )
129             {
130                 m_throwable = t;
131             }
132             duration = 0;
133         }
134
135         if( m_throwable != null )
136         {
137             throw new CascadingAssertionFailedError( "Exception in test thread.", m_throwable );
138         }
139
140         assertTrue( "m_getCount == 0 (" + m_getCount + ")", m_getCount == 0 );
141
142         // Dispose if necessary
143
if( pool instanceof Disposable )
144         {
145             ( (Disposable)pool ).dispose();
146         }
147
148         return duration;
149     }
150 }
151
Popular Tags