You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jcs-dev@jakarta.apache.org by as...@apache.org on 2006/02/07 00:56:11 UTC

svn commit: r375424 - in /jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct: LRUMapConcurrentUnitTest.java LRUMapPerformanceTest.java LRUMapUnitTest.java

Author: asmuts
Date: Mon Feb  6 15:56:09 2006
New Revision: 375424

URL: http://svn.apache.org/viewcvs?rev=375424&view=rev
Log:
adding and cleeaning up tests

Added:
    jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/LRUMapConcurrentUnitTest.java
    jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/LRUMapPerformanceTest.java
    jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/LRUMapUnitTest.java

Added: jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/LRUMapConcurrentUnitTest.java
URL: http://svn.apache.org/viewcvs/jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/LRUMapConcurrentUnitTest.java?rev=375424&view=auto
==============================================================================
--- jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/LRUMapConcurrentUnitTest.java (added)
+++ jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/LRUMapConcurrentUnitTest.java Mon Feb  6 15:56:09 2006
@@ -0,0 +1,277 @@
+package org.apache.jcs.utils.struct;
+
+import java.util.Iterator;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.jcs.utils.struct.LRUMap;
+
+/**
+ * Tests the LRUMap
+ * 
+ * @author aaronsm
+ *  
+ */
+public class LRUMapConcurrentUnitTest
+    extends TestCase
+{
+
+    private static int items = 20000;
+
+    /**
+     * Constructor for the TestSimpleLoad object
+     * 
+     * @param testName
+     *            Description of the Parameter
+     */
+    public LRUMapConcurrentUnitTest( String testName )
+    {
+        super( testName );
+    }
+
+    /**
+     * Description of the Method
+     * 
+     * @param args
+     *            Description of the Parameter
+     */
+    public static void main( String args[] )
+    {
+        String[] testCaseName = { LRUMapConcurrentUnitTest.class.getName() };
+        junit.textui.TestRunner.main( testCaseName );
+    }
+
+    /**
+     * A unit test suite for JUnit
+     * 
+     * @return The test suite
+     */
+    public static Test suite()
+    {
+        // run the basic tests
+        TestSuite suite = new TestSuite( LRUMapConcurrentUnitTest.class );
+
+        // run concurrent tests
+        final LRUMap map = new LRUMap( 2000 );
+        suite.addTest( new LRUMapConcurrentUnitTest( "conc1" )
+        {
+            public void runTest()
+                throws Exception
+            {
+                this.runConcurrentPutGetTests( map, 2000 );
+            }
+        } );
+        suite.addTest( new LRUMapConcurrentUnitTest( "conc2" )
+        {
+            public void runTest()
+                throws Exception
+            {
+                this.runConcurrentPutGetTests( map, 2000 );
+            }
+        } );
+        suite.addTest( new LRUMapConcurrentUnitTest( "conc3" )
+        {
+            public void runTest()
+                throws Exception
+            {
+                this.runConcurrentPutGetTests( map, 2000 );
+            }
+        } );
+
+        // run more concurrent tests
+        final int max2 = 20000;
+        final LRUMap map2 = new LRUMap( max2 );
+        suite.addTest( new LRUMapConcurrentUnitTest( "concB1" )
+        {
+            public void runTest()
+                throws Exception
+            {
+                this.runConcurrentRangeTests( map2, 10000, max2 );
+            }
+        } );
+        suite.addTest( new LRUMapConcurrentUnitTest( "concB1" )
+        {
+            public void runTest()
+                throws Exception
+            {
+                this.runConcurrentRangeTests( map2, 0, 9999 );
+            }
+        } );
+
+        return suite;
+    }
+
+    /**
+     * Just test that we can put, get and remove as expected.
+     * 
+     * @exception Exception
+     *                Description of the Exception
+     */
+    public void testSimpleLoad()
+        throws Exception
+    {
+        LRUMap map = new LRUMap( items );
+
+        for ( int i = 0; i < items; i++ )
+        {
+            map.put( i + ":key", "data" + i );
+        }
+
+        for ( int i = items - 1; i >= 0; i-- )
+        {
+            String res = (String) map.get( i + ":key" );
+            if ( res == null )
+            {
+                assertNotNull( "[" + i + ":key] should not be null", res );
+            }
+        }
+
+        // test removal
+        map.remove( "300:key" );
+        assertNull( map.get( "300:key" ) );
+
+    }
+
+    /**
+     * Just make sure that the LRU functions int he most simple case.
+     * 
+     * @exception Exception
+     *                Description of the Exception
+     */
+    public void testLRURemoval()
+        throws Exception
+    {
+        int total = 10;
+        LRUMap map = new LRUMap( total );
+        map.setChunkSize( 1 );
+
+        // put the max in
+        for ( int i = 0; i < total; i++ )
+        {
+            map.put( i + ":key", "data" + i );
+        }
+
+        Iterator it = map.entrySet().iterator();
+        while ( it.hasNext() )
+        {
+            System.out.println( it.next() );
+        }
+        System.out.println( map.getStatistics() );
+
+        // get the max out backwards
+        for ( int i = total - 1; i >= 0; i-- )
+        {
+            String res = (String) map.get( i + ":key" );
+            if ( res == null )
+            {
+                assertNotNull( "[" + i + ":key] should not be null", res );
+            }
+        }
+
+        System.out.println( map.getStatistics() );
+
+        //since we got them backwards the total should be at the end.
+        // add one confirm that total is gone.
+        map.put( ( total ) + ":key", "data" + ( total ) );
+        assertNull( map.get( ( total - 1 ) + ":key" ) );
+
+    }
+
+    /**
+     * @throws Exception
+     */
+    public void testLRURemovalAgain()
+        throws Exception
+    {
+        int total = 10000;
+        LRUMap map = new LRUMap( total );
+        map.setChunkSize( 1 );
+
+        // put the max in
+        for ( int i = 0; i < total * 2; i++ )
+        {
+            map.put( i + ":key", "data" + i );
+        }
+
+        // get the total number, these shoukld be null
+        for ( int i = total - 1; i >= 0; i-- )
+        {
+            assertNull( map.get( i + ":key" ) );
+
+        }
+
+        // get the total to total *2 items out, these should be foufn.
+        for ( int i = ( total * 2 ) - 1; i >= total; i-- )
+        {
+            String res = (String) map.get( i + ":key" );
+            if ( res == null )
+            {
+                assertNotNull( "[" + i + ":key] should not be null", res );
+            }
+        }
+
+        System.out.println( map.getStatistics() );
+
+    }
+
+    /**
+     * Just make sure that we can put and get concurrently
+     * 
+     * @param map
+     * @param items
+     * @throws Exception
+     */
+    public void runConcurrentPutGetTests( LRUMap map, int items )
+        throws Exception
+    {
+        for ( int i = 0; i < items; i++ )
+        {
+            map.put( i + ":key", "data" + i );
+        }
+
+        for ( int i = items - 1; i >= 0; i-- )
+        {
+            String res = (String) map.get( i + ":key" );
+            if ( res == null )
+            {
+                assertNotNull( "[" + i + ":key] should not be null", res );
+            }
+        }
+
+    }
+
+    /**
+     * Put, get, and remove from a range. This should occur at a range that is
+     * not touched by other tests.
+     * 
+     * @param map
+     * @param start
+     * @param end
+     * @throws Exception
+     */
+    public void runConcurrentRangeTests( LRUMap map, int start, int end )
+        throws Exception
+    {
+        for ( int i = start; i < end; i++ )
+        {
+            map.put( i + ":key", "data" + i );
+        }
+
+        for ( int i = end - 1; i >= start; i-- )
+        {
+            String res = (String) map.get( i + ":key" );
+            if ( res == null )
+            {
+                assertNotNull( "[" + i + ":key] should not be null", res );
+            }
+        }
+
+        // test removal
+        map.remove( start + ":key" );
+        assertNull( map.get( start + ":key" ) );
+
+    }
+
+}
\ No newline at end of file

