Author: asmuts
Date: Fri Aug 18 09:20:15 2006
New Revision: 432628
URL: http://svn.apache.org/viewvc?rev=432628&view=rev
Log:
Adding file size verifying unit tests.
Made some reusable test utilities.
Improved optimization unit test to verify the exact expected file size.
Added:
jakarta/jcs/trunk/src/java/org/apache/jcs/utils/timing/ElapsedTimer.java
jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/indexed/DiskTestObject.java
jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/indexed/DiskTestObjectUtil.java
Removed:
jakarta/jcs/trunk/src/java/org/apache/jcs/utils/timing/StopWatch.java
Modified:
jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/disk/indexed/IndexedDiskCache.java
jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/indexed/IndexDiskCacheUnitTest.java
jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/indexed/IndexedDiskCacheOptimizationUnitTest.java
Modified:
jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/disk/indexed/IndexedDiskCache.java
URL:
http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/disk/indexed/IndexedDiskCache.java?rev=432628&r1=432627&r2=432628&view=diff
==============================================================================
---
jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/disk/indexed/IndexedDiskCache.java
(original)
+++
jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/disk/indexed/IndexedDiskCache.java
Fri Aug 18 09:20:15 2006
@@ -37,7 +37,7 @@
import org.apache.jcs.engine.stats.behavior.IStatElement;
import org.apache.jcs.engine.stats.behavior.IStats;
import org.apache.jcs.utils.struct.SortedPreferentialArray;
-import org.apache.jcs.utils.timing.StopWatch;
+import org.apache.jcs.utils.timing.ElapsedTimer;
import EDU.oswego.cs.dl.util.concurrent.ReentrantWriterPreferenceReadWriteLock;
@@ -270,7 +270,7 @@
*/
private boolean checkKeyDataConsistency( boolean checkForDedOverlaps )
{
- StopWatch timer = new StopWatch();
+ ElapsedTimer timer = new ElapsedTimer();
log.debug( logCacheName + "Performing inital consistency check" );
boolean isOk = true;
@@ -318,10 +318,12 @@
/**
* Detects any overlapping elements. This expects a sorted list.
* <p>
+ * The total length of an item is IndexedDisk.RECORD_HEADER + ded.len.
+ * <p>
* @param sortedDescriptors
* @return false if there are overlaps.
*/
- private boolean checkForDedOverlaps( IndexedDiskElementDescriptor[]
sortedDescriptors )
+ protected boolean checkForDedOverlaps( IndexedDiskElementDescriptor[]
sortedDescriptors )
{
long start = System.currentTimeMillis();
boolean isOk = true;
@@ -990,7 +992,7 @@
*/
protected void optimizeFile()
{
- StopWatch timer = new StopWatch();
+ ElapsedTimer timer = new ElapsedTimer();
timesOptimized++;
if ( log.isInfoEnabled() )
{
@@ -1073,7 +1075,7 @@
*/
private long defragFile( IndexedDiskElementDescriptor[] defragList, long
startingPos )
{
- StopWatch timer = new StopWatch();
+ ElapsedTimer timer = new ElapsedTimer();
long preFileSize = 0;
long postFileSize = 0;
long expectedNextPos = 0;
Added: jakarta/jcs/trunk/src/java/org/apache/jcs/utils/timing/ElapsedTimer.java
URL:
http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/java/org/apache/jcs/utils/timing/ElapsedTimer.java?rev=432628&view=auto
==============================================================================
--- jakarta/jcs/trunk/src/java/org/apache/jcs/utils/timing/ElapsedTimer.java
(added)
+++ jakarta/jcs/trunk/src/java/org/apache/jcs/utils/timing/ElapsedTimer.java
Fri Aug 18 09:20:15 2006
@@ -0,0 +1,47 @@
+package org.apache.jcs.utils.timing;
+
+/*
+ * Copyright 2001-2004 The Apache Software Foundation. Licensed under the
Apache License, Version
+ * 2.0 (the "License") you may not use this file except in compliance with the
License. You may
+ * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by
+ * applicable law or agreed to in writing, software distributed under the
License is distributed on
+ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied. See
+ * the License for the specific language governing permissions and limitations
under the License.
+ */
+
+/**
+ * This is a simple timer utility.
+ */
+public class ElapsedTimer
+{
+ private static final String SUFFIX = "ms.";
+
+ /**
+ * Sets the start time when created.
+ */
+ private long timeStamp = System.currentTimeMillis();
+
+ /**
+ * Gets the time elapsed between the start time and now. The start time is
reset to now.
+ * Subsequent calls will get the time between then and now.
+ * <p>
+ * @return
+ */
+ public long getElapsedTime()
+ {
+ long now = System.currentTimeMillis();
+ long elapsed = now - timeStamp;
+ timeStamp = now;
+ return elapsed;
+ }
+
+ /**
+ * Retuns the elapsed time with the display suffix.
+ * <p>
+ * @return formatted elapsed Time
+ */
+ public String getElapsedTimeString()
+ {
+ return String.valueOf( getElapsedTime() ) + SUFFIX;
+ }
+}
Added:
jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/indexed/DiskTestObject.java
URL:
http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/indexed/DiskTestObject.java?rev=432628&view=auto
==============================================================================
---
jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/indexed/DiskTestObject.java
(added)
+++
jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/indexed/DiskTestObject.java
Fri Aug 18 09:20:15 2006
@@ -0,0 +1,41 @@
+package org.apache.jcs.auxiliary.disk.indexed;
+
+/*
+ * Copyright 2001-2004 The Apache Software Foundation. Licensed under the
Apache License, Version
+ * 2.0 (the "License") you may not use this file except in compliance with the
License. You may
+ * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by
+ * applicable law or agreed to in writing, software distributed under the
License is distributed on
+ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied. See
+ * the License for the specific language governing permissions and limitations
under the License.
+ */
+
+import java.io.Serializable;
+
+/**
+ * Resembles a cached image.
+ */
+public class DiskTestObject
+ implements Serializable
+{
+ private static final long serialVersionUID = 1L;
+
+ /**
+ * Key
+ */
+ public Integer id;
+
+ /**
+ * Byte size
+ */
+ public byte[] imageBytes;
+
+ /**
+ * @param id
+ * @param imageBytes
+ */
+ public DiskTestObject( Integer id, byte[] imageBytes )
+ {
+ this.id = id;
+ this.imageBytes = imageBytes;
+ }
+}
\ No newline at end of file
Added:
jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/indexed/DiskTestObjectUtil.java
URL:
http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/indexed/DiskTestObjectUtil.java?rev=432628&view=auto
==============================================================================
---
jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/indexed/DiskTestObjectUtil.java
(added)
+++
jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/indexed/DiskTestObjectUtil.java
Fri Aug 18 09:20:15 2006
@@ -0,0 +1,130 @@
+package org.apache.jcs.auxiliary.disk.indexed;
+
+/*
+ * Copyright 2001-2004 The Apache Software Foundation. Licensed under the
Apache License, Version
+ * 2.0 (the "License") you may not use this file except in compliance with the
License. You may
+ * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by
+ * applicable law or agreed to in writing, software distributed under the
License is distributed on
+ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied. See
+ * the License for the specific language governing permissions and limitations
under the License.
+ */
+
+import java.io.IOException;
+import java.util.Random;
+
+import org.apache.jcs.engine.CacheElement;
+import org.apache.jcs.engine.behavior.ICacheElement;
+import org.apache.jcs.utils.serialization.StandardSerializer;
+
+/**
+ * Utility for dealing with test objects.
+ * <p>
+ * @author Aaron Smuts
+ */
+public class DiskTestObjectUtil
+{
+ /**
+ * Total from the start to the endPostion.
+ * <p>
+ * @param testObjects
+ * @param endPosition
+ * @return size
+ * @throws IOException
+ */
+ public static long totalSize( DiskTestObject[] testObjects, int
endPosition )
+ throws IOException
+ {
+ StandardSerializer serializer = new StandardSerializer();
+ long total = 0;
+ for ( int i = 0; i < endPosition; i++ )
+ {
+ int tileSize = serializer.serialize( testObjects[i] ).length +
IndexedDisk.RECORD_HEADER;
+ total += tileSize;
+ }
+ return total;
+ }
+
+ /**
+ * Total from the start to the endPostion.
+ * <p>
+ * @param elements
+ * @param endPosition
+ * @return size
+ * @throws IOException
+ */
+ public static long totalSize( ICacheElement[] elements, int endPosition )
+ throws IOException
+ {
+ return totalSize( elements, 0, endPosition );
+ }
+
+ /**
+ * Total from the start to the endPostion.
+ * <p>
+ * @param elements
+ * @param startPosition
+ * @param endPosition
+ * @return size
+ * @throws IOException
+ */
+ public static long totalSize( ICacheElement[] elements, int startPosition,
int endPosition )
+ throws IOException
+ {
+ StandardSerializer serializer = new StandardSerializer();
+ long total = 0;
+ for ( int i = startPosition; i < endPosition; i++ )
+ {
+ int tileSize = serializer.serialize( elements[i] ).length +
IndexedDisk.RECORD_HEADER;
+ total += tileSize;
+ }
+ return total;
+ }
+
+ /**
+ * Creates an array of ICacheElements with DiskTestObjects with payloads
the byte size.
+ * <p>
+ * @param numToCreate
+ * @param bytes
+ * @param cacheName
+ * @return ICacheElement[]
+ */
+ public static ICacheElement[] createCacheElementsWithTestObjects( int
numToCreate, int bytes, String cacheName )
+ {
+ ICacheElement[] elements = new ICacheElement[numToCreate];
+ for ( int i = 0; i < numToCreate; i++ )
+ {
+ // 24 KB
+ int size = bytes * 1024;
+ DiskTestObject tile = new DiskTestObject( new Integer( i ), new
byte[size] );
+
+ ICacheElement element = new CacheElement( cacheName, tile.id, tile
);
+ elements[i] = element;
+ }
+ return elements;
+ }
+
+ /**
+ * Creates an array of ICacheElements with DiskTestObjects with payloads
the byte size.
+ * <p>
+ * @param numToCreate
+ * @param cacheName
+ * @return ICacheElement[]
+ */
+ public static ICacheElement[]
createCacheElementsWithTestObjectsOfVariableSizes( int numToCreate, String
cacheName )
+ {
+ ICacheElement[] elements = new ICacheElement[numToCreate];
+ Random random = new Random( 89 );
+ for ( int i = 0; i < numToCreate; i++ )
+ {
+ int bytes = random.nextInt( 20 );
+ // 4-24 KB
+ int size = ( bytes + 4 ) * 1024;
+ DiskTestObject tile = new DiskTestObject( new Integer( i ), new
byte[size] );
+
+ ICacheElement element = new CacheElement( cacheName, tile.id, tile
);
+ elements[i] = element;
+ }
+ return elements;
+ }
+
+}
Modified:
jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/indexed/IndexDiskCacheUnitTest.java
URL:
http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/indexed/IndexDiskCacheUnitTest.java?rev=432628&r1=432627&r2=432628&view=diff
==============================================================================
---
jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/indexed/IndexDiskCacheUnitTest.java
(original)
+++
jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/indexed/IndexDiskCacheUnitTest.java
Fri Aug 18 09:20:15 2006
@@ -1,5 +1,7 @@
package org.apache.jcs.auxiliary.disk.indexed;
+import java.io.IOException;
+
import junit.framework.TestCase;
import org.apache.jcs.engine.CacheElement;
@@ -15,7 +17,6 @@
public class IndexDiskCacheUnitTest
extends TestCase
{
-
/**
* Simply verify that we can put items in the disk cache and retrieve them.
*/
@@ -156,6 +157,99 @@
}
disk.removeAll();
+ }
+
+ /**
+ * Verify that the overlap check returns true when there are no overlaps.
+ */
+ public void testCheckForDedOverlaps_noOverlap()
+ {
+ // SETUP
+ IndexedDiskCacheAttributes cattr = new IndexedDiskCacheAttributes();
+ cattr.setCacheName( "testCheckForDedOverlaps_noOverlap" );
+ cattr.setDiskPath( "target/test-sandbox/UnitTest" );
+ IndexedDiskCache disk = new IndexedDiskCache( cattr );
+
+ int numDescriptors = 5;
+ int pos = 0;
+ IndexedDiskElementDescriptor[] sortedDescriptors = new
IndexedDiskElementDescriptor[numDescriptors];
+ for ( int i = 0; i < numDescriptors; i++ )
+ {
+ IndexedDiskElementDescriptor descriptor = new
IndexedDiskElementDescriptor( pos, i * 2 );
+ pos = pos + ( i * 2 ) + IndexedDisk.RECORD_HEADER;
+ sortedDescriptors[i] = descriptor;
+ }
+
+ // DO WORK
+ boolean result = disk.checkForDedOverlaps( sortedDescriptors );
+
+ // VERIFY
+ assertTrue( "There should be no overlap. it should be ok", result );
+ }
+
+ /**
+ * Verify that the overlap check returns false when there are overlaps.
+ */
+ public void testCheckForDedOverlaps_overlaps()
+ {
+ // SETUP
+ IndexedDiskCacheAttributes cattr = new IndexedDiskCacheAttributes();
+ cattr.setCacheName( "testCheckForDedOverlaps_overlaps" );
+ cattr.setDiskPath( "target/test-sandbox/UnitTest" );
+ IndexedDiskCache disk = new IndexedDiskCache( cattr );
+
+ int numDescriptors = 5;
+ int pos = 0;
+ IndexedDiskElementDescriptor[] sortedDescriptors = new
IndexedDiskElementDescriptor[numDescriptors];
+ for ( int i = 0; i < numDescriptors; i++ )
+ {
+ IndexedDiskElementDescriptor descriptor = new
IndexedDiskElementDescriptor( pos, i * 2 );
+ // don't add the header + IndexedDisk.RECORD_HEADER;
+ pos = pos + ( i * 2 );
+ sortedDescriptors[i] = descriptor;
+ }
+
+ // DO WORK
+ boolean result = disk.checkForDedOverlaps( sortedDescriptors );
+
+ // VERIFY
+ assertFalse( "There should be overlaps. it should be not ok", result );
+ }
+
+ /**
+ * Verify that the file size is as expected.
+ * @throws IOException
+ * @throws InterruptedException
+ */
+ public void testFileSize()
+ throws IOException, InterruptedException
+ {
+ // SETUP
+ IndexedDiskCacheAttributes cattr = new IndexedDiskCacheAttributes();
+ cattr.setCacheName( "testFileSize" );
+ cattr.setDiskPath( "target/test-sandbox/UnitTest" );
+ IndexedDiskCache disk = new IndexedDiskCache( cattr );
+
+ int numberToInsert = 20;
+ int bytes = 24;
+ ICacheElement[] elements =
DiskTestObjectUtil.createCacheElementsWithTestObjects( numberToInsert, bytes,
cattr
+ .getCacheName() );
+
+ for ( int i = 0; i < elements.length; i++ )
+ {
+ disk.doUpdate( elements[i] );
+ }
+
+ Thread.yield();
+ Thread.sleep( 100 );
+ Thread.yield();
+
+ long expectedSize = DiskTestObjectUtil.totalSize( elements,
numberToInsert );
+ long resultSize = disk.getDataFileSize();
+
+ System.out.println( "testFileSize stats " + disk.getStats() );
+
+ assertEquals( "Wrong file size", expectedSize, resultSize );
}
}
Modified:
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=432628&r1=432627&r2=432628&view=diff
==============================================================================
---
jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/indexed/IndexedDiskCacheOptimizationUnitTest.java
(original)
+++
jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/indexed/IndexedDiskCacheOptimizationUnitTest.java
Fri Aug 18 09:20:15 2006
@@ -1,11 +1,7 @@
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;
/**
@@ -33,27 +29,21 @@
cattr.setDiskPath( "target/test-sandbox/testOptimization" );
IndexedDiskCache disk = new IndexedDiskCache( cattr );
+ disk.removeAll();
+
int numberToInsert = removeCount * 2;
+ ICacheElement[] elements = DiskTestObjectUtil
+ .createCacheElementsWithTestObjectsOfVariableSizes(
numberToInsert, cattr.getCacheName() );
- int[] sizes = new int[numberToInsert];
- Random random = new Random( 89 );
- for ( int i = 0; i < numberToInsert; i++ )
+ for ( int i = 0; i < elements.length; 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 );
+ disk.doUpdate( elements[i] );
}
Thread.sleep( 1000 );
long sizeBeforeRemove = disk.getDataFileSize();
System.out.println( "file sizeBeforeRemove " + sizeBeforeRemove );
- System.out.println( "totalSize inserted " + totalSize( sizes,
numberToInsert ) );
+ System.out.println( "totalSize inserted " +
DiskTestObjectUtil.totalSize( elements, numberToInsert ) );
for ( int i = 0; i < removeCount; i++ )
{
@@ -65,59 +55,11 @@
Thread.sleep( 100 );
long sizeAfterRemove = disk.getDataFileSize();
System.out.println( "file sizeAfterRemove " + sizeAfterRemove );
- System.out.println( "totalSize expected after remove " + totalSize(
sizes, removeCount ) );
+ long expectedSizeAfterRemove = DiskTestObjectUtil.totalSize( elements,
removeCount, elements.length );
+ System.out.println( "totalSize expected after remove " +
expectedSizeAfterRemove );
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;
- }
+ assertEquals( "The file size is not as expected size.",
expectedSizeAfterRemove, sizeAfterRemove );
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]