bloritsch 2002/08/07 15:44:26
Modified: event default.properties
Added: event/src/java/org/apache/excalibur/mpool
DefaultPoolManager.java FixedSizePool.java
ManagablePool.java ObjectFactory.java Pool.java
PoolManager.java VariableSizePool.java
event/src/test/org/apache/excalibur/mpool/test
ClassInstanceObjectFactory.java
MultiThreadedPoolComparisonProfile.java
PoolComparisonProfileAbstract.java
SingleThreadedPoolComparisonProfile.java
Log:
Move MPool to the Event package
Revision Changes Path
1.13 +128 -128 jakarta-avalon-excalibur/event/default.properties
Index: default.properties
===================================================================
RCS file: /home/cvs/jakarta-avalon-excalibur/event/default.properties,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- default.properties 7 Aug 2002 16:34:55 -0000 1.12
+++ default.properties 7 Aug 2002 22:44:25 -0000 1.13
@@ -1,128 +1,128 @@
-# -------------------------------------------------------------------
-# B U I L D P R O P E R T I E S
-# -------------------------------------------------------------------
-# Specifies default property values
-# Overridden by ../default.properties and all ant.properties
-# Not user-editable; use ant.properties files instead
-
-name=excalibur-event
-Name=Excalibur Event
-dir-name=event
-version=1.0a
-package-version=0.99
-year=2002
-
-# --------------------------------------------------
-# REQUIRED LIBRARIES
-# --------------------------------------------------
-# ----- Excalibur util, version 1.0 or later -----
-excalibur-util.home=${basedir}/../util/dist
-excalibur-util.lib=${excalibur-util.home}
-excalibur-util.jar=${excalibur-util.lib}/excalibur-util-1.0.jar
-
-# ----- Excalibur thread, version 1.0 or later -----
-excalibur-thread.home=${basedir}/../thread/dist
-excalibur-thread.lib=${excalibur-thread.home}
-excalibur-thread.jar=${excalibur-thread.lib}/excalibur-thread-1.0.jar
-
-# ----- Excalibur threadcontext, version 1.0 or later -----
-excalibur-threadcontext.home=${basedir}/../threadcontext/dist
-excalibur-threadcontext.lib=${excalibur-threadcontext.home}
-excalibur-threadcontext.jar=${excalibur-threadcontext.lib}/excalibur-threadcontext-1.0.jar
-
-# ----- Excalibur Collections, version 1.0 or later -----
-excalibur-collections.home=${basedir}/../collections/dist
-excalibur-collections.lib=${excalibur-collections.home}
-excalibur-collections.jar=${excalibur-collections.lib}/excalibur-collections-1.0.jar
-
-# ----- Excalibur Concurrent, version 1.0 or later -----
-excalibur-concurrent.home=${basedir}/../concurrent/dist
-excalibur-concurrent.lib=${excalibur-concurrent.home}
-excalibur-concurrent.jar=${excalibur-concurrent.lib}/excalibur-concurrent-1.0.jar
-
-# ----- Excalibur Pool, version 1.1 or later -----
-excalibur-pool.home=${basedir}/../pool/dist
-excalibur-pool.lib=${excalibur-pool.home}
-excalibur-pool.jar=${excalibur-pool.lib}/excalibur-pool-1.1.jar
-
-# ----- Excalibur Util, version 1.0 or later -----
-excalibur-util.home=${basedir}/../util/build/lib
-excalibur-util.lib=${excalibur-util.home}
-excalibur-util.jar=${excalibur-util.lib}/excalibur-util-1.0.jar
-
-# ----- Excalibur Scratchpad -----
-excalibur-scratchpad.home=${basedir}/../all
-excalibur-scratchpad.lib=${excalibur-scratchpad.home}/build/lib
-excalibur-scratchpad.jar=${excalibur-scratchpad.lib}/excalibur-scratchpad.jar
-
-# ----- Excalibur Core -----
-excalibur-core.home=${basedir}/../all
-excalibur-core.lib=${excalibur-core.home}/build/lib
-excalibur-core.jar=${excalibur-core.lib}/excalibur-core.jar
-
-# ----- Avalon Framework, version 4.1 or later -----
-avalon-framework.home=${basedir}/../../jakarta-avalon
-avalon-framework.lib=${avalon-framework.home}/build/lib
-avalon-framework.jar=${avalon-framework.lib}/avalon-framework.jar
-
-# --------------------------------------------------
-# REQUIRED LIBRARIES FOR UNIT TESTS
-# --------------------------------------------------
-
-# For unit testing
-
-# ----- Excalibur instrument, version 0.2 or later -----
-excalibur-instrument.home=${basedir}/../instrument/dist
-excalibur-instrument.lib=${excalibur-instrument.home}
-excalibur-instrument.jar=${excalibur-instrument.lib}/excalibur-instrument-0.3.jar
-
-# --------------------------------------------------
-
-# Settings used to configure compile environment
-build.debug = on
-build.optimize = off
-build.deprecation = off
-build.compress = false
-junit.failonerror = false
-
-# location of intermediate products
-build.dir = build
-build.testsrc = ${build.dir}/testsrc
-build.testclasses = ${build.dir}/testclasses
-build.lib = ${build.dir}/lib
-build.conf = ${build.dir}/conf
-build.classes = ${build.dir}/classes
-build.tests = ${build.dir}/tests
-build.reports = ${build.dir}/reports
-
-# Set the properties for source directories
-src.dir = src
-java.dir = ${src.dir}/java
-conf.dir = ${src.dir}/conf
-test.dir = ${src.dir}/test
-
-# needed by Cocoon
-build.context = ${build.dir}/documentation
-build.docs = ${build.dir}/docs
-build.xdocs = ${build.dir}/xdocs
-context.dir = ../../jakarta-avalon/src/documentation
-tools.dir = ../../jakarta-avalon/tools
-tools.jar = ${java.home}/../lib/tools.jar
-docs.dir = docs
-xdocs.dir = ${src.dir}/xdocs
-
-# Set the properties for distribution directories
-dist.dir = dist
-dist.javadocs = ${docs.dir}/api
-
-# name of .zip/.tar.gz/.bz2 files and their top-level directory
-dist.name = ${name}-${version}
-
-# name of jar file
-jar.name = ${name}-${version}.jar
-
-# property indicating directory where all distribution archives are placed
-dist.base = distributions
-
-depchecker.prefix=.
-
+# -------------------------------------------------------------------
+# B U I L D P R O P E R T I E S
+# -------------------------------------------------------------------
+# Specifies default property values
+# Overridden by ../default.properties and all ant.properties
+# Not user-editable; use ant.properties files instead
+
+name=excalibur-event
+Name=Excalibur Event
+dir-name=event
+version=1.0a
+package-version=0.99
+year=2002
+
+# --------------------------------------------------
+# REQUIRED LIBRARIES
+# --------------------------------------------------
+# ----- Excalibur util, version 1.0 or later -----
+excalibur-util.home=${basedir}/../util/dist
+excalibur-util.lib=${excalibur-util.home}
+excalibur-util.jar=${excalibur-util.lib}/excalibur-util-1.0.jar
+
+# ----- Excalibur thread, version 1.0 or later -----
+excalibur-thread.home=${basedir}/../thread/dist
+excalibur-thread.lib=${excalibur-thread.home}
+excalibur-thread.jar=${excalibur-thread.lib}/excalibur-thread-1.0.jar
+
+# ----- Excalibur threadcontext, version 1.0 or later -----
+excalibur-threadcontext.home=${basedir}/../threadcontext/dist
+excalibur-threadcontext.lib=${excalibur-threadcontext.home}
+excalibur-threadcontext.jar=${excalibur-threadcontext.lib}/excalibur-threadcontext-1.0.jar
+
+# ----- Excalibur Collections, version 1.0 or later -----
+excalibur-collections.home=${basedir}/../collections/dist
+excalibur-collections.lib=${excalibur-collections.home}
+excalibur-collections.jar=${excalibur-collections.lib}/excalibur-collections-1.0.jar
+
+# ----- Excalibur Concurrent, version 1.0 or later -----
+excalibur-concurrent.home=${basedir}/../concurrent/dist
+excalibur-concurrent.lib=${excalibur-concurrent.home}
+excalibur-concurrent.jar=${excalibur-concurrent.lib}/excalibur-concurrent-1.0.jar
+
+# ----- Excalibur Pool, version 1.1 or later -----
+excalibur-pool.home=${basedir}/../pool/dist
+excalibur-pool.lib=${excalibur-pool.home}
+excalibur-pool.jar=${excalibur-pool.lib}/excalibur-pool-1.1.jar
+
+# ----- Excalibur Util, version 1.0 or later -----
+excalibur-util.home=${basedir}/../util/build/lib
+excalibur-util.lib=${excalibur-util.home}
+excalibur-util.jar=${excalibur-util.lib}/excalibur-util-1.0.jar
+
+# ----- Excalibur Scratchpad -----
+excalibur-scratchpad.home=${basedir}/../all
+excalibur-scratchpad.lib=${excalibur-scratchpad.home}/build/lib
+excalibur-scratchpad.jar=${excalibur-scratchpad.lib}/excalibur-scratchpad.jar
+
+# ----- Excalibur Core -----
+excalibur-core.home=${basedir}/../all
+excalibur-core.lib=${excalibur-core.home}/build/lib
+excalibur-core.jar=${excalibur-core.lib}/excalibur-core.jar
+
+# ----- Avalon Framework, version 4.1 or later -----
+avalon-framework.home=${basedir}/../../jakarta-avalon
+avalon-framework.lib=${avalon-framework.home}/build/lib
+avalon-framework.jar=${avalon-framework.lib}/avalon-framework.jar
+
+# --------------------------------------------------
+# REQUIRED LIBRARIES FOR UNIT TESTS
+# --------------------------------------------------
+
+# For unit testing
+
+# ----- Excalibur instrument, version 0.2 or later -----
+excalibur-instrument.home=${basedir}/../instrument/dist
+excalibur-instrument.lib=${excalibur-instrument.home}
+excalibur-instrument.jar=${excalibur-instrument.lib}/excalibur-instrument-0.3.jar
+
+# --------------------------------------------------
+
+# Settings used to configure compile environment
+build.debug = on
+build.optimize = off
+build.deprecation = off
+build.compress = false
+junit.failonerror = false
+
+# location of intermediate products
+build.dir = build
+build.testsrc = ${build.dir}/testsrc
+build.testclasses = ${build.dir}/testclasses
+build.lib = ${build.dir}/lib
+build.conf = ${build.dir}/conf
+build.classes = ${build.dir}/classes
+build.tests = ${build.dir}/tests
+build.reports = ${build.dir}/reports
+
+# Set the properties for source directories
+src.dir = src
+java.dir = ${src.dir}/java
+conf.dir = ${src.dir}/conf
+test.dir = ${src.dir}/test
+
+# needed by Cocoon
+build.context = ${build.dir}/documentation
+build.docs = ${build.dir}/docs
+build.xdocs = ${build.dir}/xdocs
+context.dir = ../../jakarta-avalon/src/documentation
+tools.dir = ../../jakarta-avalon/tools
+tools.jar = ${java.home}/../lib/tools.jar
+docs.dir = docs
+xdocs.dir = ${src.dir}/xdocs
+
+# Set the properties for distribution directories
+dist.dir = dist
+dist.javadocs = ${docs.dir}/api
+
+# name of .zip/.tar.gz/.bz2 files and their top-level directory
+dist.name = ${name}-${version}
+
+# name of jar file
+jar.name = ${name}-${version}.jar
+
+# property indicating directory where all distribution archives are placed
+dist.base = distributions
+
+depchecker.prefix=.
+
1.1
jakarta-avalon-excalibur/event/src/java/org/apache/excalibur/mpool/DefaultPoolManager.java
Index: DefaultPoolManager.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software
Foundation"
must not be used to endorse or promote products derived from this
software
without prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation. For more information on the
Apache Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.excalibur.mpool;
import java.util.Iterator;
import java.util.Random;
import org.apache.avalon.excalibur.collections.BucketMap;
import org.apache.excalibur.event.Queue;
import org.apache.excalibur.event.command.RepeatedCommand;
/**
* This interface is for a PoolManager that creates pools that are managed
* asynchronously. The contract is that the controller type is specified in
* the constructor.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
* @version CVS $Revision: 1.1 $ $Date: 2002/08/07 22:44:25 $
* @since 4.1
*/
public class DefaultPoolManager implements PoolManager
{
private final long m_managerKey;
private final Random m_keyGenerator;
private final BucketMap m_keyMap = new BucketMap();
private final BucketMap m_factoryMap = new BucketMap();
public DefaultPoolManager()
{
this( null );
}
public DefaultPoolManager( final Queue commandQueue )
{
m_keyGenerator = new Random();
m_managerKey = m_keyGenerator.nextLong();
if( null != commandQueue )
{
try
{
commandQueue.enqueue( new PoolManagerCommand( m_keyMap ) );
}
catch( Exception e )
{
// ignore silently for now
}
}
}
/**
* Return a managed pool that has a controller.
*/
public Pool getManagedPool( ObjectFactory factory, int initialEntries )
throws Exception
{
ManagablePool pool = (ManagablePool)m_factoryMap.get( factory );
if( null == pool )
{
final long poolKey = getKey();
pool = new VariableSizePool( factory, initialEntries, poolKey );
m_keyMap.put( pool, new Long( poolKey ) );
m_factoryMap.put( factory, pool );
}
return pool;
}
/**
* Return a new key for the pool and controller.
*/
private final long getKey()
{
return m_keyGenerator.nextLong();
}
private static final class PoolManagerCommand implements RepeatedCommand
{
private final BucketMap m_map;
private final int m_min = 4;
private final int m_max = 256;
private final int m_grow = 4;
protected PoolManagerCommand( BucketMap map )
{
m_map = map;
}
public long getDelayInterval()
{
return 10 * 1000L;
}
public long getRepeatInterval()
{
return 10 * 1000L;
}
public int getNumberOfRepeats()
{
return 0;
}
public void execute()
throws Exception
{
Iterator i = m_map.keySet().iterator();
while( i.hasNext() )
{
ManagablePool pool = (ManagablePool)i.next();
long key = ( (Long)m_map.get( pool ) ).longValue();
int size = pool.size( key );
if( size < m_min )
{
pool.grow( m_grow, key );
}
if( size > m_max )
{
pool.shrink( m_grow, key );
}
}
}
}
}
1.1
jakarta-avalon-excalibur/event/src/java/org/apache/excalibur/mpool/FixedSizePool.java
Index: FixedSizePool.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software
Foundation"
must not be used to endorse or promote products derived from this
software
without prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation. For more information on the
Apache Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.excalibur.mpool;
import org.apache.avalon.excalibur.collections.Buffer;
import org.apache.avalon.excalibur.collections.FixedSizeBuffer;
import org.apache.avalon.framework.activity.Disposable;
/**
* This is an <code>Pool</code> that caches Poolable objects for reuse.
* Please note that this pool offers no resource limiting whatsoever.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
* @version CVS $Revision: 1.1 $ $Date: 2002/08/07 22:44:25 $
* @since 4.1
*/
public final class FixedSizePool
implements Pool, Disposable
{
private boolean m_disposed = false;
private final Buffer m_buffer;
private final ObjectFactory m_factory;
public FixedSizePool( ObjectFactory factory, int size )
throws Exception
{
m_buffer = new FixedSizeBuffer( size );
m_factory = factory;
for( int i = 0; i < size; i++ )
{
m_buffer.add( newInstance() );
}
}
public Object acquire()
{
if( m_disposed )
{
throw new IllegalStateException( "Cannot get an object from a
disposed pool" );
}
Object object = null;
synchronized( m_buffer )
{
object = m_buffer.remove();
}
return object;
}
public void release( Object object )
{
if( m_disposed )
{
try
{
m_factory.dispose( object );
}
catch( Exception e )
{
// We should never get here, but ignore the exception if it
happens
}
}
else
{
synchronized( m_buffer )
{
m_buffer.add( object );
}
}
}
public Object newInstance()
throws Exception
{
return m_factory.newInstance();
}
public void dispose()
{
m_disposed = true;
synchronized( m_buffer )
{
while( !m_buffer.isEmpty() )
{
try
{
m_factory.dispose( m_buffer.remove() );
}
catch( Exception e )
{
// We should never get here, but ignore the exception if
it happens
}
}
}
}
}
1.1
jakarta-avalon-excalibur/event/src/java/org/apache/excalibur/mpool/ManagablePool.java
Index: ManagablePool.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software
Foundation"
must not be used to endorse or promote products derived from this
software
without prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation. For more information on the
Apache Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.excalibur.mpool;
/**
* This is the interface for Pools that are not a fixed size. This interface
* exposes enough explicit state so that an external asynchronous Controller
* can do it's job. A secondary purpose of this interface is to supply a
* simple authentication mechanism so that the Pool only responds to method
* invocations by the legitimate controller.
*
* <p>
* The key is a randomly generated number greater than one assigned by the
* PoolManager and given to the Pool and the PoolController. The mechanism
* to generate the number is up to the PoolManager's policy. Keep in mind
* that should the key be made publicly available, the Pool is susceptible
* to a replay attack. Therefore, it is suggested that the key be created
* at the same time the Pool is created.
* </p>
*
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
* @version CVS $Revision: 1.1 $ $Date: 2002/08/07 22:44:25 $
* @since 4.1
*/
public interface ManagablePool extends Pool
{
/**
* Grow by the specified amount. The pool should trust the Controller
* for the Grow size.
*
* @param amount an integer amount to increase the pool size by.
* @param key an integer number supplied by the PoolManager to
* validate that the method is called legitimately
*
* @throws IllegalAccessException if the key does not match the
* controller's key.
*/
void grow( int amount, long key )
throws IllegalAccessException;
/**
* Shrink the pool by the specified amount. The pool should trust the
* Controller, but be smart enough not to achieve a negative pool size.
* In other words, you should clip the shrink amount so that the pool
* does not go below 0.
*
* @param amount an integer amount to decrease the pool size by.
* @param key an integer number supplied by the PoolManager to
* validate that the method is called legitimately
*
* @throws IllegalAccessException if the key does not match the
* controller's key.
*/
void shrink( int amount, long key )
throws IllegalAccessException;
/**
* Determine the pool's current size. The size is defined as the number
* of Poolable objects in reserve.
*
* @param key an integer number supplied by the PoolManager to
* validate that the method is called legitimately
*
* @return size of pool's reserve.
*
* @throws IllegalAccessException if the key does not match the
* controller's key.
*/
int size( long key )
throws IllegalAccessException;
}
1.1
jakarta-avalon-excalibur/event/src/java/org/apache/excalibur/mpool/ObjectFactory.java
Index: ObjectFactory.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software
Foundation"
must not be used to endorse or promote products derived from this
software
without prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation. For more information on the
Apache Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.excalibur.mpool;
/**
* This interface is to define how an ObjectFactory is defined. While this
* class is not strictly necessary, the implementation of the Pool can differ
* object creation to and instance of this interface.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
* @version CVS $Revision: 1.1 $ $Date: 2002/08/07 22:44:25 $
* @since 4.1.2
*/
public interface ObjectFactory
{
/**
* Create a new instance of the object being pooled.
*
* @return the pooled Object instance
* @throws Exception if the object cannot be instantiated
*/
Object newInstance() throws Exception;
/**
* Get the class of the object you are creating.
*
* @return Class object of the factory's class
*/
Class getCreatedClass();
/**
* Performs any deconstruction that is necessary for the
* object.
*
* @param object to destroy
* @throws IllegalArgumentException if the object is not of
* the same class that the factory creates.
* @throws Exception if there is any other reason that the
* factory has problems disposing of the object.
*/
void dispose( Object object ) throws Exception;
}
1.1
jakarta-avalon-excalibur/event/src/java/org/apache/excalibur/mpool/Pool.java
Index: Pool.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software
Foundation"
must not be used to endorse or promote products derived from this
software
without prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation. For more information on the
Apache Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.excalibur.mpool;
/**
* This interface is to define how a Pool is used. We have determined by
* using the previous Pool implementations that the Pool marker interface
* is considered harmful. When generics are introduced in JDK 1.5, this
* interface will be a prime candidate for those improvements.
*
* <p>
* It is important to realize that some objects are cheaper to simply allow
* the garbage collector to take care of them. Therefore, only pool objects
* that are computationally expensive to create. Prime candidates would be
* Components, JDBC Connection objects, Socket connections, etc.
* </p>
* <p>
* The interface is inspired by both the Mutex acquire/release and the
* structure of the ThreadLocal object. In fact, it would be trivial
* to implement a "ThreadLocal" pool.
* </p>
*
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
* @version CVS $Revision: 1.1 $ $Date: 2002/08/07 22:44:26 $
* @since 4.1.2
*/
public interface Pool
{
/**
* Acquire an instance of the pooled object.
*
* @return the pooled Object instance
*/
Object acquire() throws Exception;
/**
* Release the instance of the pooled object.
*
* @param pooledObject The pooled object to release to the pool.
*/
void release( Object pooledObject );
/**
* Create a new instance of the object being pooled.
*
* @return the pooled Object instance
*/
Object newInstance() throws Exception;
}
1.1
jakarta-avalon-excalibur/event/src/java/org/apache/excalibur/mpool/PoolManager.java
Index: PoolManager.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software
Foundation"
must not be used to endorse or promote products derived from this
software
without prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation. For more information on the
Apache Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.excalibur.mpool;
/**
* This interface is for a PoolManager that creates pools that are managed
* asynchronously. The contract is that the controller type is specified in
* the constructor.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
* @version CVS $Revision: 1.1 $ $Date: 2002/08/07 22:44:26 $
* @since 4.1
*/
public interface PoolManager
{
/**
* Return a managed pool that has a controller.
*/
Pool getManagedPool( ObjectFactory factory, int initialEntries )
throws Exception;
}
1.1
jakarta-avalon-excalibur/event/src/java/org/apache/excalibur/mpool/VariableSizePool.java
Index: VariableSizePool.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software
Foundation"
must not be used to endorse or promote products derived from this
software
without prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation. For more information on the
Apache Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.excalibur.mpool;
import org.apache.avalon.excalibur.collections.Buffer;
import org.apache.avalon.excalibur.collections.BufferUnderflowException;
import org.apache.avalon.excalibur.collections.VariableSizeBuffer;
import org.apache.avalon.framework.activity.Disposable;
/**
* This is an <code>Pool</code> that caches Poolable objects for reuse.
* Please note that this pool offers no resource limiting whatsoever.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
* @version CVS $Revision: 1.1 $ $Date: 2002/08/07 22:44:26 $
* @since 4.1
*/
public final class VariableSizePool
implements Pool, Disposable, ManagablePool
{
private boolean m_disposed = false;
private final Buffer m_buffer;
private final ObjectFactory m_factory;
private final long m_key;
/**
* Constructor for an unmanaged pool
*/
public VariableSizePool( ObjectFactory factory, int size )
throws Exception
{
this( factory, size, -1 );
}
/**
* Constructor for a managed pool
*/
public VariableSizePool( ObjectFactory factory, int size, long key )
throws Exception
{
m_buffer = new VariableSizeBuffer( size );
m_factory = factory;
m_key = key;
synchronized( m_factory )
{
for( int i = 0; i < size; i++ )
{
m_buffer.add( newInstance() );
}
}
}
public Object acquire()
throws Exception
{
if( m_disposed )
{
throw new IllegalStateException( "Cannot get an object from a
disposed pool" );
}
Object object = null;
try
{
synchronized( m_buffer )
{
object = m_buffer.remove();
}
}
catch( BufferUnderflowException bue )
{
object = newInstance();
}
if( null == object )
{
object = newInstance();
}
return object;
}
public void release( Object pooledObject )
{
if( m_disposed )
{
try
{
m_factory.dispose( pooledObject );
}
catch( Exception e )
{
// We should never get here, but ignore the exception if it
happens
}
}
else
{
synchronized( m_buffer )
{
m_buffer.add( pooledObject );
}
}
}
public Object newInstance()
throws Exception
{
synchronized( m_factory )
{
return m_factory.newInstance();
}
}
public void dispose()
{
m_disposed = true;
synchronized( m_buffer )
{
while( !m_buffer.isEmpty() )
{
try
{
m_factory.dispose( m_buffer.remove() );
}
catch( Exception e )
{
// We should never get here, but ignore the exception if
it happens
}
}
}
}
public void shrink( final int byNum, final long key )
throws IllegalAccessException
{
if( m_key < 0 || m_key != key )
{
throw new IllegalAccessException();
}
synchronized( m_buffer )
{
final int num = Math.min( byNum, m_buffer.size() );
for( int i = 0; i < num; i++ )
{
try
{
m_factory.dispose( m_buffer.remove() );
}
catch( Exception e )
{
// ignore exception
}
}
}
}
public void grow( final int byNum, final long key )
throws IllegalAccessException
{
if( m_key < 0 || m_key != key )
{
throw new IllegalAccessException();
}
synchronized( m_buffer )
{
for( int i = 0; i < byNum; i++ )
{
try
{
m_buffer.add( newInstance() );
}
catch( Exception e )
{
// ignore exception
}
}
}
}
public int size( final long key )
throws IllegalAccessException
{
if( m_key < 0 || m_key != key )
{
throw new IllegalAccessException();
}
synchronized( m_buffer )
{
return m_buffer.size();
}
}
}
1.1
jakarta-avalon-excalibur/event/src/test/org/apache/excalibur/mpool/test/ClassInstanceObjectFactory.java
Index: ClassInstanceObjectFactory.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software
Foundation"
must not be used to endorse or promote products derived from this
software
without prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation. For more information on the
Apache Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.excalibur.mpool.test;
import java.util.HashMap;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.logger.Logger;
import org.apache.excalibur.mpool.ObjectFactory;
/**
* @author <a href="mailto:[EMAIL PROTECTED]">Leif Mortenson</a>
* @version CVS $Revision: 1.1 $ $Date: 2002/08/07 22:44:26 $
* @since 4.1
*/
public class ClassInstanceObjectFactory
implements ObjectFactory, org.apache.avalon.excalibur.pool.ObjectFactory
{
private HashMap m_instances = new HashMap();
private Logger m_logger;
private Class m_clazz;
private int m_id;
/*---------------------------------------------------------------
* Constructors
*-------------------------------------------------------------*/
/**
* Creates a reproducable log of activity in the provided StringBuffer
*/
public ClassInstanceObjectFactory( Class clazz, Logger logger )
{
m_clazz = clazz;
m_logger = logger;
m_id = 1;
}
/*---------------------------------------------------------------
* ObjectFactory Methods
*-------------------------------------------------------------*/
public Object newInstance() throws Exception
{
Object object = m_clazz.newInstance();
Integer id = new Integer( m_id++ );
m_instances.put( object, id );
if( m_logger.isDebugEnabled() )
{
m_logger.debug( "ClassInstanceObjectFactory.newInstance() id:" +
id );
}
return object;
}
public Class getCreatedClass()
{
return m_clazz;
}
public void dispose( Object object ) throws Exception
{
if( object instanceof Disposable )
{
( (Disposable)object ).dispose();
}
Integer id = (Integer)m_instances.remove( object );
if( m_logger.isDebugEnabled() )
{
m_logger.debug( "ClassInstanceObjectFactory.decommission(a "
+ object.getClass().getName() + ") id:" + id );
}
}
public void decommission( Object object ) throws Exception
{
dispose( object );
}
}
1.1
jakarta-avalon-excalibur/event/src/test/org/apache/excalibur/mpool/test/MultiThreadedPoolComparisonProfile.java
Index: MultiThreadedPoolComparisonProfile.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software
Foundation"
must not be used to endorse or promote products derived from this
software
without prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation. For more information on the
Apache Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.excalibur.mpool.test;
import org.apache.avalon.excalibur.pool.Poolable;
import org.apache.avalon.excalibur.testcase.CascadingAssertionFailedError;
import org.apache.avalon.excalibur.testcase.LatchedThreadGroup;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.logger.Logger;
import org.apache.excalibur.mpool.Pool;
/**
* This is used to profile and compare various pool implementations
* given a single access thread.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Leif Mortenson</a>
* @version $Id: MultiThreadedPoolComparisonProfile.java,v 1.1 2002/08/07
22:44:26 bloritsch Exp $
*/
public class MultiThreadedPoolComparisonProfile
extends PoolComparisonProfileAbstract
{
protected static final int THREADS = 100;
private int m_startedCount;
private boolean m_latched;
private int m_completedCount;
private int m_getCount;
private Throwable m_throwable;
/*---------------------------------------------------------------
* Constructors
*-------------------------------------------------------------*/
public MultiThreadedPoolComparisonProfile( String name )
{
super( name );
}
/*---------------------------------------------------------------
* PoolComparisonProfileAbstract Methods
*-------------------------------------------------------------*/
protected long getPoolRunTime( final Pool pool, final int gets )
throws Exception
{
if( gets % THREADS != 0 )
{
m_logger.info( "Invalid: " + gets % THREADS + " gets(" + gets +
") threads(" + THREADS + ")" );
fail( "gets must be evenly divisible by THREADS" );
}
m_getCount = 0;
m_throwable = null;
// Create the runnable
MPoolRunner runnable = new MPoolRunner( pool, gets, m_logger );
LatchedThreadGroup group = new LatchedThreadGroup( runnable, THREADS
);
group.enableLogging( m_logger );
long duration;
try
{
duration = group.go();
}
catch( Throwable t )
{
// Throwable could have been thrown by one of the tests.
if( m_throwable == null )
{
m_throwable = t;
}
duration = 0;
}
if( m_throwable != null )
{
throw new CascadingAssertionFailedError( "Exception in test
thread.", m_throwable );
}
assertTrue( "m_getCount == 0 (" + m_getCount + ")", m_getCount == 0 );
// Dispose if necessary
if( pool instanceof Disposable )
{
( (Disposable)pool ).dispose();
}
return duration;
}
/*---------------------------------------------------------------
* PoolComparisonProfileAbstract Methods
*-------------------------------------------------------------*/
protected long getPoolRunTime( final
org.apache.avalon.excalibur.pool.Pool pool, final int gets )
throws Exception
{
if( gets % THREADS != 0 )
{
m_logger.info( "Invalid: " + gets % THREADS + " gets(" + gets +
") threads(" + THREADS + ")" );
fail( "gets must be evenly divisible by THREADS" );
}
m_getCount = 0;
m_throwable = null;
// Create the runnable
PoolRunner runnable = new PoolRunner( pool, gets, m_logger );
LatchedThreadGroup group = new LatchedThreadGroup( runnable, THREADS
);
group.enableLogging( m_logger );
long duration;
try
{
duration = group.go();
}
catch( Throwable t )
{
// Throwable could have been thrown by one of the tests.
if( m_throwable == null )
{
m_throwable = t;
}
duration = 0;
}
if( m_throwable != null )
{
throw new CascadingAssertionFailedError( "Exception in test
thread.", m_throwable );
}
assertTrue( "m_getCount == 0 (" + m_getCount + ")", m_getCount == 0 );
// Dispose if necessary
if( pool instanceof Disposable )
{
( (Disposable)pool ).dispose();
}
return duration;
}
private static class PoolRunner implements Runnable
{
private Logger m_logger;
private org.apache.avalon.excalibur.pool.Pool m_pool;
private int m_getCount = 0;
private Throwable m_throwable = null;
private int m_gets;
public PoolRunner( org.apache.avalon.excalibur.pool.Pool pool, int
gets, Logger logger )
{
m_pool = pool;
m_logger = logger;
m_gets = gets;
}
public int getCount()
{
return m_getCount;
}
public Throwable getThrowable()
{
return m_throwable;
}
public void run()
{
// Perform this threads part of the test.
final int cnt = m_gets / THREADS;
final Poolable[] poolTmp = new Poolable[ cnt ];
final int loops = ( TEST_SIZE / THREADS ) / cnt;
for( int i = 0; i < loops; i++ )
{
// Get some Poolables
for( int j = 0; j < cnt; j++ )
{
try
{
poolTmp[ j ] = m_pool.get();
m_getCount++;
}
catch( Throwable t )
{
m_logger.error( "Unexpected error", t );
if( m_throwable == null )
{
m_throwable = t;
}
return;
}
}
// Make the loops hold the poolables longer than they are
released, but only slightly.
Thread.yield();
// Put the Poolables back
for( int j = 0; j < cnt; j++ )
{
m_pool.put( poolTmp[ j ] );
m_getCount--;
poolTmp[ j ] = null;
}
}
}
}
private static class MPoolRunner implements Runnable
{
private Logger m_logger;
private Pool m_pool;
private int m_getCount = 0;
private Throwable m_throwable = null;
private final int m_gets;
public MPoolRunner( Pool pool, int gets, Logger logger )
{
m_pool = pool;
m_logger = logger;
m_gets = gets;
}
public int getCount()
{
return m_getCount;
}
public Throwable getThrowable()
{
return m_throwable;
}
public void run()
{
// Perform this threads part of the test.
final int cnt = m_gets / THREADS;
final Object[] poolTmp = new Poolable[ cnt ];
final int loops = ( TEST_SIZE / THREADS ) / cnt;
for( int i = 0; i < loops; i++ )
{
// Get some Poolables
for( int j = 0; j < cnt; j++ )
{
try
{
poolTmp[ j ] = m_pool.acquire();
m_getCount++;
}
catch( Throwable t )
{
m_logger.error( "Unexpected error after " +
m_getCount +
" items retrieved and " + m_gets + "
requested", t );
if( m_throwable == null )
{
m_throwable = t;
}
return;
}
}
// Make the loops hold the poolables longer than they are
released, but only slightly.
Thread.yield();
// Put the Poolables back
for( int j = 0; j < cnt; j++ )
{
m_pool.release( poolTmp[ j ] );
m_getCount--;
poolTmp[ j ] = null;
}
}
}
}
}
1.1
jakarta-avalon-excalibur/event/src/test/org/apache/excalibur/mpool/test/PoolComparisonProfileAbstract.java
Index: PoolComparisonProfileAbstract.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software
Foundation"
must not be used to endorse or promote products derived from this
software
without prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation. For more information on the
Apache Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.excalibur.mpool.test;
import junit.framework.TestCase;
import org.apache.avalon.excalibur.pool.ResourceLimitingPool;
import org.apache.avalon.framework.logger.LogEnabled;
import org.apache.avalon.framework.logger.LogKitLogger;
import org.apache.avalon.framework.logger.Logger;
import org.apache.excalibur.mpool.FixedSizePool;
import org.apache.excalibur.mpool.ObjectFactory;
import org.apache.excalibur.mpool.Pool;
import org.apache.excalibur.mpool.VariableSizePool;
/**
* Used as a basis for the PoolComparisonProfile Tests
*
* @author <a href="mailto:[EMAIL PROTECTED]">Leif Mortenson</a>
* @version $Id: PoolComparisonProfileAbstract.java,v 1.1 2002/08/07 22:44:26
bloritsch Exp $
*/
public abstract class PoolComparisonProfileAbstract
extends TestCase
{
/**
* The TEST_SIZE defines the overall size of the tests. Decreasing this
will
* decrease the time the test takes to run, but also decrease its
efficiency.
*/
protected static final int TEST_SIZE = 50000;
protected Logger m_logger;
protected Logger m_poolLogger;
/*---------------------------------------------------------------
* Constructors
*-------------------------------------------------------------*/
public PoolComparisonProfileAbstract( String name )
{
super( name );
// Set to debug to see more useful information.
org.apache.log.Logger logger =
org.apache.log.Hierarchy.getDefaultHierarchy().getLoggerFor(
"test" );
logger.setPriority( org.apache.log.Priority.INFO );
m_logger = new LogKitLogger( logger );
// The output from the pools is too much data to be useful, so use a
different logger.
org.apache.log.Logger poolLogger =
org.apache.log.Hierarchy.getDefaultHierarchy().getLoggerFor(
"pool" );
poolLogger.setPriority( org.apache.log.Priority.INFO );
m_poolLogger = new LogKitLogger( poolLogger );
}
/*---------------------------------------------------------------
* FixedSizePool vs ResourceLimitingPool TestCases
*-------------------------------------------------------------*/
/**
* Compare the FixedSizePool and ResourceLimitingPool when the
* ResourceLimitingPool is configured to act like a FixedSizePool.
* <p>
* Test will use pools with a max size of 100, while getting up to 100 at
a time,
* Poolables are small objects.
*/
public void
testCompare_FixedSizePool_And_ResourceLimitingPool_Max100_Gets100_SmallPoolables()
throws Exception
{
String name =
"FixedSizePool_And_ResourceLimitingPool_Max100_Gets100_SmallPoolables";
Class poolableClass = SmallPoolable.class;
ClassInstanceObjectFactory factory = new ClassInstanceObjectFactory(
poolableClass, m_poolLogger );
int min = 0;
int max = 100;
boolean maxStrict = true;
boolean blocking = false;
long blockTimeout = 0;
long trimInterval = 0;
FixedSizePool poolA = new FixedSizePool( factory, max );
ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max,
maxStrict, blocking, blockTimeout, trimInterval );
poolB.enableLogging( m_poolLogger );
generalTest( name, poolA, poolB, 100, factory );
}
/**
* Compare the FixedSizePool and ResourceLimitingPool when the
* ResourceLimitingPool is configured to act like a FixedSizePool.
* <p>
* Test will use pools with a max size of 100, while getting up to 100 at
a time,
* Poolables are medium objects.
*/
public void
testCompare_FixedSizePool_And_ResourceLimitingPool_Max100_Gets100_MediumPoolables()
throws Exception
{
String name =
"FixedSizePool_And_ResourceLimitingPool_Max100_Gets100_MediumPoolables";
Class poolableClass = MediumPoolable.class;
ClassInstanceObjectFactory factory = new ClassInstanceObjectFactory(
poolableClass, m_poolLogger );
int min = 0;
int max = 100;
boolean maxStrict = true;
boolean blocking = false;
long blockTimeout = 0;
long trimInterval = 0;
FixedSizePool poolA = new FixedSizePool( factory, max );
ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max,
maxStrict, blocking, blockTimeout, trimInterval );
poolB.enableLogging( m_poolLogger );
generalTest( name, poolA, poolB, 100, factory );
}
/**
* Compare the FixedSizePool and ResourceLimitingPool when the
* ResourceLimitingPool is configured to act like a FixedSizePool.
* <p>
* Test will use pools with a max size of 100, while getting up to 100 at
a time,
* Poolables are large objects.
*/
public void
testCompare_FixedSizePool_And_ResourceLimitingPool_Max100_Gets100_LargePoolables()
throws Exception
{
String name =
"FixedSizePool_And_ResourceLimitingPool_Max100_Gets100_LargePoolables";
Class poolableClass = LargePoolable.class;
ClassInstanceObjectFactory factory = new ClassInstanceObjectFactory(
poolableClass, m_poolLogger );
int min = 0;
int max = 100;
boolean maxStrict = true;
boolean blocking = false;
long blockTimeout = 0;
long trimInterval = 0;
FixedSizePool poolA = new FixedSizePool( factory, max );
ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max,
maxStrict, blocking, blockTimeout, trimInterval );
poolB.enableLogging( m_poolLogger );
generalTest( name, poolA, poolB, 100, factory );
}
/*---------------------------------------------------------------
* FixedSizePool vs VariableSizePool TestCases
*-------------------------------------------------------------*/
/**
* Compare the FixedSizePool and VariableSizePool when the
* VariableSizePool is configured to act like a FixedSizePool.
* <p>
* Test will use pools with a max size of 100, while getting up to 100 at
a time,
* Poolables are small objects.
*/
public void
testCompare_FixedSizePool_And_VariableSizePool_Max100_Gets100_SmallPoolables()
throws Exception
{
String name =
"FixedSizePool_And_VariableSizePool_Max100_Gets100_SmallPoolables";
Class poolableClass = SmallPoolable.class;
ClassInstanceObjectFactory factory = new ClassInstanceObjectFactory(
poolableClass, m_poolLogger );
int max = 100;
FixedSizePool poolA = new FixedSizePool( factory, max );
VariableSizePool poolB = new VariableSizePool( factory, max );
generalTest( name, poolA, poolB, 100, factory );
}
/**
* Compare the FixedSizePool and VariableSizePool when the
* VariableSizePool is configured to act like a FixedSizePool.
* <p>
* Test will use pools with a max size of 100, while getting up to 100 at
a time,
* Poolables are medium objects.
*/
public void
testCompare_FixedSizePool_And_VariableSizePool_Max100_Gets100_MediumPoolables()
throws Exception
{
String name =
"FixedSizePool_And_VariableSizePool_Max100_Gets100_MediumPoolables";
Class poolableClass = MediumPoolable.class;
ClassInstanceObjectFactory factory = new ClassInstanceObjectFactory(
poolableClass, m_poolLogger );
int max = 100;
FixedSizePool poolA = new FixedSizePool( factory, max );
VariableSizePool poolB = new VariableSizePool( factory, max );
generalTest( name, poolA, poolB, 100, factory );
}
/**
* Compare the FixedSizePool and VariableSizePool when the
* VariableSizePool is configured to act like a FixedSizePool.
* <p>
* Test will use pools with a max size of 100, while getting up to 100 at
a time,
* Poolables are large objects.
*/
public void
testCompare_FixedSizePool_And_VariableSizePool_Max100_Gets100_LargePoolables()
throws Exception
{
String name =
"FixedSizePool_And_VariableSizePool_Max100_Gets100_LargePoolables";
Class poolableClass = LargePoolable.class;
ClassInstanceObjectFactory factory = new ClassInstanceObjectFactory(
poolableClass, m_poolLogger );
int max = 100;
FixedSizePool poolA = new FixedSizePool( factory, max );
VariableSizePool poolB = new VariableSizePool( factory, max );
generalTest( name, poolA, poolB, 100, factory );
}
/*---------------------------------------------------------------
* Test Classes
*-------------------------------------------------------------*/
public static class SmallPoolable
implements org.apache.avalon.excalibur.pool.Poolable
{
int a;
}
public static class MediumPoolable
implements org.apache.avalon.excalibur.pool.Poolable
{
int[] a = new int[ 100 ];
}
public static class LargePoolable
implements org.apache.avalon.excalibur.pool.Poolable
{
int[][] a = new int[ 10 ][ 100 ];
}
/**
* Dummy class used for timing test cases where no pooling is done.
*/
public static class NoPoolingPool
implements Pool, LogEnabled
{
private ObjectFactory m_factory;
private Logger m_logger;
public NoPoolingPool( ObjectFactory factory )
{
m_factory = factory;
}
public void enableLogging( Logger logger )
{
m_logger = logger;
}
public Object acquire() throws Exception
{
return newInstance();
}
public void release( Object poolable )
{
try
{
m_factory.dispose( poolable );
}
catch( Exception e )
{
m_logger.debug( "Error decommissioning object", e );
}
}
public Object newInstance() throws Exception
{
return m_factory.newInstance();
}
}
/*---------------------------------------------------------------
* VariableSizePool vs ResourceLimitingPool TestCases
*-------------------------------------------------------------*/
/**
* Compare the VariableSizePool and ResourceLimitingPool when the
* ResourceLimitingPool is configured to act like a VariableSizePool.
* <p>
* Test will use pools with a max size of 100, while getting up to 100 at
a time,
* Poolables are small objects.
*/
public void
testCompare_VariableSizePool_And_ResourceLimitingPool_Max100_Gets100_SmallPoolables()
throws Exception
{
String name =
"VariableSizePool_And_ResourceLimitingPool_Max100_Gets100_SmallPoolables";
Class poolableClass = SmallPoolable.class;
ClassInstanceObjectFactory factory = new ClassInstanceObjectFactory(
poolableClass, m_poolLogger );
int min = 0;
int max = 100;
boolean maxStrict = false;
boolean blocking = false;
long blockTimeout = 0;
long trimInterval = 0;
VariableSizePool poolA = new VariableSizePool( factory, max );
ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max,
maxStrict, blocking, blockTimeout, trimInterval );
poolB.enableLogging( m_poolLogger );
generalTest( name, poolA, poolB, 100, factory );
}
/**
* Compare the VariableSizePool and ResourceLimitingPool when the
* ResourceLimitingPool is configured to act like a VariableSizePool.
* <p>
* Test will use pools with a max size of 100, while getting up to 200 at
a time,
* Poolables are small objects.
*/
public void
testCompare_VariableSizePool_And_ResourceLimitingPool_Max100_Gets200_SmallPoolables()
throws Exception
{
String name =
"VariableSizePool_And_ResourceLimitingPool_Max100_Gets200_SmallPoolables";
Class poolableClass = SmallPoolable.class;
ClassInstanceObjectFactory factory = new ClassInstanceObjectFactory(
poolableClass, m_poolLogger );
int min = 0;
int max = 100;
boolean maxStrict = false;
boolean blocking = false;
long blockTimeout = 0;
long trimInterval = 0;
VariableSizePool poolA = new VariableSizePool( factory, max );
ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max,
maxStrict, blocking, blockTimeout, trimInterval );
poolB.enableLogging( m_poolLogger );
generalTest( name, poolA, poolB, 200, factory );
}
/**
* Compare the VariableSizePool and ResourceLimitingPool when the
* ResourceLimitingPool is configured to act like a VariableSizePool.
* <p>
* Test will use pools with a max size of 100, while getting up to 100 at
a time,
* Poolables are medium objects.
*/
public void
testCompare_VariableSizePool_And_ResourceLimitingPool_Max100_Gets100_MediumPoolables()
throws Exception
{
String name =
"VariableSizePool_And_ResourceLimitingPool_Max10_Gets100_MediumPoolables";
Class poolableClass = MediumPoolable.class;
ClassInstanceObjectFactory factory = new ClassInstanceObjectFactory(
poolableClass, m_poolLogger );
int min = 0;
int max = 100;
boolean maxStrict = false;
boolean blocking = false;
long blockTimeout = 0;
long trimInterval = 0;
VariableSizePool poolA = new VariableSizePool( factory, max );
ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max,
maxStrict, blocking, blockTimeout, trimInterval );
poolB.enableLogging( m_poolLogger );
generalTest( name, poolA, poolB, 100, factory );
}
/**
* Compare the VariableSizePool and ResourceLimitingPool when the
* ResourceLimitingPool is configured to act like a VariableSizePool.
* <p>
* Test will use pools with a max size of 100, while getting up to 200 at
a time,
* Poolables are medium objects.
*/
public void
testCompare_VariableSizePool_And_ResourceLimitingPool_Max100_Gets200_MediumPoolables()
throws Exception
{
String name =
"VariableSizePool_And_ResourceLimitingPool_Max100_Gets200_MediumPoolables";
Class poolableClass = MediumPoolable.class;
ClassInstanceObjectFactory factory = new ClassInstanceObjectFactory(
poolableClass, m_poolLogger );
int min = 0;
int max = 100;
boolean maxStrict = false;
boolean blocking = false;
long blockTimeout = 0;
long trimInterval = 0;
VariableSizePool poolA = new VariableSizePool( factory, max );
ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max,
maxStrict, blocking, blockTimeout, trimInterval );
poolB.enableLogging( m_poolLogger );
generalTest( name, poolA, poolB, 200, factory );
}
/**
* Compare the VariableSizePool and ResourceLimitingPool when the
* ResourceLimitingPool is configured to act like a VariableSizePool.
* <p>
* Test will use pools with a max size of 100, while getting up to 100 at
a time,
* Poolables are large objects.
*/
public void
testCompare_VariableSizePool_And_ResourceLimitingPool_Max100_Gets100_LargePoolables()
throws Exception
{
String name =
"VariableSizePool_And_ResourceLimitingPool_Max100_Gets100_LargePoolables";
Class poolableClass = LargePoolable.class;
ClassInstanceObjectFactory factory = new ClassInstanceObjectFactory(
poolableClass, m_poolLogger );
int min = 0;
int max = 100;
boolean maxStrict = false;
boolean blocking = false;
long blockTimeout = 0;
long trimInterval = 0;
VariableSizePool poolA = new VariableSizePool( factory, max );
ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max,
maxStrict, blocking, blockTimeout, trimInterval );
poolB.enableLogging( m_poolLogger );
generalTest( name, poolA, poolB, 100, factory );
}
/**
* Compare the VariableSizePool and ResourceLimitingPool when the
* ResourceLimitingPool is configured to act like a VariableSizePool.
* <p>
* Test will use pools with a max size of 100, while getting up to 200 at
a time,
* Poolables are large objects.
*/
public void
testCompare_VariableSizePool_And_ResourceLimitingPool_Max100_Gets200_LargePoolables()
throws Exception
{
String name =
"VariableSizePool_And_ResourceLimitingPool_Max100_Gets200_LargePoolables";
Class poolableClass = LargePoolable.class;
ClassInstanceObjectFactory factory = new ClassInstanceObjectFactory(
poolableClass, m_poolLogger );
int min = 0;
int max = 100;
boolean maxStrict = false;
boolean blocking = false;
long blockTimeout = 0;
long trimInterval = 0;
VariableSizePool poolA = new VariableSizePool( factory, max );
ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max,
maxStrict, blocking, blockTimeout, trimInterval );
poolB.enableLogging( m_poolLogger );
generalTest( name, poolA, poolB, 200, factory );
}
/*---------------------------------------------------------------
* Utility Methods
*-------------------------------------------------------------*/
protected void resetMemory()
{
System.gc();
System.gc();
// Let the system settle down.
try
{
Thread.sleep( 50 );
}
catch( InterruptedException e )
{
}
Runtime runtime = Runtime.getRuntime();
m_logger.debug( "Memory: " + ( runtime.totalMemory() -
runtime.freeMemory() ) );
}
protected String getShortClassName( Object o )
{
String name = o.getClass().getName();
int pos = name.lastIndexOf( '.' );
if( pos > 0 )
{
name = name.substring( pos + 1 );
}
return name;
}
protected abstract long getPoolRunTime( Pool pool, int gets )
throws Exception;
protected abstract long getPoolRunTime(
org.apache.avalon.excalibur.pool.Pool pool, int gets )
throws Exception;
/**
* The guts of the various test cases. Will dispose the pools
*/
protected void generalTest( String name, Pool poolA, Pool poolB, int
gets, ClassInstanceObjectFactory factory )
throws Exception
{
m_logger.info( "Test Case: " + name );
// Get the short class names
final String poolAName = getShortClassName( poolA );
final String poolBName = getShortClassName( poolB );
// Start clean
resetMemory();
// Get a baseline speed for object creation
NoPoolingPool poolBase = new NoPoolingPool( factory );
poolBase.enableLogging( m_poolLogger );
final long noPoolDuration = getPoolRunTime( poolBase, gets );
m_logger.info( " Unpooled time = " + noPoolDuration + "ms. to use
" + TEST_SIZE + " objects." );
resetMemory();
// Get the time for poolA
final long poolADuration = getPoolRunTime( poolA, gets );
m_logger.info( " " + poolAName + " time = " + poolADuration +
"ms. to use " + TEST_SIZE + " objects, " + gets + " at a time." );
resetMemory();
// Get the time for poolB
final long poolBDuration = getPoolRunTime( poolB, gets );
m_logger.info( " " + poolBName + " time = " + poolBDuration +
"ms. to use " + TEST_SIZE + " objects, " + gets + " at a time." );
resetMemory();
// Show a summary
if( m_logger.isInfoEnabled() )
{
double mult;
mult = ( poolADuration > 0 ? ( noPoolDuration * 100 /
poolADuration ) / 100.0 : Float.POSITIVE_INFINITY );
m_logger.info( " => " + poolAName + " is " + mult + " X as fast
as not pooling." );
mult = ( poolBDuration > 0 ? ( noPoolDuration * 100 /
poolBDuration ) / 100.0 : Float.POSITIVE_INFINITY );
m_logger.info( " => " + poolBName + " is " + mult + " X as fast
as not pooling." );
mult = ( poolBDuration > 0 ? ( poolADuration * 100 /
poolBDuration ) / 100.0 : Float.POSITIVE_INFINITY );
m_logger.info( " => " + poolBName + " is " + mult + " X as fast
as " + poolAName + "." );
mult = ( poolADuration > 0 ? ( poolBDuration * 100 /
poolADuration ) / 100.0 : Float.POSITIVE_INFINITY );
m_logger.info( " => " + poolAName + " is " + mult + " X as fast
as " + poolBName + "." );
}
}
/**
* The guts of the various test cases. Will dispose the pools
*/
protected void generalTest( String name, Pool poolA,
org.apache.avalon.excalibur.pool.Pool poolB, int gets,
ClassInstanceObjectFactory factory )
throws Exception
{
m_logger.info( "Test Case: " + name );
// Get the short class names
final String poolAName = getShortClassName( poolA );
final String poolBName = getShortClassName( poolB );
// Start clean
resetMemory();
// Get a baseline speed for object creation
NoPoolingPool poolBase = new NoPoolingPool( factory );
poolBase.enableLogging( m_poolLogger );
final long noPoolDuration = getPoolRunTime( poolBase, gets );
m_logger.info( " Unpooled time = " + noPoolDuration + "ms. to use
" + TEST_SIZE + " objects." );
resetMemory();
// Get the time for poolA
final long poolADuration = getPoolRunTime( poolA, gets );
m_logger.info( " " + poolAName + " time = " + poolADuration +
"ms. to use " + TEST_SIZE + " objects, " + gets + " at a time." );
resetMemory();
// Get the time for poolB
final long poolBDuration = getPoolRunTime( poolB, gets );
m_logger.info( " " + poolBName + " time = " + poolBDuration +
"ms. to use " + TEST_SIZE + " objects, " + gets + " at a time." );
resetMemory();
// Show a summary
if( m_logger.isInfoEnabled() )
{
double mult;
mult = ( poolADuration > 0 ? ( noPoolDuration * 100 /
poolADuration ) / 100.0 : Float.POSITIVE_INFINITY );
m_logger.info( " => " + poolAName + " is " + mult + " X as fast
as not pooling." );
mult = ( poolBDuration > 0 ? ( noPoolDuration * 100 /
poolBDuration ) / 100.0 : Float.POSITIVE_INFINITY );
m_logger.info( " => " + poolBName + " is " + mult + " X as fast
as not pooling." );
mult = ( poolBDuration > 0 ? ( poolADuration * 100 /
poolBDuration ) / 100.0 : Float.POSITIVE_INFINITY );
m_logger.info( " => " + poolBName + " is " + mult + " X as fast
as " + poolAName + "." );
mult = ( poolADuration > 0 ? ( poolBDuration * 100 /
poolADuration ) / 100.0 : Float.POSITIVE_INFINITY );
m_logger.info( " => " + poolAName + " is " + mult + " X as fast
as " + poolBName + "." );
}
}
}
1.1
jakarta-avalon-excalibur/event/src/test/org/apache/excalibur/mpool/test/SingleThreadedPoolComparisonProfile.java
Index: SingleThreadedPoolComparisonProfile.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software
Foundation"
must not be used to endorse or promote products derived from this
software
without prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation. For more information on the
Apache Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.excalibur.mpool.test;
import org.apache.avalon.excalibur.pool.Poolable;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.excalibur.mpool.Pool;
/**
* This is used to profile and compare various pool implementations
* given a single access thread.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Leif Mortenson</a>
* @version $Id: SingleThreadedPoolComparisonProfile.java,v 1.1 2002/08/07
22:44:26 bloritsch Exp $
*/
public class SingleThreadedPoolComparisonProfile
extends PoolComparisonProfileAbstract
{
/*---------------------------------------------------------------
* Constructors
*-------------------------------------------------------------*/
public SingleThreadedPoolComparisonProfile( String name )
{
super( name );
}
/*---------------------------------------------------------------
* PoolComparisonProfileAbstract Methods
*-------------------------------------------------------------*/
protected long getPoolRunTime( Pool pool, int gets )
throws Exception
{
// Start clean
resetMemory();
final long startTime = System.currentTimeMillis();
final Object[] poolTmp = new Object[ gets ];
final int loops = TEST_SIZE / gets;
for( int i = 0; i < loops; i++ )
{
// Get some Poolables
for( int j = 0; j < gets; j++ )
{
poolTmp[ j ] = pool.acquire();
}
// Put the Poolables back
for( int j = 0; j < gets; j++ )
{
pool.release( poolTmp[ j ] );
poolTmp[ j ] = null;
}
}
final long duration = System.currentTimeMillis() - startTime;
// Dispose if necessary
if( pool instanceof Disposable )
{
( (Disposable)pool ).dispose();
}
return duration;
}
/*---------------------------------------------------------------
* PoolComparisonProfileAbstract Methods
*-------------------------------------------------------------*/
protected long getPoolRunTime( org.apache.avalon.excalibur.pool.Pool
pool, int gets )
throws Exception
{
// Start clean
resetMemory();
final long startTime = System.currentTimeMillis();
final Poolable[] poolTmp = new Poolable[ gets ];
final int loops = TEST_SIZE / gets;
for( int i = 0; i < loops; i++ )
{
// Get some Poolables
for( int j = 0; j < gets; j++ )
{
poolTmp[ j ] = pool.get();
}
// Put the Poolables back
for( int j = 0; j < gets; j++ )
{
pool.put( poolTmp[ j ] );
poolTmp[ j ] = null;
}
}
final long duration = System.currentTimeMillis() - startTime;
// Dispose if necessary
if( pool instanceof Disposable )
{
( (Disposable)pool ).dispose();
}
return duration;
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>