KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > mpool > test > SingleThreadedPoolComparisonProfile


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

17 package org.apache.excalibur.mpool.test;
18
19 import org.apache.avalon.excalibur.pool.Poolable;
20 import org.apache.avalon.framework.activity.Disposable;
21 import org.apache.excalibur.mpool.Pool;
22
23 /**
24  * This is used to profile and compare various pool implementations
25  * given a single access thread.
26  *
27  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
28  * @version $Id: SingleThreadedPoolComparisonProfile.java,v 1.4 2004/02/28 11:47:32 cziegeler Exp $
29  */

30 public class SingleThreadedPoolComparisonProfile
31     extends PoolComparisonProfileAbstract
32 {
33     /*---------------------------------------------------------------
34      * Constructors
35      *-------------------------------------------------------------*/

36     public SingleThreadedPoolComparisonProfile( String JavaDoc name )
37     {
38         super( name );
39     }
40
41     /*---------------------------------------------------------------
42      * PoolComparisonProfileAbstract Methods
43      *-------------------------------------------------------------*/

44     protected long getPoolRunTime( Pool pool, int gets )
45         throws Exception JavaDoc
46     {
47         // Start clean
48
resetMemory();
49
50         final long startTime = System.currentTimeMillis();
51         final Object JavaDoc[] poolTmp = new Object JavaDoc[ gets ];
52         final int loops = TEST_SIZE / gets;
53         for( int i = 0; i < loops; i++ )
54         {
55             // Get some Poolables
56
for( int j = 0; j < gets; j++ )
57             {
58                 poolTmp[ j ] = pool.acquire();
59             }
60
61             // Put the Poolables back
62
for( int j = 0; j < gets; j++ )
63             {
64                 pool.release( poolTmp[ j ] );
65                 poolTmp[ j ] = null;
66             }
67         }
68         final long duration = System.currentTimeMillis() - startTime;
69
70         // Dispose if necessary
71
if( pool instanceof Disposable )
72         {
73             ( (Disposable)pool ).dispose();
74         }
75
76         return duration;
77     }
78
79     /*---------------------------------------------------------------
80      * PoolComparisonProfileAbstract Methods
81      *-------------------------------------------------------------*/

82     protected long getPoolRunTime( org.apache.avalon.excalibur.pool.Pool pool, int gets )
83         throws Exception JavaDoc
84     {
85         // Start clean
86
resetMemory();
87
88         final long startTime = System.currentTimeMillis();
89         final Poolable[] poolTmp = new Poolable[ gets ];
90         final int loops = TEST_SIZE / gets;
91         for( int i = 0; i < loops; i++ )
92         {
93             // Get some Poolables
94
for( int j = 0; j < gets; j++ )
95             {
96                 poolTmp[ j ] = pool.get();
97             }
98
99             // Put the Poolables back
100
for( int j = 0; j < gets; j++ )
101             {
102                 pool.put( poolTmp[ j ] );
103                 poolTmp[ j ] = null;
104             }
105         }
106         final long duration = System.currentTimeMillis() - startTime;
107
108         // Dispose if necessary
109
if( pool instanceof Disposable )
110         {
111             ( (Disposable)pool ).dispose();
112         }
113
114         return duration;
115     }
116 }
117
Popular Tags