You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by bl...@apache.org on 2002/06/26 20:33:25 UTC

cvs commit: jakarta-avalon-excalibur/collections/src/test/org/apache/avalon/excalibur/collections/test ThreadedMapTest.java

bloritsch    2002/06/26 11:33:25

  Added:       collections/src/test/org/apache/avalon/excalibur/collections/test
                        ThreadedMapTest.java
  Log:
  Added performance testcase
  
  Revision  Changes    Path
  1.1                  jakarta-avalon-excalibur/collections/src/test/org/apache/avalon/excalibur/collections/test/ThreadedMapTest.java
  
  Index: ThreadedMapTest.java
  ===================================================================
  /**
   * Created on Jun 20, 2002
   *
   * To change this generated comment edit the template variable "filecomment":
   * Window>Preferences>Java>Templates.
   */
  package org.apache.avalon.excalibur.collections.test;
  
  import java.util.Collections;
  import java.util.HashMap;
  import java.util.Map;
  import java.util.Random;
  
  import org.apache.avalon.excalibur.collections.BucketMap;
  import org.apache.avalon.excalibur.collections.Buffer;
  import org.apache.avalon.excalibur.collections.FixedSizeBuffer;
  
  import junit.framework.TestCase;
  
  /**
   * @author bloritsch
   *
   * To change this generated comment edit the template variable "typecomment":
   * Window>Preferences>Java>Templates.
   */
  public class ThreadedMapTest extends TestCase
  {
      private static final int ITERATIONS = 10000;
      private static final int THREADS = 100;
  
      private MapStart start;
  
      private Map queue;
  
      private MapStart[] stages;
  
      public ThreadedMapTest( String name )
      {
          super( name );
      }
  
      public void testThreaded() throws Exception
      {
          initialize( ITERATIONS, THREADS, new BucketMap( ( ITERATIONS / 2 ) + 1) );
          long start = System.currentTimeMillis();
          start();
          long end = System.currentTimeMillis();
  
          System.out.println("BucketMap (" + ((ITERATIONS / 2) + 1) + " buckets) took " + (end - start) + "ms");
          System.out.println("Thats " + ( (float)(end - start) / (float)(ITERATIONS * THREADS) ) + "ms per operation");
  
          initialize( ITERATIONS, THREADS, new BucketMap() );
          start = System.currentTimeMillis();
          start();
          end = System.currentTimeMillis();
  
          System.out.println("Unsized BucketMap (255 buckets) took " + (end - start) + "ms");
          System.out.println("Thats " + ( (float)(end - start) / (float)(ITERATIONS * THREADS) ) + "ms per operation");
  
          initialize( ITERATIONS, THREADS, Collections.synchronizedMap(new HashMap()) );
          start = System.currentTimeMillis();
          start();
          end = System.currentTimeMillis();
  
          System.out.println("Synchronized HashMap took " + (end - start) + "ms");
          System.out.println("Thats " + ( (float)(end - start) / (float)(ITERATIONS * THREADS) ) + "ms per operation");
      }
  
      public void initialize( int count, int threads, Map map ) throws Exception
      {
          this.stages = new MapStart[ threads ];
  
          for (int i = 0; i < threads; i++)
          {
              this.stages[ i ] = new MapStart( count, map );
          }
      }
  
      public void start() throws Exception
      {
          /*
           * Commented out. Tests should be silent(?). /LS
           *
           */
           System.out.println("Starting test");
  
          for( int i = 0; i < this.stages.length; i++ )
          {
              this.stages[ i ].start();
          }
  
          stop();
      }
  
      public void stop() throws Exception
      {
          for( int i = 0; i < this.stages.length; i++ )
          {
              try
              {
                  this.stages[ i ].join();
                  assertEquals(ITERATIONS, this.stages[i].getCount());
              }
              catch( InterruptedException e )
              {
                  throw new RuntimeException( "Stage unexpectedly interrupted: " + e );
              }
          }
  
          /*
           *
           * Commented out. Tests should be silent(?). /LS
           */
           System.out.println("Test complete");
      }
  
      private class MapStart extends Thread
      {
          private final Map map;
          private final int mapCount;
          private int count;
  
          public MapStart( int mapCount, Map newmap )
          {
              this.mapCount = mapCount;
              this.map = newmap;
          }
  
          public int getCount()
          {
              return count;
          }
  
          public void run()
          {
              long seed = 0;
              Random rnd = new Random(seed);
              
              for( int i = 0; i < this.mapCount / 2; i++ )
              {
                  this.map.put(new Integer( rnd.nextInt() ), new Integer( rnd.nextInt() ) );
                  this.count++;
              }
              
              rnd.setSeed( seed ); // reset pseudo random
              
              for( int i = 0; i < this.mapCount / 2; i++ )
              {
                  Integer value = (Integer)this.map.get( new Integer( rnd.nextInt() ));
                  assertEquals(value.intValue(), rnd.nextInt());
                  this.count++;
              }
          }
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>