Added: jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/LRUMapPerformanceTest.java
URL: http://svn.apache.org/viewcvs/jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/LRUMapPerformanceTest.java?rev=375424&view=auto
==============================================================================
--- jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/LRUMapPerformanceTest.java (added)
+++ jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/LRUMapPerformanceTest.java Mon Feb  6 15:56:09 2006
@@ -0,0 +1,185 @@
+package org.apache.jcs.utils.struct;
+
+import java.util.Map;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.jcs.TestJCSvHashtablePerf;
+import org.apache.jcs.utils.struct.LRUMap;
+
+/**
+ * This ensures that the jcs version of the LRU map is as fast as the commons
+ * version. It has been testing at .6 to .7 times the commons LRU.
+ * 
+ * @author aaronsm
+ *  
+ */
+public class LRUMapPerformanceTest
+    extends TestCase
+{
+
+    float ratioPut = 0;
+
+    float ratioGet = 0;
+
+    float target = 1.0f;
+
+    int loops = 20;
+
+    int tries = 50000;
+
+    /**
+     * @param testName
+     */
+    public LRUMapPerformanceTest( String testName )
+    {
+        super( testName );
+    }
+
+    /**
+     * A unit test suite for JUnit
+     * 
+     * @return The test suite
+     */
+    public static Test suite()
+    {
+        return new TestSuite( LRUMapPerformanceTest.class );
+    }
+
+    /**
+     * A unit test for JUnit
+     * 
+     * @exception Exception
+     *                Description of the Exception
+     */
+    public void testSimpleLoad()
+        throws Exception
+    {
+        doWork();
+        assertTrue( this.ratioPut < target );
+        assertTrue( this.ratioGet < target );
+    }
+
+    /**
+     *  
+     */
+    public void doWork()
+    {
+
+        long start = 0;
+        long end = 0;
+        long time = 0;
+        float tPer = 0;
+
+        long putTotalJCS = 0;
+        long getTotalJCS = 0;
+        long putTotalHashtable = 0;
+        long getTotalHashtable = 0;
+
+        String name = "LRUMap";
+        String cache2Name = "";
+
+        try
+        {
+
+            Map cache = new LRUMap( tries );
+
+            for ( int j = 0; j < loops; j++ )
+            {
+
+                name = "JCS      ";
+                start = System.currentTimeMillis();
+                for ( int i = 0; i < tries; i++ )
+                {
+                    cache.put( "key:" + i, "data" + i );
+                }
+                end = System.currentTimeMillis();
+                time = end - start;
+                putTotalJCS += time;
+                tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
+                System.out.println( name + " put time for " + tries + " = " + time + "; millis per = " + tPer );
+
+                start = System.currentTimeMillis();
+                for ( int i = 0; i < tries; i++ )
+                {
+                    cache.get( "key:" + i );
+                }
+                end = System.currentTimeMillis();
+                time = end - start;
+                getTotalJCS += time;
+                tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
+                System.out.println( name + " get time for " + tries + " = " + time + "; millis per = " + tPer );
+
+                ///////////////////////////////////////////////////////////////
+                cache2Name = "LRUMapJCS (commons)";
+                //or LRUMapJCS
+                Map cache2 = new org.apache.commons.collections.map.LRUMap( tries );
+                //cache2Name = "Hashtable";
+                //Hashtable cache2 = new Hashtable();
+                start = System.currentTimeMillis();
+                for ( int i = 0; i < tries; i++ )
+                {
+                    cache2.put( "key:" + i, "data" + i );
+                }
+                end = System.currentTimeMillis();
+                time = end - start;
+                putTotalHashtable += time;
+                tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
+                System.out.println( cache2Name + " put time for " + tries + " = " + time + "; millis per = " + tPer );
+
+                start = System.currentTimeMillis();
+                for ( int i = 0; i < tries; i++ )
+                {
+                    cache2.get( "key:" + i );
+                }
+                end = System.currentTimeMillis();
+                time = end - start;
+                getTotalHashtable += time;
+                tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
+                System.out.println( cache2Name + " get time for " + tries + " = " + time + "; millis per = " + tPer );
+
+                System.out.println( "\n" );
+            }
+
+        }
+        catch ( Exception e )
+        {
+            e.printStackTrace( System.out );
+            System.out.println( e );
+        }
+
+        long putAvJCS = putTotalJCS / loops;
+        long getAvJCS = getTotalJCS / loops;
+        long putAvHashtable = putTotalHashtable / loops;
+        long getAvHashtable = getTotalHashtable / loops;
+
+        System.out.println( "Finished " + loops + " loops of " + tries + " gets and puts" );
+
+        System.out.println( "\n" );
+        System.out.println( "Put average for LRUMap       = " + putAvJCS );
+        System.out.println( "Put average for " + cache2Name + " = " + putAvHashtable );
+        ratioPut = Float.intBitsToFloat( (int) putAvJCS ) / Float.intBitsToFloat( (int) putAvHashtable );
+        System.out.println( name + " puts took " + ratioPut + " times the " + cache2Name + ", the goal is <" + target
+            + "x" );
+
+        System.out.println( "\n" );
+        System.out.println( "Get average for LRUMap       = " + getAvJCS );
+        System.out.println( "Get average for " + cache2Name + " = " + getAvHashtable );
+        ratioGet = Float.intBitsToFloat( (int) getAvJCS ) / Float.intBitsToFloat( (int) getAvHashtable );
+        System.out.println( name + " gets took " + ratioGet + " times the " + cache2Name + ", the goal is <" + target
+            + "x" );
+
+    }
+
+    /**
+     * @param args
+     */
+    public static void main( String args[] )
+    {
+        TestJCSvHashtablePerf test = new TestJCSvHashtablePerf( "command" );
+        test.doWork();
+    }
+
+}

