1 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 32 public class MultiThreadedPoolComparisonProfile 33 extends PoolComparisonProfileAbstract 34 { 35 protected static final int THREADS = 100; 36 37 protected Object m_semaphore = new Object (); 38 protected int m_getCount; 39 protected Throwable m_throwable; 40 41 44 public MultiThreadedPoolComparisonProfile( String name ) 45 { 46 super( name ); 47 } 48 49 52 protected long getPoolRunTime( final Pool pool, final int gets ) 53 throws Exception 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 Runnable runnable = new Runnable () 65 { 66 public void run() 67 { 68 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 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 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 Thread.yield(); 102 103 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 t ) 126 { 127 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 if( pool instanceof Disposable ) 144 { 145 ( (Disposable)pool ).dispose(); 146 } 147 148 return duration; 149 } 150 } 151 | Popular Tags |