1 19 package org.apache.avalon.excalibur.pool.test; 20 21 import junit.framework.TestCase; 22 23 import org.apache.avalon.excalibur.pool.HardResourceLimitingPool; 24 import org.apache.avalon.excalibur.pool.ObjectFactory; 25 import org.apache.avalon.excalibur.pool.Pool; 26 import org.apache.avalon.excalibur.pool.Poolable; 27 import org.apache.avalon.excalibur.pool.ResourceLimitingPool; 28 import org.apache.avalon.excalibur.pool.SoftResourceLimitingPool; 29 30 import org.apache.avalon.framework.logger.LogEnabled; 31 import org.apache.avalon.framework.logger.ConsoleLogger; 32 import org.apache.avalon.framework.logger.Logger; 33 34 40 public abstract class PoolComparisonProfileAbstract 41 extends TestCase 42 { 43 47 protected static final int TEST_SIZE = 50000; 48 49 protected Logger m_logger; 50 protected Logger m_poolLogger; 51 52 55 public PoolComparisonProfileAbstract( String name ) 56 { 57 super( name ); 58 59 m_logger = new ConsoleLogger( ConsoleLogger.LEVEL_INFO ); 60 m_poolLogger = m_logger.getChildLogger( "pool" ); 61 } 62 63 66 73 public void testCompare_SoftResourceLimitingPool_And_ResourceLimitingPool_Max100_Gets100_SmallPoolables() 74 throws Exception 75 { 76 String name = "SoftResourceLimitingPool_And_ResourceLimitingPool_Max10_Gets10_SmallPoolables"; 77 78 Class poolableClass = SmallPoolable.class; 79 ObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger ); 80 int min = 0; 81 int max = 100; 82 boolean maxStrict = false; 83 boolean blocking = false; 84 long blockTimeout = 0; 85 long trimInterval = 0; 86 87 SoftResourceLimitingPool poolA = new SoftResourceLimitingPool( factory, min, max ); 88 poolA.enableLogging( m_poolLogger ); 89 poolA.initialize(); 90 91 ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max, maxStrict, blocking, blockTimeout, trimInterval ); 92 poolB.enableLogging( m_poolLogger ); 93 94 generalTest( name, poolA, poolB, 100, factory ); 95 } 96 97 104 public void testCompare_SoftResourceLimitingPool_And_ResourceLimitingPool_Max100_Gets200_SmallPoolables() 105 throws Exception 106 { 107 String name = "SoftResourceLimitingPool_And_ResourceLimitingPool_Max100_Gets200_SmallPoolables"; 108 109 Class poolableClass = SmallPoolable.class; 110 ObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger ); 111 int min = 0; 112 int max = 100; 113 boolean maxStrict = false; 114 boolean blocking = false; 115 long blockTimeout = 0; 116 long trimInterval = 0; 117 118 SoftResourceLimitingPool poolA = new SoftResourceLimitingPool( factory, min, max ); 119 poolA.enableLogging( m_poolLogger ); 120 poolA.initialize(); 121 122 ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max, maxStrict, blocking, blockTimeout, trimInterval ); 123 poolB.enableLogging( m_poolLogger ); 124 125 generalTest( name, poolA, poolB, 200, factory ); 126 } 127 128 135 public void testCompare_SoftResourceLimitingPool_And_ResourceLimitingPool_Max100_Gets100_MediumPoolables() 136 throws Exception 137 { 138 String name = "SoftResourceLimitingPool_And_ResourceLimitingPool_Max100_Gets100_MediumPoolables"; 139 140 Class poolableClass = MediumPoolable.class; 141 ObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger ); 142 int min = 0; 143 int max = 100; 144 boolean maxStrict = false; 145 boolean blocking = false; 146 long blockTimeout = 0; 147 long trimInterval = 0; 148 149 SoftResourceLimitingPool poolA = new SoftResourceLimitingPool( factory, min, max ); 150 poolA.enableLogging( m_poolLogger ); 151 poolA.initialize(); 152 153 ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max, maxStrict, blocking, blockTimeout, trimInterval ); 154 poolB.enableLogging( m_poolLogger ); 155 156 generalTest( name, poolA, poolB, 100, factory ); 157 } 158 159 166 public void testCompare_SoftResourceLimitingPool_And_ResourceLimitingPool_Max100_Gets200_MediumPoolables() 167 throws Exception 168 { 169 String name = "SoftResourceLimitingPool_And_ResourceLimitingPool_Max100_Gets200_MediumPoolables"; 170 171 Class poolableClass = MediumPoolable.class; 172 ObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger ); 173 int min = 0; 174 int max = 100; 175 boolean maxStrict = false; 176 boolean blocking = false; 177 long blockTimeout = 0; 178 long trimInterval = 0; 179 180 SoftResourceLimitingPool poolA = new SoftResourceLimitingPool( factory, min, max ); 181 poolA.enableLogging( m_poolLogger ); 182 poolA.initialize(); 183 184 ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max, maxStrict, blocking, blockTimeout, trimInterval ); 185 poolB.enableLogging( m_poolLogger ); 186 187 generalTest( name, poolA, poolB, 200, factory ); 188 } 189 190 197 public void testCompare_SoftResourceLimitingPool_And_ResourceLimitingPool_Max100_Gets100_LargePoolables() 198 throws Exception 199 { 200 String name = "SoftResourceLimitingPool_And_ResourceLimitingPool_Max100_Gets100_LargePoolables"; 201 202 Class poolableClass = LargePoolable.class; 203 ObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger ); 204 int min = 0; 205 int max = 100; 206 boolean maxStrict = false; 207 boolean blocking = false; 208 long blockTimeout = 0; 209 long trimInterval = 0; 210 211 SoftResourceLimitingPool poolA = new SoftResourceLimitingPool( factory, min, max ); 212 poolA.enableLogging( m_poolLogger ); 213 poolA.initialize(); 214 215 ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max, maxStrict, blocking, blockTimeout, trimInterval ); 216 poolB.enableLogging( m_poolLogger ); 217 218 generalTest( name, poolA, poolB, 100, factory ); 219 } 220 221 228 public void testCompare_SoftResourceLimitingPool_And_ResourceLimitingPool_Max100_Gets200_LargePoolables() 229 throws Exception 230 { 231 String name = "SoftResourceLimitingPool_And_ResourceLimitingPool_Max100_Gets200_LargePoolables"; 232 233 Class poolableClass = LargePoolable.class; 234 ObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger ); 235 int min = 0; 236 int max = 100; 237 boolean maxStrict = false; 238 boolean blocking = false; 239 long blockTimeout = 0; 240 long trimInterval = 0; 241 242 SoftResourceLimitingPool poolA = new SoftResourceLimitingPool( factory, min, max ); 243 poolA.enableLogging( m_poolLogger ); 244 poolA.initialize(); 245 246 ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max, maxStrict, blocking, blockTimeout, trimInterval ); 247 poolB.enableLogging( m_poolLogger ); 248 249 generalTest( name, poolA, poolB, 200, factory ); 250 } 251 252 255 262 public void testCompare_HardResourceLimitingPool_And_ResourceLimitingPool_Max100_Gets100_SmallPoolables() 263 throws Exception 264 { 265 String name = "HardResourceLimitingPool_And_ResourceLimitingPool_Max100_Gets100_SmallPoolables"; 266 267 Class poolableClass = SmallPoolable.class; 268 ObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger ); 269 int min = 0; 270 int max = 100; 271 boolean maxStrict = true; 272 boolean blocking = false; 273 long blockTimeout = 0; 274 long trimInterval = 0; 275 276 HardResourceLimitingPool poolA = new HardResourceLimitingPool( factory, min, max ); 277 poolA.enableLogging( m_poolLogger ); 278 poolA.initialize(); 279 280 ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max, maxStrict, blocking, blockTimeout, trimInterval ); 281 poolB.enableLogging( m_poolLogger ); 282 283 generalTest( name, poolA, poolB, 100, factory ); 284 } 285 286 293 public void testCompare_HardResourceLimitingPool_And_ResourceLimitingPool_Max10_Gets10_MediumPoolables() 294 throws Exception 295 { 296 String name = "HardResourceLimitingPool_And_ResourceLimitingPool_Max100_Gets100_MediumPoolables"; 297 298 Class poolableClass = MediumPoolable.class; 299 ObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger ); 300 int min = 0; 301 int max = 100; 302 boolean maxStrict = true; 303 boolean blocking = false; 304 long blockTimeout = 0; 305 long trimInterval = 0; 306 307 HardResourceLimitingPool poolA = new HardResourceLimitingPool( factory, min, max ); 308 poolA.enableLogging( m_poolLogger ); 309 poolA.initialize(); 310 311 ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max, maxStrict, blocking, blockTimeout, trimInterval ); 312 poolB.enableLogging( m_poolLogger ); 313 314 generalTest( name, poolA, poolB, 100, factory ); 315 } 316 317 324 public void testCompare_HardResourceLimitingPool_And_ResourceLimitingPool_Max100_Gets100_LargePoolables() 325 throws Exception 326 { 327 String name = "HardResourceLimitingPool_And_ResourceLimitingPool_Max100_Gets100_LargePoolables"; 328 329 Class poolableClass = LargePoolable.class; 330 ObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger ); 331 int min = 0; 332 int max = 100; 333 boolean maxStrict = true; 334 boolean blocking = false; 335 long blockTimeout = 0; 336 long trimInterval = 0; 337 338 HardResourceLimitingPool poolA = new HardResourceLimitingPool( factory, min, max ); 339 poolA.enableLogging( m_poolLogger ); 340 poolA.initialize(); 341 342 ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max, maxStrict, blocking, blockTimeout, trimInterval ); 343 poolB.enableLogging( m_poolLogger ); 344 345 generalTest( name, poolA, poolB, 100, factory ); 346 } 347 348 351 public static class SmallPoolable 352 implements Poolable 353 { 354 int a; 355 } 356 357 public static class MediumPoolable 358 implements Poolable 359 { 360 int[] a = new int[ 100 ]; 361 } 362 363 public static class LargePoolable 364 implements Poolable 365 { 366 int[][] a = new int[ 10 ][ 100 ]; 367 } 368 369 372 public static class NoPoolingPool 373 implements Pool, LogEnabled 374 { 375 private ObjectFactory m_factory; 376 private Logger m_logger; 377 378 public NoPoolingPool( ObjectFactory factory ) 379 { 380 m_factory = factory; 381 } 382 383 public void enableLogging( Logger logger ) 384 { 385 m_logger = logger; 386 } 387 388 public Poolable get() throws Exception 389 { 390 return (Poolable)m_factory.newInstance(); 391 } 392 393 public void put( Poolable poolable ) 394 { 395 try 396 { 397 m_factory.decommission( poolable ); 398 } 399 catch( Exception e ) 400 { 401 m_logger.debug( "Error decommissioning object", e ); 402 } 403 } 404 } 405 406 409 protected void resetMemory() 410 { 411 System.gc(); 412 System.gc(); 413 414 try 416 { 417 Thread.sleep( 50 ); 418 } 419 catch( InterruptedException e ) 420 { 421 } 422 Runtime runtime = Runtime.getRuntime(); 423 m_logger.debug( "Memory: " + ( runtime.totalMemory() - runtime.freeMemory() ) ); 424 } 425 426 protected String getShortClassName( Object o ) 427 { 428 String name = o.getClass().getName(); 429 int pos = name.lastIndexOf( '.' ); 430 if( pos > 0 ) 431 { 432 name = name.substring( pos + 1 ); 433 } 434 return name; 435 } 436 437 protected abstract long getPoolRunTime( Pool pool, int gets ) 438 throws Exception ; 439 440 443 protected void generalTest( String name, Pool poolA, Pool poolB, int gets, ObjectFactory factory ) 444 throws Exception 445 { 446 m_logger.info( "Test Case: " + name ); 447 448 final String poolAName = getShortClassName( poolA ); 450 final String poolBName = getShortClassName( poolB ); 451 452 resetMemory(); 454 455 NoPoolingPool poolBase = new NoPoolingPool( factory ); 457 poolBase.enableLogging( m_poolLogger ); 458 final long noPoolDuration = getPoolRunTime( poolBase, gets ); 459 m_logger.info( " Unpooled time = " + noPoolDuration + "ms. to use " + TEST_SIZE + " objects." ); 460 resetMemory(); 461 462 463 final long poolADuration = getPoolRunTime( poolA, gets ); 465 m_logger.info( " " + poolAName + " time = " + poolADuration + "ms. to use " + TEST_SIZE + " objects, " + gets + " at a time." ); 466 resetMemory(); 467 468 469 final long poolBDuration = getPoolRunTime( poolB, gets ); 471 m_logger.info( " " + poolBName + " time = " + poolBDuration + "ms. to use " + TEST_SIZE + " objects, " + gets + " at a time." ); 472 resetMemory(); 473 474 if( m_logger.isInfoEnabled() ) 476 { 477 double mult; 478 mult = ( poolADuration > 0 ? ( noPoolDuration * 100 / poolADuration ) / 100.0 : Float.POSITIVE_INFINITY ); 479 m_logger.info( " => " + poolAName + " is " + mult + " X as fast as not pooling." ); 480 481 mult = ( poolBDuration > 0 ? ( noPoolDuration * 100 / poolBDuration ) / 100.0 : Float.POSITIVE_INFINITY ); 482 m_logger.info( " => " + poolBName + " is " + mult + " X as fast as not pooling." ); 483 484 mult = ( poolBDuration > 0 ? ( poolADuration * 100 / poolBDuration ) / 100.0 : Float.POSITIVE_INFINITY ); 485 m_logger.info( " => " + poolBName + " is " + mult + " X as fast as " + poolAName + "." ); 486 } 487 } 488 } 489 | Popular Tags |