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/17 23:54:47 UTC

svn commit: r378647 - in /jakarta/jcs/trunk/src: test-conf/TestMRUCache.ccf test/org/apache/jcs/engine/memory/mru/LRUvsMRUPerformanceTest.java test/org/apache/jcs/engine/memory/mru/MRUMemoryCacheUnitTest.java

Author: asmuts
Date: Fri Feb 17 14:54:46 2006
New Revision: 378647

URL: http://svn.apache.org/viewcvs?rev=378647&view=rev
Log:
new test config

Added:
    jakarta/jcs/trunk/src/test-conf/TestMRUCache.ccf
    jakarta/jcs/trunk/src/test/org/apache/jcs/engine/memory/mru/LRUvsMRUPerformanceTest.java
    jakarta/jcs/trunk/src/test/org/apache/jcs/engine/memory/mru/MRUMemoryCacheUnitTest.java

Added: jakarta/jcs/trunk/src/test-conf/TestMRUCache.ccf
URL: http://svn.apache.org/viewcvs/jakarta/jcs/trunk/src/test-conf/TestMRUCache.ccf?rev=378647&view=auto
==============================================================================
--- jakarta/jcs/trunk/src/test-conf/TestMRUCache.ccf (added)
+++ jakarta/jcs/trunk/src/test-conf/TestMRUCache.ccf Fri Feb 17 14:54:46 2006
@@ -0,0 +1,29 @@
+# JCS Config for unit testing, just a simple memory only cache, with 0 max size
+# with the memory shrinker on.
+
+jcs.default=
+jcs.default.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes
+jcs.default.cacheattributes.MaxObjects=1000
+jcs.default.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.mru.MRUMemoryCache
+jcs.default.cacheattributes.UseMemoryShrinker=true
+jcs.default.cacheattributes.MaxMemoryIdleTimeSeconds=3600
+jcs.default.cacheattributes.ShrinkerIntervalSeconds=1
+jcs.default.elementattributes=org.apache.jcs.engine.ElementAttributes
+jcs.default.elementattributes.IsEternal=false
+jcs.default.elementattributes.MaxLifeSeconds=600
+jcs.default.elementattributes.IdleTime=1800
+jcs.default.elementattributes.IsSpool=true
+jcs.default.elementattributes.IsRemote=true
+jcs.default.elementattributes.IsLateral=true
+
+# Region defined that uses the MRU
+jcs.region.mruDefined=
+jcs.region.mruDefined.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes
+jcs.region.mruDefined.cacheattributes.MaxObjects=100000
+jcs.region.mruDefined.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.mru.MRUMemoryCache
+
+# Region defined that uses the LRU
+jcs.region.lruDefined=
+jcs.region.lruDefined.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes
+jcs.region.lruDefined.cacheattributes.MaxObjects=100000
+jcs.region.lruDefined.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache

