Added: jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheConcurrentUnitTest.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheConcurrentUnitTest.java?rev=437311&view=auto ============================================================================== --- jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheConcurrentUnitTest.java (added) +++ jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheConcurrentUnitTest.java Sat Aug 26 23:45:57 2006 @@ -0,0 +1,206 @@ +package org.apache.jcs.auxiliary.disk.block; + +/* + * 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 junit.extensions.ActiveTestSuite; +import junit.framework.Test; +import junit.framework.TestCase; + +import org.apache.jcs.JCS; + +/** + * Test which exercises the block disk cache. This one uses three different + * regions for thre threads. + */ +public class BlockDiskCacheConcurrentUnitTest + extends TestCase +{ + /** + * Number of items to cache, twice the configured maxObjects for the memory + * cache regions. + */ + private static int items = 200; + + /** + * Constructor for the TestDiskCache object. + * + * @param testName + */ + public BlockDiskCacheConcurrentUnitTest( String testName ) + { + super( testName ); + } + + /** + * Main method passes this test to the text test runner. + * + * @param args + */ + public static void main( String args[] ) + { + String[] testCaseName = { BlockDiskCacheConcurrentUnitTest.class.getName() }; + junit.textui.TestRunner.main( testCaseName ); + } + + /** + * A unit test suite for JUnit + * + * @return The test suite + */ + public static Test suite() + { + ActiveTestSuite suite = new ActiveTestSuite(); + + suite.addTest( new BlockDiskCacheConcurrentUnitTest( "testBlockDiskCache1" ) + { + public void runTest() + throws Exception + { + this.runTestForRegion( "indexedRegion1" ); + } + } ); + + suite.addTest( new BlockDiskCacheConcurrentUnitTest( "testBlockDiskCache2" ) + { + public void runTest() + throws Exception + { + this.runTestForRegion( "indexedRegion2" ); + } + } ); + + suite.addTest( new BlockDiskCacheConcurrentUnitTest( "testBlockDiskCache3" ) + { + public void runTest() + throws Exception + { + this.runTestForRegion( "indexedRegion3" ); + } + } ); + + suite.addTest( new BlockDiskCacheConcurrentUnitTest( "testBlockDiskCache4" ) + { + public void runTest() + throws Exception + { + this.runTestForRegionInRange( "indexedRegion3", 300, 600 ); + } + } ); + + return suite; + } + + /** + * Test setup + */ + public void setUp() + { + JCS.setConfigFilename( "/TestBlockDiskCache.ccf" ); + } + + /** + * Adds items to cache, gets them, and removes them. The item count is more + * than the size of the memory cache, so items should spool to disk. + * + * @param region + * Name of the region to access + * + * @exception Exception + * If an error occurs + */ + public void runTestForRegion( String region ) + throws Exception + { + JCS jcs = JCS.getInstance( region ); + + // Add items to cache + for ( int i = 0; i <= items; i++ ) + { + jcs.put( i + ":key", region + " data " + i ); + } + + // Test that all items are in cache + for ( int i = 0; i <= items; i++ ) + { + String value = (String) jcs.get( i + ":key" ); + + assertEquals( region + " data " + i, value ); + } + + // Remove all the items + for ( int i = 0; i <= items; i++ ) + { + jcs.remove( i + ":key" ); + } + + // Verify removal + // another thread may have inserted since + for ( int i = 0; i <= items; i++ ) + { + assertNull( "Removed key should be null: " + i + ":key" + "\n stats " + jcs.getStats(), jcs + .get( i + ":key" ) ); + } + } + + /** + * Adds items to cache, gets them, and removes them. The item count is more + * than the size of the memory cache, so items should spool to disk. + * + * @param region + * Name of the region to access + * @param start + * @param end + * + * @exception Exception + * If an error occurs + */ + public void runTestForRegionInRange( String region, int start, int end ) + throws Exception + { + JCS jcs = JCS.getInstance( region ); + + // Add items to cache + for ( int i = start; i <= end; i++ ) + { + jcs.put( i + ":key", region + " data " + i ); + } + + // Test that all items are in cache + for ( int i = start; i <= end; i++ ) + { + String value = (String) jcs.get( i + ":key" ); + + assertEquals( region + " data " + i, value ); + } + + // Remove all the items + for ( int i = start; i <= end; i++ ) + { + jcs.remove( i + ":key" ); + } + + System.out.println( jcs.getStats() ); + + // Verify removal + // another thread may have inserted since + for ( int i = start; i <= end; i++ ) + { + assertNull( "Removed key should be null: " + i + ":key " + "\n stats " + jcs.getStats(), jcs.get( i + + ":key" ) ); + } + } +}
Added: jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheRandomConcurrentTestUtil.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheRandomConcurrentTestUtil.java?rev=437311&view=auto ============================================================================== --- jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheRandomConcurrentTestUtil.java (added) +++ jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheRandomConcurrentTestUtil.java Sat Aug 26 23:45:57 2006 @@ -0,0 +1,66 @@ +package org.apache.jcs.auxiliary.disk.block; + +import junit.framework.TestCase; + +import org.apache.jcs.JCS; +import org.apache.jcs.access.TestCacheAccess; + +/** + * This is used by other tests to generate a random load on the disk cache. + */ +public class BlockDiskCacheRandomConcurrentTestUtil + extends TestCase +{ + + /** + * Constructor for the TestDiskCache object. + * + * @param testName + */ + public BlockDiskCacheRandomConcurrentTestUtil( String testName ) + { + super( testName ); + } + + /** + * Randomly adds items to cache, gets them, and removes them. The range + * count is more than the size of the memory cache, so items should spool to + * disk. + * + * @param region + * Name of the region to access + * @param range + * @param numOps + * @param testNum + * + * @exception Exception + * If an error occurs + */ + public void runTestForRegion( String region, int range, int numOps, int testNum ) + throws Exception + { + // run a rondom operation test to detect deadlocks + TestCacheAccess tca = new TestCacheAccess( "/TestBlockDiskCacheCon.ccf" ); + tca.setRegion( region ); + tca.random( range, numOps ); + + // make sure a simple put then get works + // this may fail if the other tests are flooding the disk cache + JCS jcs = JCS.getInstance( region ); + String key = "testKey" + testNum; + String data = "testData" + testNum; + jcs.put( key, data ); + String value = (String) jcs.get( key ); + assertEquals( data, value ); + + } + + /** + * Test setup + */ + public void setUp() + { + JCS.setConfigFilename( "/TestBlockDiskCacheCon.ccf" ); + } + +} Added: jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheSameRegionConcurrentUnitTest.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheSameRegionConcurrentUnitTest.java?rev=437311&view=auto ============================================================================== --- jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheSameRegionConcurrentUnitTest.java (added) +++ jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheSameRegionConcurrentUnitTest.java Sat Aug 26 23:45:57 2006 @@ -0,0 +1,136 @@ +package org.apache.jcs.auxiliary.disk.block; + +/* + * 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 junit.extensions.ActiveTestSuite; +import junit.framework.Test; +import junit.framework.TestCase; + +import org.apache.jcs.JCS; + +/** + * Test which exercises the block disk cache. Runs three threads against the same region. + */ +public class BlockDiskCacheSameRegionConcurrentUnitTest + extends TestCase +{ + /** + * Constructor for the TestDiskCache object. + * @param testName + */ + public BlockDiskCacheSameRegionConcurrentUnitTest( String testName ) + { + super( testName ); + } + + /** + * Main method passes this test to the text test runner. + * @param args + */ + public static void main( String args[] ) + { + String[] testCaseName = { BlockDiskCacheSameRegionConcurrentUnitTest.class.getName() }; + junit.textui.TestRunner.main( testCaseName ); + } + + /** + * A unit test suite for JUnit + * @return The test suite + */ + public static Test suite() + { + ActiveTestSuite suite = new ActiveTestSuite(); + + suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache1" ) + { + public void runTest() + throws Exception + { + this.runTestForRegion( "blockRegion4", 0, 200 ); + } + } ); + + suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache2" ) + { + public void runTest() + throws Exception + { + this.runTestForRegion( "blockRegion4", 1000, 1200 ); + } + } ); + + suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache3" ) + { + public void runTest() + throws Exception + { + this.runTestForRegion( "blockRegion4", 2000, 2200 ); + } + } ); + + suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache4" ) + { + public void runTest() + throws Exception + { + this.runTestForRegion( "blockRegion4", 2200, 5200 ); + } + } ); + + suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache5" ) + { + public void runTest() + throws Exception + { + this.runTestForRegion( "blockRegion4", 0, 5200 ); + } + } ); + + return suite; + } + + /** + * Test setup + */ + public void setUp() + { + JCS.setConfigFilename( "/TestBlockDiskCacheCon.ccf" ); + } + + /** + * Adds items to cache, gets them, and removes them. The item count is more than the size of the + * memory cache, so items should spool to disk. + * @param region Name of the region to access + * @param start + * @param end + * @exception Exception If an error occurs + */ + public void runTestForRegion( String region, int start, int end ) + throws Exception + { + JCS jcs = JCS.getInstance( region ); + + // Add items to cache + + for ( int i = start; i <= end; i++ ) + { + jcs.put( i + ":key", region + " data " + i ); + } + + // Test that all items are in cache + + for ( int i = start; i <= end; i++ ) + { + String value = (String) jcs.get( i + ":key" ); + + assertEquals( region + " data " + i, value ); + } + } +} Added: jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheSteadyLoadTest.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheSteadyLoadTest.java?rev=437311&view=auto ============================================================================== --- jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheSteadyLoadTest.java (added) +++ jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskCacheSteadyLoadTest.java Sat Aug 26 23:45:57 2006 @@ -0,0 +1,141 @@ +package org.apache.jcs.auxiliary.disk.block; + +import java.text.DecimalFormat; +import java.util.Random; + +import junit.framework.TestCase; + +import org.apache.jcs.JCS; +import org.apache.jcs.auxiliary.disk.DiskTestObject; +import org.apache.jcs.utils.timing.ElapsedTimer; + +/** + * This allows you to put thousands of large objects into the disk cache and to force removes to + * trigger optimizations along the way. + * <p> + * @author Aaron Smuts + */ +public class BlockDiskCacheSteadyLoadTest + extends TestCase +{ + private static final String LOG_DIVIDER = "---------------------------"; + + private static Runtime rt = Runtime.getRuntime(); + + private static DecimalFormat format = new DecimalFormat( "#,###" ); + + /** + * Insert 2000 wait 1 second, repeat. Average 1000 / sec. + * <p> + * @throws Exception + */ + public void testRunSteadyLoadTest() + throws Exception + { + JCS.setConfigFilename( "/TestBlockDiskCacheSteadyLoad.ccf" ); + + System.out.println( "runSteadyLoadTest" ); + + logMemoryUsage(); + + int numPerRun = 250; + long pauseBetweenRuns = 1000; + int runCount = 0; + int runs = 1000; + int upperKB = 50; + + JCS jcs = JCS.getInstance( ( numPerRun / 2 ) + "aSecond" ); + + ElapsedTimer timer = new ElapsedTimer(); + int numToGet = numPerRun * ( runs / 10 ); + for ( int i = 0; i < numToGet; i++ ) + { + jcs.get( String.valueOf( i ) ); + } + System.out.println( LOG_DIVIDER ); + System.out.println( "After getting " + numToGet ); + System.out.println( "Elapsed " + timer.getElapsedTimeString() ); + logMemoryUsage(); + + jcs.clear(); + Thread.sleep( 3000 ); + System.out.println( LOG_DIVIDER ); + System.out.println( "Start putting" ); + + long totalSize = 0; + int totalPut = 0; + + Random random = new Random( 89 ); + while ( runCount < runs ) + { + runCount++; + for ( int i = 0; i < numPerRun; i++ ) + { + // 1/2 upper to upperKB-4 KB + int kiloBytes = Math.max( upperKB / 2, random.nextInt( upperKB ) ); + int bytes = ( kiloBytes ) * 1024; + totalSize += bytes; + totalPut++; + DiskTestObject object = new DiskTestObject( new Integer( i ), new byte[bytes] ); + jcs.put( String.valueOf( totalPut ), object ); + } + + // get half of those inserted the previous run + if ( runCount > 1 ) + { + for ( int j = ( ( totalPut - numPerRun ) - ( numPerRun / 2 ) ); j < ( totalPut - numPerRun ); j++ ) + { + jcs.get( String.valueOf( j ) ); + } + } + + // remove half of those inserted the previous run + if ( runCount > 1 ) + { + for ( int j = ( ( totalPut - numPerRun ) - ( numPerRun / 2 ) ); j < ( totalPut - numPerRun ); j++ ) + { + jcs.remove( String.valueOf( j ) ); + } + } + + + Thread.sleep( pauseBetweenRuns ); + if ( runCount % 1 == 0 ) + { + System.out.println( LOG_DIVIDER ); + System.out.println( "Elapsed " + timer.getElapsedTimeString() ); + System.out.println( "Run count: " + runCount + " Average size: " + ( totalSize / totalPut ) + "\n" + + jcs.getStats() ); + logMemoryUsage(); + } + } + + Thread.sleep( 3000 ); + System.out.println( jcs.getStats() ); + logMemoryUsage(); + + Thread.sleep( 10000 ); + System.out.println( jcs.getStats() ); + logMemoryUsage(); + + System.gc(); + Thread.sleep( 3000 ); + System.gc(); + System.out.println( jcs.getStats() ); + logMemoryUsage(); + } + + /** + * Logs the memory usage. + */ + private static void logMemoryUsage() + { + long byte2MB = 1024 * 1024; + long total = rt.totalMemory() / byte2MB; + long free = rt.freeMemory() / byte2MB; + long used = total - free; + System.out.println( LOG_DIVIDER ); + System.out.println( "Memory:" + " Used:" + format.format( used ) + "MB" + " Free:" + format.format( free ) + + "MB" + " Total:" + format.format( total ) + "MB" ); + } +} Added: jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskElementDescriptorUnitTest.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskElementDescriptorUnitTest.java?rev=437311&view=auto ============================================================================== --- jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskElementDescriptorUnitTest.java (added) +++ jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskElementDescriptorUnitTest.java Sat Aug 26 23:45:57 2006 @@ -0,0 +1,70 @@ +package org.apache.jcs.auxiliary.disk.block; + +import junit.framework.TestCase; + +/** + * Simple tests for the element descriptor + * <p> + * @author Aaron Smuts + */ +public class BlockDiskElementDescriptorUnitTest + extends TestCase +{ + + /** + * Verify that the memory used per element is reasonable. + * <p> + * TODO figure out a more precise expectation. + * <p> + * @throws Exception + */ + public void testMemorySize() + throws Exception + { + // SETUP + long memoryBefore = measureMemoryUse(); + System.out.println( "Before: " + memoryBefore ); + + int numElements = 25000; + BlockDiskElementDescriptor[] elements = new BlockDiskElementDescriptor[numElements]; + + long memoryStart = measureMemoryUse(); + System.out.println( "Start: " + memoryStart ); + + // DO WORK + for ( int i = 0; i < numElements; i++ ) + { + BlockDiskElementDescriptor descriptor = new BlockDiskElementDescriptor(); + descriptor.setKey( new Integer( i ) ); + descriptor.setBlocks( new int[] { 1, 2 } ); + elements[i] = descriptor; + } + + // VERIFY + long memoryEnd = measureMemoryUse(); + System.out.println( "End: " + memoryEnd ); + + long diff = memoryEnd - memoryStart; + System.out.println( "diff: " + diff ); + + long perDiff = diff / numElements; + System.out.println( "per diff: " + perDiff ); + + // about 20 bytes each + assertTrue( "Too much was used.", perDiff < 75 ); + } + + /** + * Measure memory used by the VM. + * @return + * @throws InterruptedException + */ + protected long measureMemoryUse() + throws InterruptedException + { + System.gc(); + Thread.sleep( 3000 ); + System.gc(); + return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); + } +} Added: jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskUnitTest.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskUnitTest.java?rev=437311&view=auto ============================================================================== --- jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskUnitTest.java (added) +++ jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/BlockDiskUnitTest.java Sat Aug 26 23:45:57 2006 @@ -0,0 +1,226 @@ +package org.apache.jcs.auxiliary.disk.block; + +import java.io.File; + +import junit.framework.TestCase; + +/** + * Test for the disk acces layer of the Block Disk Cache. + * <p> + * @author Aaron Smuts + */ +public class BlockDiskUnitTest + extends TestCase +{ + private File rafDir; + + /** + * Creates the base directory + */ + public BlockDiskUnitTest() + { + String rootDirName = "target/test-sandbox/block"; + this.rafDir = new File( rootDirName ); + this.rafDir.mkdirs(); + } + + /** + * Test writing an element within a single block size. + * <p> + * @throws Exception + */ + public void testWriteSingleBlockElement() + throws Exception + { + // SETUP + String fileName = "testWriteSingleBlockElement"; + File file = new File( rafDir, fileName + ".data" ); + file.delete(); + BlockDisk disk = new BlockDisk( file ); + + // DO WORK + int bytes = 1 * 1024; + int[] blocks = disk.write( new byte[bytes] ); + + // VERIFY + System.out.println( "testWriteSingleBlockElement " + disk ); + assertEquals( "Wrong number of blocks recorded.", 1, disk.getNumberOfBlocks() ); + assertEquals( "Wrong number of blocks returned.", 1, blocks.length ); + assertEquals( "Wrong block returned.", 0, blocks[0] ); + } + + /** + * Test writing and reading an element within a single block size. + * <p> + * @throws Exception + */ + public void testWriteAndReadSingleBlockElement() + throws Exception + { + // SETUP + String fileName = "testWriteAndReadSingleBlockElement"; + File file = new File( rafDir, fileName + ".data" ); + file.delete(); + BlockDisk disk = new BlockDisk( file ); + + // DO WORK + int bytes = 1 * 1024; + int[] blocks = disk.write( new byte[bytes] ); + + byte[] result = (byte[]) disk.read( blocks ); + + // VERIFY + assertEquals( "Wrong item retured.", new byte[bytes].length, result.length ); + } + + /** + * Test writing two elements that each fit within a single block size. + * <p> + * @throws Exception + */ + public void testWriteTwoSingleBlockElements() + throws Exception + { + // SETUP + String fileName = "testWriteSingleBlockElement"; + File file = new File( rafDir, fileName + ".data" ); + file.delete(); + BlockDisk disk = new BlockDisk( file ); + + // DO WORK + int bytes = 1 * 1024; + int[] blocks1 = disk.write( new byte[bytes] ); + int[] blocks2 = disk.write( new byte[bytes] ); + + // VERIFY + assertEquals( "Wrong number of blocks recorded.", 2, disk.getNumberOfBlocks() ); + assertEquals( "Wrong number of blocks returned.", 1, blocks1.length ); + assertEquals( "Wrong block returned.", 0, blocks1[0] ); + assertEquals( "Wrong number of blocks returned.", 1, blocks2.length ); + assertEquals( "Wrong block returned.", 1, blocks2[0] ); + } + + /** + * Verify that it says we need two blocks if the total size will fit. + * <p> + * @throws Exception + */ + public void testCalculateBlocksNeededDouble() + throws Exception + { + // SETUP + String fileName = "testCalculateBlocksNeededDouble"; + File file = new File( rafDir, fileName + ".data" ); + file.delete(); + BlockDisk disk = new BlockDisk( file ); + + // DO WORK + int result = disk.calculateTheNumberOfBlocksNeeded( new byte[disk.getBlockSizeBytes() * 2 + - ( 2 * BlockDisk.HEADER_SIZE_BYTES )] ); + + // Verify + assertEquals( "Wrong number of blocks", 2, result ); + } + + /** + * Test writing an element that takes two blocks. + * <p> + * @throws Exception + */ + public void testWriteDoubleBlockElement() + throws Exception + { + // SETUP + String fileName = "testWriteDoubleBlockElement"; + File file = new File( rafDir, fileName + ".data" ); + BlockDisk disk = new BlockDisk( file ); + + // DO WORK + // byte arrays encur 27 bytes of serialization overhead. + int bytes = getBytesForBlocksOfByteArrays( disk.getBlockSizeBytes(), 2 ); + int[] blocks = disk.write( new byte[bytes] ); + + // VERIFY + System.out.println( "testWriteDoubleBlockElement " + disk ); + assertEquals( "Wrong number of blocks recorded.", 2, disk.getNumberOfBlocks() ); + assertEquals( "Wrong number of blocks returned.", 2, blocks.length ); + assertEquals( "Wrong block returned.", 0, blocks[0] ); + } + + /** + * Test writing and reading elements that do not fit within a single block. + * <p> + * @throws Exception + */ + public void testWriteAndReadMultipleMultiBlockElement() + throws Exception + { + // SETUP + String fileName = "testWriteAndReadSingleBlockElement"; + File file = new File( rafDir, fileName + ".data" ); + file.delete(); + BlockDisk disk = new BlockDisk( file ); + + // DO WORK + int numBlocksPerElement = 4; + int bytes = getBytesForBlocksOfByteArrays( disk.getBlockSizeBytes(), numBlocksPerElement ); + + int numElements = 100; + for ( int i = 0; i < numElements; i++ ) + { + int[] blocks = disk.write( new byte[bytes] ); + byte[] result = (byte[]) disk.read( blocks ); + + // VERIFY + assertEquals( "Wrong item retured.", new byte[bytes].length, result.length ); + assertEquals( "Wrong number of blocks returned.", numBlocksPerElement, blocks.length ); + } + System.out.println( "testWriteAndReadMultipleMultiBlockElement " + disk ); + } + + /** + * Test writing and reading elements that do not fit within a single block. + * <p> + * @throws Exception + */ + public void testWriteAndReadMultipleMultiBlockElement_setSize() + throws Exception + { + // SETUP + String fileName = "testWriteAndReadSingleBlockElement"; + File file = new File( rafDir, fileName + ".data" ); + file.delete(); + int blockSizeBytes = 1024; + BlockDisk disk = new BlockDisk( file, blockSizeBytes ); + + // DO WORK + int numBlocksPerElement = 4; + int bytes = getBytesForBlocksOfByteArrays( disk.getBlockSizeBytes(), numBlocksPerElement ); + + int numElements = 100; + for ( int i = 0; i < numElements; i++ ) + { + int[] blocks = disk.write( new byte[bytes] ); + byte[] result = (byte[]) disk.read( blocks ); + + // VERIFY + assertEquals( "Wrong item retured.", new byte[bytes].length, result.length ); + assertEquals( "Wrong number of blocks returned.", numBlocksPerElement, blocks.length ); + } + System.out.println( "testWriteAndReadMultipleMultiBlockElement_setSize " + disk ); + assertEquals( "Wrong number of elements.", numBlocksPerElement * numElements, disk.getNumberOfBlocks() ); + } + + /** + * Used to get the size for byte arrays that will take up the number of blocks specified. + * <p> + * @param blockSize + * @param numBlocks + * @return num bytes. + */ + private int getBytesForBlocksOfByteArrays( int blockSize, int numBlocks ) + { + // byte arrays encur some bytes of serialization overhead. + return blockSize * numBlocks - ( numBlocks * BlockDisk.HEADER_SIZE_BYTES ) - ( numBlocks * 14 ); + } +} Added: jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/HugeQuantityBlockDiskCacheLoadTest.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/HugeQuantityBlockDiskCacheLoadTest.java?rev=437311&view=auto ============================================================================== --- jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/HugeQuantityBlockDiskCacheLoadTest.java (added) +++ jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/block/HugeQuantityBlockDiskCacheLoadTest.java Sat Aug 26 23:45:57 2006 @@ -0,0 +1,99 @@ +package org.apache.jcs.auxiliary.disk.block; + +import junit.framework.TestCase; + +import org.apache.jcs.JCS; + +/** + * Put a few hundred thousand entries in the block disk cache. + * <p. + * @author Aaron Smuts + * + */ +public class HugeQuantityBlockDiskCacheLoadTest + extends TestCase +{ + + /** + * Test setup + */ + public void setUp() + { + JCS.setConfigFilename( "/TestBlockDiskCacheHuge.ccf" ); + } + + /** + * Adds items to cache, gets them, and removes them. The item count is more + * than the size of the memory cache, so items should spool to disk. + * + * @param region + * Name of the region to access + * + * @exception Exception + * If an error occurs + */ + public void testLargeNumberOfItems() + throws Exception + { + int items = 300000; + String region = "testCache1"; + + JCS jcs = JCS.getInstance( region ); + + try + { + + System.out.println( "Start: " + measureMemoryUse() ); + + // Add items to cache + + for ( int i = 0; i <= items; i++ ) + { + jcs.put( i + ":key", region + " data " + i ); + } + + System.out.println( jcs.getStats() ); + System.out.println( "--------------------------" ); + System.out.println( "After put: " + measureMemoryUse() ); + + Thread.sleep( 5000 ); + + System.out.println( jcs.getStats() ); + System.out.println( "--------------------------" ); + System.out.println( "After wait: " + measureMemoryUse() ); + + // Test that all items are in cache + + for ( int i = 0; i <= items; i++ ) + { + String value = (String) jcs.get( i + ":key" ); + + assertEquals( region + " data " + i, value ); + } + + System.out.println( "After get: " + measureMemoryUse() ); + } + finally + { + // dump the stats to the report + System.out.println( jcs.getStats() ); + System.out.println( "--------------------------" ); + System.out.println( "End: " + measureMemoryUse() ); + } + } + + /** + * Measure memory used by the VM. + * + * @return + * @throws InterruptedException + */ + protected long measureMemoryUse() + throws InterruptedException + { + System.gc(); + Thread.sleep( 3000 ); + System.gc(); + return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); + } +} Modified: 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=437311&r1=437310&r2=437311&view=diff ============================================================================== --- jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/indexed/DiskTestObjectUtil.java (original) +++ jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/indexed/DiskTestObjectUtil.java Sat Aug 26 23:45:57 2006 @@ -12,6 +12,7 @@ import java.io.IOException; import java.util.Random; +import org.apache.jcs.auxiliary.disk.DiskTestObject; import org.apache.jcs.engine.CacheElement; import org.apache.jcs.engine.behavior.ICacheElement; import org.apache.jcs.utils.serialization.StandardSerializer; Modified: jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/indexed/IndexedDiskCacheSteadyLoadTest.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/indexed/IndexedDiskCacheSteadyLoadTest.java?rev=437311&r1=437310&r2=437311&view=diff ============================================================================== --- jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/indexed/IndexedDiskCacheSteadyLoadTest.java (original) +++ jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/indexed/IndexedDiskCacheSteadyLoadTest.java Sat Aug 26 23:45:57 2006 @@ -6,6 +6,7 @@ import junit.framework.TestCase; import org.apache.jcs.JCS; +import org.apache.jcs.auxiliary.disk.DiskTestObject; import org.apache.jcs.utils.timing.ElapsedTimer; /** Added: jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/SingleLinkedListUnitTest.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/SingleLinkedListUnitTest.java?rev=437311&view=auto ============================================================================== --- jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/SingleLinkedListUnitTest.java (added) +++ jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/SingleLinkedListUnitTest.java Sat Aug 26 23:45:57 2006 @@ -0,0 +1,85 @@ +package org.apache.jcs.utils.struct; + +import junit.framework.TestCase; + +/** + * Tests for the simple linked list. + * <p> + * @author Aaron Smuts + */ +public class SingleLinkedListUnitTest + extends TestCase +{ + /** + * Verify that we get a null and that there are no exceptions. + */ + public void testTakeFromEmptyList() + { + // SETUP + SingleLinkedList list = new SingleLinkedList(); + + // DO WORK + Object result = list.takeFirst(); + + // VERIFY + assertNull( "Shounldn't have anything.", result ); + } + + /** + * Verify FIFO behavior. Verifies that all items are removed. + */ + public void testAddABunchAndTakeFromList() + { + // SETUP + SingleLinkedList list = new SingleLinkedList(); + + // DO WORK + int numToPut = 100; + for ( int i = 0; i < numToPut; i++ ) + { + list.addLast( new Integer( i ) ); + } + + // VERIFY + assertEquals( "Wrong nubmer in list.", numToPut, list.size() ); + + for ( int i = 0; i < numToPut; i++ ) + { + Object result = list.takeFirst(); + assertEquals( "Wrong value returned.", new Integer( i ), result ); + } + + // DO WORK + Object result = list.takeFirst(); + + // VERIFY + assertNull( "Shounldn't have anything left.", result ); + } + + /** + * Verify that after calling clear all items are removed adn the size is 0. + */ + public void testAddABunchAndClear() + { + // SETUP + SingleLinkedList list = new SingleLinkedList(); + + // DO WORK + int numToPut = 100; + for ( int i = 0; i < numToPut; i++ ) + { + list.addLast( new Integer( i ) ); + } + + // VERIFY + assertEquals( "Wrong nubmer in list.", numToPut, list.size() ); + + // DO WORK + list.clear(); + Object result = list.takeFirst(); + + // VERIFY + assertEquals( "Wrong nubmer in list.", 0, list.size() ); + assertNull( "Shounldn't have anything left.", result ); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
