jakarta-jcs-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From asm...@apache.org
Subject svn commit: r432579 [2/2] - in /jakarta/jcs/trunk/src: java/org/apache/jcs/auxiliary/disk/indexed/ java/org/apache/jcs/utils/timing/ test-conf/ test/org/apache/jcs/auxiliary/disk/indexed/
Date Fri, 18 Aug 2006 13:30:46 GMT
Modified: jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/indexed/IndexedDiskCacheKeyStoreUnitTest.java
URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/indexed/IndexedDiskCacheKeyStoreUnitTest.java?rev=432579&r1=432578&r2=432579&view=diff
==============================================================================
--- jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/indexed/IndexedDiskCacheKeyStoreUnitTest.java
(original)
+++ jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/indexed/IndexedDiskCacheKeyStoreUnitTest.java
Fri Aug 18 06:30:45 2006
@@ -40,7 +40,7 @@
         {
             IElementAttributes eAttr = new ElementAttributes();
             eAttr.setIsSpool( true );
-            ICacheElement element = new CacheElement( "testStoreKeys", "key:" + i, "data:"
+ i );
+            ICacheElement element = new CacheElement( cattr.getCacheName(), "key:" + i, "data:"
+ i );
             element.setElementAttributes( eAttr );
             disk.doUpdate( element );
         }
@@ -82,7 +82,7 @@
         throws Exception
     {
         IndexedDiskCacheAttributes cattr = new IndexedDiskCacheAttributes();
-        cattr.setCacheName( "testOptiimize" );
+        cattr.setCacheName( "testOptimize" );
         cattr.setMaxKeySize( 100 );
         cattr.setDiskPath( "target/test-sandbox/KeyStoreUnitTest" );
         IndexedDiskCache disk = new IndexedDiskCache( cattr );
@@ -94,7 +94,7 @@
         {
             IElementAttributes eAttr = new ElementAttributes();
             eAttr.setIsSpool( true );
-            ICacheElement element = new CacheElement( "testOptiimize", "key:" + i, "data:"
+ i );
+            ICacheElement element = new CacheElement( cattr.getCacheName(), "key:" + i, "data:"
+ i );
             element.setElementAttributes( eAttr );
             disk.doUpdate( element );
         }
@@ -103,7 +103,7 @@
         
         IElementAttributes eAttr = new ElementAttributes();
         eAttr.setIsSpool( true );
-        ICacheElement elementSetup = new CacheElement( "testOptiimize", "key:" + "A", "data:"
+ "A" );
+        ICacheElement elementSetup = new CacheElement( cattr.getCacheName(), "key:" + "A",
"data:" + "A" );
         elementSetup.setElementAttributes( eAttr );
         disk.doUpdate( elementSetup );
                 
@@ -115,7 +115,7 @@
 
         long preSize = disk.getDataFileSize();
         // synchronous versoin
-        disk.optimizeRealTime();
+        disk.optimizeFile(); //deoptimizeRealTime();
         long postSize = disk.getDataFileSize();
         
         System.out.println( "preAddRemoveSize " + preAddRemoveSize );
@@ -130,11 +130,6 @@
             ICacheElement element = disk.doGet( "key:" + i );
             assertNotNull( "postsave, Should have recevied an element.", element );
             assertEquals( "postsave, element is wrong.", "data:" + i, element.getVal() );
-        }
-        
-        
-        
-    }
-        
-        
+        }                     
+    }             
 }

