1 17 package org.apache.excalibur.mpool.test; 18 19 import junit.framework.TestCase; 20 21 import org.apache.avalon.excalibur.pool.ResourceLimitingPool; 22 import org.apache.avalon.framework.logger.LogEnabled; 23 import org.apache.avalon.framework.logger.LogKitLogger; 24 import org.apache.avalon.framework.logger.Logger; 25 import org.apache.excalibur.mpool.BlockingFixedSizePool; 26 import org.apache.excalibur.mpool.FixedSizePool; 27 import org.apache.excalibur.mpool.ObjectFactory; 28 import org.apache.excalibur.mpool.Pool; 29 import org.apache.excalibur.mpool.VariableSizePool; 30 31 37 public abstract class PoolComparisonProfileAbstract 38 extends TestCase 39 { 40 44 protected static final int TEST_SIZE = 50000; 45 46 protected Logger m_logger; 47 protected Logger m_poolLogger; 48 49 52 public PoolComparisonProfileAbstract( String name ) 53 { 54 super( name ); 55 56 org.apache.log.Logger logger = 58 org.apache.log.Hierarchy.getDefaultHierarchy().getLoggerFor( "test" ); 59 logger.setPriority( org.apache.log.Priority.INFO ); 60 m_logger = new LogKitLogger( logger ); 61 62 org.apache.log.Logger poolLogger = 64 org.apache.log.Hierarchy.getDefaultHierarchy().getLoggerFor( "pool" ); 65 poolLogger.setPriority( org.apache.log.Priority.INFO ); 66 m_poolLogger = new LogKitLogger( poolLogger ); 67 } 68 69 72 79 public void testCompare_FixedSizePool_And_ResourceLimitingPool_Max100_Gets100_SmallPoolables() 80 throws Exception 81 { 82 String name = "FixedSizePool_And_ResourceLimitingPool_Max100_Gets100_SmallPoolables"; 83 84 Class poolableClass = SmallPoolable.class; 85 ClassInstanceObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger ); 86 int max = 100; 87 boolean maxStrict = true; 88 boolean blocking = false; 89 long blockTimeout = 0; 90 long trimInterval = 0; 91 92 FixedSizePool poolA = new FixedSizePool( factory, max ); 93 94 ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max, maxStrict, blocking, blockTimeout, trimInterval ); 95 poolB.enableLogging( m_poolLogger ); 96 97 generalTest( name, poolA, poolB, 100, factory ); 98 } 99 100 107 public void testCompare_FixedSizePool_And_BlockingFixedSizePool_Max100_Gets100_SmallPoolables() 108 throws Exception 109 { 110 String name = "FixedSizePool_And_BlockingFixedSizePool_Max100_Gets100_SmallPoolables"; 111 112 Class poolableClass = SmallPoolable.class; 113 ClassInstanceObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger ); 114 int max = 100; 115 long blockTimeout = 1; 116 117 FixedSizePool poolA = new FixedSizePool( factory, max ); 118 BlockingFixedSizePool poolB = new BlockingFixedSizePool( factory, max, blockTimeout ); 119 poolB.initialize(); 120 121 generalTest( name, poolA, poolB, 100, factory ); 122 } 123 124 131 public void testCompare_FixedSizePool_And_BlockingFixedSizePool_Max100_Gets100_MediumPoolables() 132 throws Exception 133 { 134 String name = "FixedSizePool_And_BlockingFixedSizePool_Max100_Gets100_MediumPoolables"; 135 136 Class poolableClass = MediumPoolable.class; 137 ClassInstanceObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger ); 138 int max = 100; 139 long blockTimeout = 1; 140 141 FixedSizePool poolA = new FixedSizePool( factory, max ); 142 BlockingFixedSizePool poolB = new BlockingFixedSizePool( factory, max, blockTimeout ); 143 poolB.initialize(); 144 145 generalTest( name, poolA, poolB, 100, factory ); 146 } 147 148 155 public void testCompare_FixedSizePool_And_BlockingFixedSizePool_Max100_Gets100_LargePoolables() 156 throws Exception 157 { 158 String name = "FixedSizePool_And_BlockingFixedSizePool_Max100_Gets100_LargePoolables"; 159 160 Class poolableClass = LargePoolable.class; 161 ClassInstanceObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger ); 162 int max = 100; 163 long blockTimeout = 1; 164 165 FixedSizePool poolA = new FixedSizePool( factory, max ); 166 BlockingFixedSizePool poolB = new BlockingFixedSizePool( factory, max, blockTimeout ); 167 poolB.initialize(); 168 169 generalTest( name, poolA, poolB, 100, factory ); 170 } 171 172 179 public void testCompare_FixedSizePool_And_ResourceLimitingPool_Max100_Gets100_LargePoolables() 180 throws Exception 181 { 182 String name = "FixedSizePool_And_ResourceLimitingPool_Max100_Gets100_LargePoolables"; 183 184 Class poolableClass = LargePoolable.class; 185 ClassInstanceObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger ); 186 int max = 100; 187 boolean maxStrict = true; 188 boolean blocking = false; 189 long blockTimeout = 0; 190 long trimInterval = 0; 191 192 FixedSizePool poolA = new FixedSizePool( factory, max ); 193 194 ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max, maxStrict, blocking, blockTimeout, trimInterval ); 195 poolB.enableLogging( m_poolLogger ); 196 197 generalTest( name, poolA, poolB, 100, factory ); 198 } 199 200 203 210 public void testCompare_FixedSizePool_And_VariableSizePool_Max100_Gets100_SmallPoolables() 211 throws Exception 212 { 213 String name = "FixedSizePool_And_VariableSizePool_Max100_Gets100_SmallPoolables"; 214 215 Class poolableClass = SmallPoolable.class; 216 ClassInstanceObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger ); 217 int max = 100; 218 219 FixedSizePool poolA = new FixedSizePool( factory, max ); 220 221 VariableSizePool poolB = new VariableSizePool( factory, max ); 222 223 generalTest( name, poolA, poolB, 100, factory ); 224 } 225 226 233 public void testCompare_FixedSizePool_And_VariableSizePool_Max100_Gets100_MediumPoolables() 234 throws Exception 235 { 236 String name = "FixedSizePool_And_VariableSizePool_Max100_Gets100_MediumPoolables"; 237 238 Class poolableClass = MediumPoolable.class; 239 ClassInstanceObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger ); 240 int max = 100; 241 242 FixedSizePool poolA = new FixedSizePool( factory, max ); 243 244 VariableSizePool poolB = new VariableSizePool( factory, max ); 245 246 generalTest( name, poolA, poolB, 100, factory ); 247 } 248 249 256 public void testCompare_FixedSizePool_And_VariableSizePool_Max100_Gets100_LargePoolables() 257 throws Exception 258 { 259 String name = "FixedSizePool_And_VariableSizePool_Max100_Gets100_LargePoolables"; 260 261 Class poolableClass = LargePoolable.class; 262 ClassInstanceObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger ); 263 int max = 100; 264 265 FixedSizePool poolA = new FixedSizePool( factory, max ); 266 267 VariableSizePool poolB = new VariableSizePool( factory, max ); 268 269 generalTest( name, poolA, poolB, 100, factory ); 270 } 271 272 275 public static class SmallPoolable 276 implements org.apache.avalon.excalibur.pool.Poolable 277 { 278 int a; 279 } 280 281 public static class MediumPoolable 282 implements org.apache.avalon.excalibur.pool.Poolable 283 { 284 int[] a = new int[ 100 ]; 285 } 286 287 public static class LargePoolable 288 implements org.apache.avalon.excalibur.pool.Poolable 289 { 290 int[][] a = new int[ 10 ][ 100 ]; 291 } 292 293 296 public static class NoPoolingPool 297 implements Pool, LogEnabled 298 { 299 private ObjectFactory m_factory; 300 private Logger m_logger; 301 302 public NoPoolingPool( ObjectFactory factory ) 303 { 304 m_factory = factory; 305 } 306 307 public void enableLogging( Logger logger ) 308 { 309 m_logger = logger; 310 } 311 312 public Object acquire() throws Exception 313 { 314 return newInstance(); 315 } 316 317 public void release( Object poolable ) 318 { 319 try 320 { 321 m_factory.dispose( poolable ); 322 } 323 catch( Exception e ) 324 { 325 m_logger.debug( "Error decommissioning object", e ); 326 } 327 } 328 329 public Object newInstance() throws Exception 330 { 331 return m_factory.newInstance(); 332 } 333 } 334 335 338 345 public void testCompare_VariableSizePool_And_ResourceLimitingPool_Max100_Gets100_SmallPoolables() 346 throws Exception 347 { 348 String name = "VariableSizePool_And_ResourceLimitingPool_Max100_Gets100_SmallPoolables"; 349 350 Class poolableClass = SmallPoolable.class; 351 ClassInstanceObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger ); 352 int max = 100; 353 boolean maxStrict = false; 354 boolean blocking = false; 355 long blockTimeout = 0; 356 long trimInterval = 0; 357 358 VariableSizePool poolA = new VariableSizePool( factory, max ); 359 360 ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max, maxStrict, blocking, blockTimeout, trimInterval ); 361 poolB.enableLogging( m_poolLogger ); 362 363 generalTest( name, poolA, poolB, 100, factory ); 364 } 365 366 373 public void testCompare_VariableSizePool_And_ResourceLimitingPool_Max100_Gets200_SmallPoolables() 374 throws Exception 375 { 376 String name = "VariableSizePool_And_ResourceLimitingPool_Max100_Gets200_SmallPoolables"; 377 378 Class poolableClass = SmallPoolable.class; 379 ClassInstanceObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger ); 380 int max = 100; 381 boolean maxStrict = false; 382 boolean blocking = false; 383 long blockTimeout = 0; 384 long trimInterval = 0; 385 386 VariableSizePool poolA = new VariableSizePool( factory, max ); 387 388 ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max, maxStrict, blocking, blockTimeout, trimInterval ); 389 poolB.enableLogging( m_poolLogger ); 390 391 generalTest( name, poolA, poolB, 200, factory ); 392 } 393 394 401 public void testCompare_VariableSizePool_And_ResourceLimitingPool_Max100_Gets100_MediumPoolables() 402 throws Exception 403 { 404 String name = "VariableSizePool_And_ResourceLimitingPool_Max10_Gets100_MediumPoolables"; 405 406 Class poolableClass = MediumPoolable.class; 407 ClassInstanceObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger ); 408 int max = 100; 409 boolean maxStrict = false; 410 boolean blocking = false; 411 long blockTimeout = 0; 412 long trimInterval = 0; 413 414 VariableSizePool poolA = new VariableSizePool( factory, max ); 415 416 ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max, maxStrict, blocking, blockTimeout, trimInterval ); 417 poolB.enableLogging( m_poolLogger ); 418 419 generalTest( name, poolA, poolB, 100, factory ); 420 } 421 422 429 public void testCompare_VariableSizePool_And_ResourceLimitingPool_Max100_Gets200_MediumPoolables() 430 throws Exception 431 { 432 String name = "VariableSizePool_And_ResourceLimitingPool_Max100_Gets200_MediumPoolables"; 433 434 Class poolableClass = MediumPoolable.class; 435 ClassInstanceObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger ); 436 int max = 100; 437 boolean maxStrict = false; 438 boolean blocking = false; 439 long blockTimeout = 0; 440 long trimInterval = 0; 441 442 VariableSizePool poolA = new VariableSizePool( factory, max ); 443 444 ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max, maxStrict, blocking, blockTimeout, trimInterval ); 445 poolB.enableLogging( m_poolLogger ); 446 447 generalTest( name, poolA, poolB, 200, factory ); 448 } 449 450 457 public void testCompare_VariableSizePool_And_ResourceLimitingPool_Max100_Gets100_LargePoolables() 458 throws Exception 459 { 460 String name = "VariableSizePool_And_ResourceLimitingPool_Max100_Gets100_LargePoolables"; 461 462 Class poolableClass = LargePoolable.class; 463 ClassInstanceObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger ); 464 int max = 100; 465 boolean maxStrict = false; 466 boolean blocking = false; 467 long blockTimeout = 0; 468 long trimInterval = 0; 469 470 VariableSizePool poolA = new VariableSizePool( factory, max ); 471 472 ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max, maxStrict, blocking, blockTimeout, trimInterval ); 473 poolB.enableLogging( m_poolLogger ); 474 475 generalTest( name, poolA, poolB, 100, factory ); 476 } 477 478 485 public void testCompare_VariableSizePool_And_ResourceLimitingPool_Max100_Gets200_LargePoolables() 486 throws Exception 487 { 488 String name = "VariableSizePool_And_ResourceLimitingPool_Max100_Gets200_LargePoolables"; 489 490 Class poolableClass = LargePoolable.class; 491 ClassInstanceObjectFactory factory = new ClassInstanceObjectFactory( poolableClass, m_poolLogger ); 492 int max = 100; 493 boolean maxStrict = false; 494 boolean blocking = false; 495 long blockTimeout = 0; 496 long trimInterval = 0; 497 498 VariableSizePool poolA = new VariableSizePool( factory, max ); 499 500 ResourceLimitingPool poolB = new ResourceLimitingPool( factory, max, maxStrict, blocking, blockTimeout, trimInterval ); 501 poolB.enableLogging( m_poolLogger ); 502 503 generalTest( name, poolA, poolB, 200, factory ); 504 } 505 506 509 protected void resetMemory() 510 { 511 System.gc(); 512 System.gc(); 513 514 try 516 { 517 Thread.sleep( 50 ); 518 } 519 catch( InterruptedException e ) 520 { 521 } 522 Runtime runtime = Runtime.getRuntime(); 523 m_logger.debug( "Memory: " + ( runtime.totalMemory() - runtime.freeMemory() ) ); 524 } 525 526 protected String getShortClassName( Object o ) 527 { 528 String name = o.getClass().getName(); 529 int pos = name.lastIndexOf( '.' ); 530 if( pos > 0 ) 531 { 532 name = name.substring( pos + 1 ); 533 } 534 return name; 535 } 536 537 protected abstract long getPoolRunTime( Pool pool, int gets ) 538 throws Exception ; 539 540 protected abstract long getPoolRunTime( org.apache.avalon.excalibur.pool.Pool pool, int gets ) 541 throws Exception ; 542 543 546 protected void generalTest( String name, Pool poolA, Pool poolB, int gets, ClassInstanceObjectFactory factory ) 547 throws Exception 548 { 549 m_logger.info( "Test Case: " + name ); 550 551 final String poolAName = getShortClassName( poolA ); 553 final String poolBName = getShortClassName( poolB ); 554 555 resetMemory(); 557 558 NoPoolingPool poolBase = new NoPoolingPool( factory ); 560 poolBase.enableLogging( m_poolLogger ); 561 final long noPoolDuration = getPoolRunTime( poolBase, gets ); 562 m_logger.info( " Unpooled time = " + noPoolDuration + "ms. to use " + TEST_SIZE + " objects." ); 563 resetMemory(); 564 565 566 final long poolADuration = getPoolRunTime( poolA, gets ); 568 m_logger.info( " " + poolAName + " time = " + poolADuration + "ms. to use " + TEST_SIZE + " objects, " + gets + " at a time." ); 569 resetMemory(); 570 571 572 final long poolBDuration = getPoolRunTime( poolB, gets ); 574 m_logger.info( " " + poolBName + " time = " + poolBDuration + "ms. to use " + TEST_SIZE + " objects, " + gets + " at a time." ); 575 resetMemory(); 576 577 if( m_logger.isInfoEnabled() ) 579 { 580 double mult; 581 mult = ( poolADuration > 0 ? ( noPoolDuration * 100 / poolADuration ) / 100.0 : Float.POSITIVE_INFINITY ); 582 m_logger.info( " => " + poolAName + " is " + mult + " X as fast as not pooling." ); 583 584 mult = ( poolBDuration > 0 ? ( noPoolDuration * 100 / poolBDuration ) / 100.0 : Float.POSITIVE_INFINITY ); 585 m_logger.info( " => " + poolBName + " is " + mult + " X as fast as not pooling." ); 586 587 mult = ( poolBDuration > 0 ? ( poolADuration * 100 / poolBDuration ) / 100.0 : Float.POSITIVE_INFINITY ); 588 m_logger.info( " => " + poolBName + " is " + mult + " X as fast as " + poolAName + "." ); 589 590 mult = ( poolADuration > 0 ? ( poolBDuration * 100 / poolADuration ) / 100.0 : Float.POSITIVE_INFINITY ); 591 m_logger.info( " => " + poolAName + " is " + mult + " X as fast as " + poolBName + "." ); 592 } 593 } 594 595 598 protected void generalTest( String name, Pool poolA, org.apache.avalon.excalibur.pool.Pool poolB, int gets, ClassInstanceObjectFactory factory ) 599 throws Exception 600 { 601 m_logger.info( "Test Case: " + name ); 602 603 final String poolAName = getShortClassName( poolA ); 605 final String poolBName = getShortClassName( poolB ); 606 607 resetMemory(); 609 610 NoPoolingPool poolBase = new NoPoolingPool( factory ); 612 poolBase.enableLogging( m_poolLogger ); 613 final long noPoolDuration = getPoolRunTime( poolBase, gets ); 614 m_logger.info( " Unpooled time = " + noPoolDuration + "ms. to use " + TEST_SIZE + " objects." ); 615 resetMemory(); 616 617 618 final long poolADuration = getPoolRunTime( poolA, gets ); 620 m_logger.info( " " + poolAName + " time = " + poolADuration + "ms. to use " + TEST_SIZE + " objects, " + gets + " at a time." ); 621 resetMemory(); 622 623 624 final long poolBDuration = getPoolRunTime( poolB, gets ); 626 m_logger.info( " " + poolBName + " time = " + poolBDuration + "ms. to use " + TEST_SIZE + " objects, " + gets + " at a time." ); 627 resetMemory(); 628 629 if( m_logger.isInfoEnabled() ) 631 { 632 double mult; 633 mult = ( poolADuration > 0 ? ( noPoolDuration * 100 / poolADuration ) / 100.0 : Float.POSITIVE_INFINITY ); 634 m_logger.info( " => " + poolAName + " is " + mult + " X as fast as not pooling." ); 635 636 mult = ( poolBDuration > 0 ? ( noPoolDuration * 100 / poolBDuration ) / 100.0 : Float.POSITIVE_INFINITY ); 637 m_logger.info( " => " + poolBName + " is " + mult + " X as fast as not pooling." ); 638 639 mult = ( poolBDuration > 0 ? ( poolADuration * 100 / poolBDuration ) / 100.0 : Float.POSITIVE_INFINITY ); 640 m_logger.info( " => " + poolBName + " is " + mult + " X as fast as " + poolAName + "." ); 641 642 mult = ( poolADuration > 0 ? ( poolBDuration * 100 / poolADuration ) / 100.0 : Float.POSITIVE_INFINITY ); 643 m_logger.info( " => " + poolAName + " is " + mult + " X as fast as " + poolBName + "." ); 644 } 645 } 646 } 647 | Popular Tags |