Added: jakarta/jcs/trunk/src/test/org/apache/jcs/engine/memory/mru/LRUvsMRUPerformanceTest.java
URL: http://svn.apache.org/viewcvs/jakarta/jcs/trunk/src/test/org/apache/jcs/engine/memory/mru/LRUvsMRUPerformanceTest.java?rev=378647&view=auto
==============================================================================
--- jakarta/jcs/trunk/src/test/org/apache/jcs/engine/memory/mru/LRUvsMRUPerformanceTest.java (added)
+++ jakarta/jcs/trunk/src/test/org/apache/jcs/engine/memory/mru/LRUvsMRUPerformanceTest.java Fri Feb 17 14:54:46 2006
@@ -0,0 +1,163 @@
+package org.apache.jcs.engine.memory.mru;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.jcs.JCS;
+import org.apache.jcs.engine.memory.lru.LRUMemoryCache;
+
+/**
+ * Tests the performance difference between the LRU and the MRU
+ * 
+ */
+public class LRUvsMRUPerformanceTest
+    extends TestCase
+{
+
+    float ratioPut = 0;
+
+    float ratioGet = 0;
+
+    float target = 1.20f;
+
+    int loops = 20;
+
+    int tries = 10000;
+
+    /**
+     * A unit test for JUnit
+     * 
+     * @exception Exception
+     *                Description of the Exception
+     */
+    public void testSimpleLoad()
+        throws Exception
+    {
+        Log log1 = LogFactory.getLog( LRUMemoryCache.class );
+        if ( log1.isDebugEnabled() )
+        {
+            System.out.println( "The log level must be at info or above for the a performance test." );
+            return;
+        }
+        Log log2 = LogFactory.getLog( MRUMemoryCache.class );
+        if ( log2.isDebugEnabled() )
+        {
+            System.out.println( "The log level must be at info or above for the a performance test." );
+            return;
+        }
+        doWork();
+
+        assertTrue( "Ratio is unacceptible.", this.ratioPut < target );
+        assertTrue( "Ratio is unacceptible.", this.ratioGet < target );
+    }
+
+    /**
+     * Runs the test
+     */
+    public void doWork()
+    {
+
+        long start = 0;
+        long end = 0;
+        long time = 0;
+        float tPer = 0;
+
+        long putTotalLRU = 0;
+        long getTotalLRU = 0;
+        long putTotalMRU = 0;
+        long getTotalMRU = 0;
+
+        try
+        {
+
+            JCS.setConfigFilename( "/TestMRUCache.ccf" );
+            JCS cache = JCS.getInstance( "lruDefined" );
+            JCS mru = JCS.getInstance( "mruDefined" );
+
+            System.out.println( "LRU = " + cache );
+
+            for ( int j = 0; j < loops; j++ )
+            {
+
+                System.out.println( "Beginning loop " + j );
+
+                String name = "LRU      ";
+                start = System.currentTimeMillis();
+                for ( int i = 0; i < tries; i++ )
+                {
+                    cache.put( "key:" + i, "data" + i );
+                }
+                end = System.currentTimeMillis();
+                time = end - start;
+                putTotalLRU += 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;
+                getTotalLRU += time;
+                tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
+                System.out.println( name + " get time for " + tries + " = " + time + "; millis per = " + tPer );
+
+                // /////////////////////////////////////////////////////////////
+                name = "MRU";
+                start = System.currentTimeMillis();
+                for ( int i = 0; i < tries; i++ )
+                {
+                    mru.put( "key:" + i, "data" + i );
+                }
+                end = System.currentTimeMillis();
+                time = end - start;
+                putTotalMRU += 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++ )
+                {
+                    mru.get( "key:" + i );
+                }
+                end = System.currentTimeMillis();
+                time = end - start;
+                getTotalMRU += time;
+                tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
+                System.out.println( name + " 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 = putTotalLRU / loops;
+        long getAvJCS = getTotalLRU / loops;
+        long putAvHashtable = putTotalMRU / loops;
+        long getAvHashtable = getTotalMRU / loops;
+
+        System.out.println( "Finished " + loops + " loops of " + tries + " gets and puts" );
+
+        System.out.println( "\n" );
+        System.out.println( "Put average for JCS       = " + putAvJCS );
+        System.out.println( "Put average for MRU = " + putAvHashtable );
+        ratioPut = Float.intBitsToFloat( (int) putAvJCS ) / Float.intBitsToFloat( (int) putAvHashtable );
+        System.out.println( "JCS puts took " + ratioPut + " times the Hashtable, the goal is <" + target + "x" );
+
+        System.out.println( "\n" );
+        System.out.println( "Get average for JCS       = " + getAvJCS );
+        System.out.println( "Get average for MRU = " + getAvHashtable );
+        ratioGet = Float.intBitsToFloat( (int) getAvJCS ) / Float.intBitsToFloat( (int) getAvHashtable );
+        System.out.println( "JCS gets took " + ratioGet + " times the Hashtable, the goal is <" + target + "x" );
+
+    }
+
+}

Added: jakarta/jcs/trunk/src/test/org/apache/jcs/engine/memory/mru/MRUMemoryCacheUnitTest.java
URL: http://svn.apache.org/viewcvs/jakarta/jcs/trunk/src/test/org/apache/jcs/engine/memory/mru/MRUMemoryCacheUnitTest.java?rev=378647&view=auto
==============================================================================
--- jakarta/jcs/trunk/src/test/org/apache/jcs/engine/memory/mru/MRUMemoryCacheUnitTest.java (added)
+++ jakarta/jcs/trunk/src/test/org/apache/jcs/engine/memory/mru/MRUMemoryCacheUnitTest.java Fri Feb 17 14:54:46 2006
@@ -0,0 +1,281 @@
+package org.apache.jcs.engine.memory.mru;
+
+import junit.framework.TestCase;
+
+import org.apache.jcs.JCS;
+import org.apache.jcs.access.exception.CacheException;
+import org.apache.jcs.engine.CacheElement;
+import org.apache.jcs.engine.behavior.ICacheElement;
+import org.apache.jcs.engine.control.CompositeCache;
+import org.apache.jcs.engine.control.CompositeCacheManager;
+
+/**
+ * Tests for the test MRU implementation that uses the java linked list class.
+ * This is more a set of tests for the hub than for the MRU, since we don't care
+ * about the MRU.
+ * 
+ * @author Aaron Smuts
+ * 
+ */
+public class MRUMemoryCacheUnitTest
+    extends TestCase
+{
+
+    /**
+     * Test setup
+     */
+    public void setUp()
+    {
+        JCS.setConfigFilename( "/TestMRUCache.ccf" );
+    }
+
+    /**
+     * Verify that the mru gets used by a non-defined region when it is set as
+     * the defualt in the default region.
+     * 
+     * @throws CacheException
+     */
+    public void testLoadFromCCF()
+        throws CacheException
+    {
+        JCS cache = JCS.getInstance( "testPutGet" );
+        String memoryCacheName = cache.getCacheAttributes().getMemoryCacheName();
+        assertTrue( "Cache name should have MRU in it.", memoryCacheName.indexOf( "MRUMemoryCache" ) != -1 );
+    }
+
+    /**
+     * put twice as many as the max. verify that the second half is in the
+     * cache.
+     * 
+     * @throws CacheException
+     */
+    public void testPutGetThroughHub()
+        throws CacheException
+    {
+        JCS cache = JCS.getInstance( "testPutGetThroughHub" );
+
+        int max = cache.getCacheAttributes().getMaxObjects();
+        int items = max * 2;
+
+        for ( int i = 0; i < items; i++ )
+        {
+            cache.put( i + ":key", "myregion" + " data " + i );
+        }
+
+        // Test that first items are not in the cache
+        for ( int i = max; i >= 0; i-- )
+        {
+            String value = (String) cache.get( i + ":key" );
+            assertNull( "Should not have value for key [" + i + ":key" + "] in the cache.", value );
+        }
+
+        // Test that last items are in cache
+        // skip 2 for the buffer.
+        for ( int i = max + 2; i < items; i++ )
+        {
+            String value = (String) cache.get( i + ":key" );
+            assertEquals( "myregion" + " data " + i, value );
+        }
+
+    }
+
+    /**
+     * Put twice as many as the max, twice. verify that the second half is in
+     * the cache.
+     * 
+     * @throws CacheException
+     */
+    public void testPutGetThroughHubTwice()
+        throws CacheException
+    {
+        JCS cache = JCS.getInstance( "testPutGetThroughHub" );
+
+        int max = cache.getCacheAttributes().getMaxObjects();
+        int items = max * 2;
+
+        for ( int i = 0; i < items; i++ )
+        {
+            cache.put( i + ":key", "myregion" + " data " + i );
+        }
+
+        for ( int i = 0; i < items; i++ )
+        {
+            cache.put( i + ":key", "myregion" + " data " + i );
+        }
+
+        // Test that first items are not in the cache
+        for ( int i = max; i >= 0; i-- )
+        {
+            String value = (String) cache.get( i + ":key" );
+            assertNull( "Should not have value for key [" + i + ":key" + "] in the cache.", value );
+        }
+
+        // Test that last items are in cache
+        // skip 2 for the buffer.
+        for ( int i = max + 2; i < items; i++ )
+        {
+            String value = (String) cache.get( i + ":key" );
+            assertEquals( "myregion" + " data " + i, value );
+        }
+
+    }
+
+    /**
+     * put the max and remove each. verify that they are all null.
+     * 
+     * @throws CacheException
+     */
+    public void testPutRemoveThroughHub()
+        throws CacheException
+    {
+        JCS cache = JCS.getInstance( "testPutGetThroughHub" );
+
+        int max = cache.getCacheAttributes().getMaxObjects();
+        int items = max * 2;
+
+        for ( int i = 0; i < items; i++ )
+        {
+            cache.put( i + ":key", "myregion" + " data " + i );
+        }
+
+        for ( int i = 0; i < items; i++ )
+        {
+            cache.remove( i + ":key" );
+        }
+
+        // Test that first items are not in the cache
+        for ( int i = max; i >= 0; i-- )
+        {
+            String value = (String) cache.get( i + ":key" );
+            assertNull( "Should not have value for key [" + i + ":key" + "] in the cache.", value );
+        }
+    }
+
+    /**
+     * put the max and clear. verify that no elements remain.
+     * 
+     * @throws CacheException
+     */
+    public void testClearThroughHub()
+        throws CacheException
+    {
+        JCS cache = JCS.getInstance( "testPutGetThroughHub" );
+
+        int max = cache.getCacheAttributes().getMaxObjects();
+        int items = max * 2;
+
+        for ( int i = 0; i < items; i++ )
+        {
+            cache.put( i + ":key", "myregion" + " data " + i );
+        }
+
+        cache.clear();
+
+        // Test that first items are not in the cache
+        for ( int i = max; i >= 0; i-- )
+        {
+            String value = (String) cache.get( i + ":key" );
+            assertNull( "Should not have value for key [" + i + ":key" + "] in the cache.", value );
+        }
+    }
+
+    /**
+     * put twice the max and clear. verify that no elements remain.
+     * 
+     * @throws CacheException
+     */
+    public void testGetStatsThroughHub()
+        throws CacheException
+    {
+        JCS cache = JCS.getInstance( "testGetStatsThroughHub" );
+
+        int max = cache.getCacheAttributes().getMaxObjects();
+        int items = max * 2;
+
+        for ( int i = 0; i < items; i++ )
+        {
+            cache.put( i + ":key", "myregion" + " data " + i );
+        }
+
+        String stats = cache.getStats();
+
+        System.out.println( stats );
+
+        // TODO improve stats check
+        assertTrue( "Should have 200 puts", stats.indexOf( "2000" ) != -1 );
+    }
+
+    /**
+     * Put half the max and clear. get the key array and verify that it has the
+     * correct number of items.
+     * 
+     * @throws Exception
+     */
+    public void testGetKeyArray()
+        throws Exception
+    {
+        CompositeCacheManager cacheMgr = CompositeCacheManager.getUnconfiguredInstance();
+        cacheMgr.configure( "/TestMRUCache.ccf" );
+        CompositeCache cache = cacheMgr.getCache( "testGetKeyArray" );
+
+        MRUMemoryCache mru = new MRUMemoryCache();
+        mru.initialize( cache );
+
+        int max = cache.getCacheAttributes().getMaxObjects();
+        int items = max / 2;
+
+        for ( int i = 0; i < items; i++ )
+        {
+            ICacheElement ice = new CacheElement( cache.getCacheName(), i + ":key", cache.getCacheName() + " data " + i );
+            ice.setElementAttributes( cache.getElementAttributes().copy() );
+            mru.update( ice );
+        }
+
+        Object[] keys = mru.getKeyArray();
+
+        assertEquals( "Wrong number of keys.", items, keys.length );
+    }
+
+    
+    /**
+     * Add a few keys with the delimeter.  Remove them.
+     * 
+     * 
+     * @throws CacheException
+     */
+    public void testRemovePartialThroughHub()
+        throws CacheException
+    {
+        JCS cache = JCS.getInstance( "testGetStatsThroughHub" );
+
+        int max = cache.getCacheAttributes().getMaxObjects();
+        int items = max / 2;
+        
+        cache.put( "test", "data" );
+        
+        String root = "myroot";
+        
+        for ( int i = 0; i < items; i++ )
+        {
+            cache.put( root + ":" + i + ":key", "myregion" + " data " + i );
+        }
+        
+        // Test that last items are in cache
+        for ( int i = 0; i < items; i++ )
+        {
+            String value = (String) cache.get( root + ":" + i + ":key" );
+            assertEquals( "myregion" + " data " + i, value );
+        }
+        
+        // remove partial
+        cache.remove( root + ":" );
+        
+        for ( int i = 0; i < items; i++ )
+        {
+            assertNull( "Should have been removed by partial loop.", cache.get( root + ":" + i + ":key" ) );
+        }
+        
+        assertNotNull( "Other item should be in the cache.", cache.get( "test" ) );
+        
+    }
+}



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