Added: jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/indexed/IndexedDiskCacheOptimizationUnitTest.java
URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/indexed/IndexedDiskCacheOptimizationUnitTest.java?rev=432579&view=auto
==============================================================================
--- jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/indexed/IndexedDiskCacheOptimizationUnitTest.java
(added)
+++ jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/indexed/IndexedDiskCacheOptimizationUnitTest.java
Fri Aug 18 06:30:45 2006
@@ -0,0 +1,123 @@
+package org.apache.jcs.auxiliary.disk.indexed;
+
+import java.io.Serializable;
+import java.util.Random;
+
+import junit.framework.TestCase;
+
+import org.apache.jcs.engine.CacheElement;
+import org.apache.jcs.engine.behavior.ICacheElement;
+
+/**
+ * @author Aaron Smuts
+ */
+public class IndexedDiskCacheOptimizationUnitTest
+    extends TestCase
+{
+
+    /**
+     * Set the optimize at remove count to 10. Add 20. Check the file size. Remove 10. Check
the
+     * times optimized. Check the file size.
+     * @throws Exception
+     */
+    public void testBasicOptimization()
+        throws Exception
+    {
+        int removeCount = 50;
+
+        IndexedDiskCacheAttributes cattr = new IndexedDiskCacheAttributes();
+        cattr.setCacheName( "testOptimization" );
+        cattr.setMaxKeySize( removeCount * 3 );
+        cattr.setOptimizeAtRemoveCount( removeCount );
+        cattr.setMaxRecycleBinSize( removeCount * 3 );
+        cattr.setDiskPath( "target/test-sandbox/testOptimization" );
+        IndexedDiskCache disk = new IndexedDiskCache( cattr );
+
+        int numberToInsert = removeCount * 2;
+
+        int[] sizes = new int[numberToInsert];
+        Random random = new Random( 89 );
+        for ( int i = 0; i < numberToInsert; i++ )
+        {
+            int bytes = random.nextInt( 20 );
+            // 4-24 KB
+            int size = ( bytes + 4 ) * 1024;
+            sizes[i] = size;
+            Tile tile = new Tile( new Integer( i ), new byte[size] );
+            // images
+
+            ICacheElement element = new CacheElement( cattr.getCacheName(), tile.id, tile
);
+            disk.doUpdate( element );
+        }
+
+        Thread.sleep( 1000 );
+        long sizeBeforeRemove = disk.getDataFileSize();
+        System.out.println( "file sizeBeforeRemove " + sizeBeforeRemove );
+        System.out.println( "totalSize inserted " + totalSize( sizes, numberToInsert ) );
+
+        for ( int i = 0; i < removeCount; i++ )
+        {
+            disk.doRemove( new Integer( i ) );
+        }
+
+        Thread.sleep( 100 );
+        Thread.yield();
+        Thread.sleep( 100 );
+        long sizeAfterRemove = disk.getDataFileSize();
+        System.out.println( "file sizeAfterRemove " + sizeAfterRemove );
+        System.out.println( "totalSize expected after remove " + totalSize( sizes, removeCount
) );
+
+        assertTrue( "The post optimization size should be smaller.", sizeAfterRemove <
sizeBeforeRemove );
+
+        long reality = Math.abs( totalSize( sizes, removeCount ) - sizeAfterRemove );
+        assertTrue( "The file size should be within 15% of the expected size. reality = "
+ reality,
+                    reality < (sizeAfterRemove * 1.15 ) - sizeAfterRemove );
+        // TODO figure out the estimated size purportion.
+    }
+
+    /**
+     * Total from the start to the endPostion.
+     * <p>
+     * @param sizes
+     * @param endPosition
+     * @return size
+     */
+    private long totalSize( int[] sizes, int endPosition )
+    {
+        long total = 0;
+        for ( int i = 0; i < endPosition; i++ )
+        {
+            total += sizes[i];
+        }
+        return total;
+    }
+
+    /**
+     * Resembles a cached image.
+     */
+    private static class Tile
+        implements Serializable
+    {
+        private static final long serialVersionUID = 1L;
+
+        /**
+         * Key
+         */
+        public Integer id;
+
+        /**
+         * Byte size
+         */
+        public byte[] imageBytes;
+
+        /**
+         * @param id
+         * @param imageBytes
+         */
+        public Tile( Integer id, byte[] imageBytes )
+        {
+            this.id = id;
+            this.imageBytes = imageBytes;
+        }
+    }
+}



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


Mime
View raw message