Added: jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/LRUMapUnitTest.java
URL: http://svn.apache.org/viewcvs/jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/LRUMapUnitTest.java?rev=375424&view=auto
==============================================================================
--- jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/LRUMapUnitTest.java (added)
+++ jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/LRUMapUnitTest.java Mon Feb  6 15:56:09 2006
@@ -0,0 +1,83 @@
+package org.apache.jcs.utils.struct;
+
+import junit.framework.TestCase;
+
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+
+/**
+ * Test basic functionality.
+ * 
+ * @author Aaron Smuts
+ * 
+ */
+public class LRUMapUnitTest
+    extends TestCase
+{
+    private Level origLevel = Level.INFO;
+
+    public void setUp()
+    {
+        Logger logger = Logger.getLogger( LRUMap.class );
+        origLevel = logger.getLevel();
+        logger.setLevel( Level.DEBUG );
+    }
+
+    public void tearDown()
+    {
+        Logger logger = Logger.getLogger( LRUMap.class );
+        logger.setLevel( origLevel );
+    }
+
+    /**
+     * Verify that we can put, get, and remove and item.
+     * 
+     */
+    public void testPutGetRemove()
+    {
+        int max = 100;
+        LRUMap map = new LRUMap( max );
+
+        String key = "MyKey";
+        String data = "testdata";
+
+        map.put( key, data );
+        assertEquals( "Data is wrong.", data, map.get( key ) );
+
+        map.verifyCache( key );
+
+        map.remove( key );
+        assertNull( "Data should have been removed.", map.get( key ) );
+    }
+
+    /**
+     * Just test that we can put, get and remove as expected.
+     * 
+     * @exception Exception
+     *                Description of the Exception
+     */
+    public void testSimpleLoad()
+        throws Exception
+    {
+        int items = 2000;
+        LRUMap map = new LRUMap( items );
+
+        for ( int i = 0; i < items; i++ )
+        {
+            map.put( i + ":key", "data" + i );
+        }
+
+        for ( int i = items - 1; i >= 0; i-- )
+        {
+            String res = (String) map.get( i + ":key" );
+            if ( res == null )
+            {
+                assertNotNull( "[" + i + ":key] should not be null", res );
+            }
+        }
+
+        // verify that this passes.
+        map.verifyCache();
+
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: jcs-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jcs-dev-help@jakarta.apache.org