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 tv...@apache.org on 2007/05/10 18:04:27 UTC
svn commit: r536904 [34/38] - in /jakarta/jcs/trunk: ./
auxiliary-builds/javagroups/
auxiliary-builds/javagroups/src/java/org/apache/jcs/auxiliary/javagroups/
auxiliary-builds/javagroups/src/test/org/apache/jcs/auxiliary/javagroups/
auxiliary-builds/jd...
Modified: jakarta/jcs/trunk/src/test/org/apache/jcs/engine/memory/shrinking/ShrinkerThreadUnitTest.java
URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/engine/memory/shrinking/ShrinkerThreadUnitTest.java?view=diff&rev=536904&r1=536903&r2=536904
==============================================================================
--- jakarta/jcs/trunk/src/test/org/apache/jcs/engine/memory/shrinking/ShrinkerThreadUnitTest.java (original)
+++ jakarta/jcs/trunk/src/test/org/apache/jcs/engine/memory/shrinking/ShrinkerThreadUnitTest.java Thu May 10 09:03:42 2007
@@ -1,188 +1,191 @@
-package org.apache.jcs.engine.memory.shrinking;
-
-/*
- * 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.framework.TestCase;
-
-import org.apache.jcs.engine.CacheElement;
-import org.apache.jcs.engine.CompositeCacheAttributes;
-import org.apache.jcs.engine.ElementAttributes;
-import org.apache.jcs.engine.behavior.ICacheElement;
-import org.apache.jcs.engine.control.event.ElementEventHandlerMockImpl;
-import org.apache.jcs.engine.memory.MemoryCacheMockImpl;
-
-/**
- * This tests the functionality of the shrinker thread.
- *
- * @author Aaron Smuts
- *
- */
-public class ShrinkerThreadUnitTest
- extends TestCase
-{
-
- /**
- * Setup cache attributes in mock. Create the shrinker with the mock. Add
- * some elements into the mock memory cache see that they get spooled.
- *
- * @throws Exception
- *
- */
- public void testSimpleShrink()
- throws Exception
- {
- MemoryCacheMockImpl memory = new MemoryCacheMockImpl();
-
- CompositeCacheAttributes cacheAttr = new CompositeCacheAttributes();
- cacheAttr.setMaxMemoryIdleTimeSeconds( 1 );
- cacheAttr.setMaxSpoolPerRun( 10 );
-
- memory.setCacheAttributes( cacheAttr );
-
- String key = "key";
- String value = "value";
-
- ICacheElement element = new CacheElement( "testRegion", key, value );
-
- ElementAttributes elementAttr = new ElementAttributes();
- elementAttr.setIsEternal( false );
- element.setElementAttributes( elementAttr );
- element.getElementAttributes().setMaxLifeSeconds( 1 );
- memory.update( element );
-
- ICacheElement returnedElement1 = memory.get( key );
- assertNotNull( "We should have received an element", returnedElement1 );
-
- // set this to 2 seconds ago.
- elementAttr.lastAccessTime = System.currentTimeMillis() - 2000;
-
- ShrinkerThread shrinker = new ShrinkerThread( memory );
- Thread runner = new Thread( shrinker );
- runner.run();
-
- Thread.sleep( 500 );
-
- ICacheElement returnedElement2 = memory.get( key );
- assertTrue( "Waterfall should have been called.", memory.waterfallCallCount > 0 );
- assertNull( "We not should have received an element. It should have been spooled.", returnedElement2 );
- }
-
- /**
- * Add 10 to the memory cache. Set the spool per run limit to 3.
- *
- * @throws Exception
- */
- public void testSimpleShrinkMutiple()
- throws Exception
- {
- MemoryCacheMockImpl memory = new MemoryCacheMockImpl();
-
- CompositeCacheAttributes cacheAttr = new CompositeCacheAttributes();
- cacheAttr.setMaxMemoryIdleTimeSeconds( 1 );
- cacheAttr.setMaxSpoolPerRun( 3 );
-
- memory.setCacheAttributes( cacheAttr );
-
- for ( int i = 0; i < 10; i++ )
- {
- String key = "key" + i;
- String value = "value";
-
- ICacheElement element = new CacheElement( "testRegion", key, value );
-
- ElementAttributes elementAttr = new ElementAttributes();
- elementAttr.setIsEternal( false );
- element.setElementAttributes( elementAttr );
- element.getElementAttributes().setMaxLifeSeconds( 1 );
- memory.update( element );
-
- ICacheElement returnedElement1 = memory.get( key );
- assertNotNull( "We should have received an element", returnedElement1 );
-
- // set this to 2 seconds ago.
- elementAttr.lastAccessTime = System.currentTimeMillis() - 2000;
- }
-
- ShrinkerThread shrinker = new ShrinkerThread( memory );
- Thread runner = new Thread( shrinker );
- runner.run();
-
- Thread.sleep( 500 );
-
- assertEquals( "Waterfall called the wrong number of times.", 3, memory.waterfallCallCount );
-
- assertEquals( "Wrong number of elements remain.", 7, memory.getSize() );
- }
-
- /**
- * Add a mock event handler to the items. Verify that it gets called.
- * <p>
- * This is only testing the spooled background event
- *
- * @throws Exception
- */
- public void testSimpleShrinkMutipleWithEventHandler()
- throws Exception
-{
- MemoryCacheMockImpl memory = new MemoryCacheMockImpl();
-
- CompositeCacheAttributes cacheAttr = new CompositeCacheAttributes();
- cacheAttr.setMaxMemoryIdleTimeSeconds( 1 );
- cacheAttr.setMaxSpoolPerRun( 3 );
-
- memory.setCacheAttributes( cacheAttr );
-
- ElementEventHandlerMockImpl handler = new ElementEventHandlerMockImpl();
-
- for ( int i = 0; i < 10; i++ )
- {
- String key = "key" + i;
- String value = "value";
-
- ICacheElement element = new CacheElement( "testRegion", key, value );
-
- ElementAttributes elementAttr = new ElementAttributes();
- elementAttr.addElementEventHandler( handler );
- elementAttr.setIsEternal( false );
- element.setElementAttributes( elementAttr );
- element.getElementAttributes().setMaxLifeSeconds( 1 );
- memory.update( element );
-
- ICacheElement returnedElement1 = memory.get( key );
- assertNotNull( "We should have received an element", returnedElement1 );
-
- // set this to 2 seconds ago.
- elementAttr.lastAccessTime = System.currentTimeMillis() - 2000;
- }
-
- ShrinkerThread shrinker = new ShrinkerThread( memory );
- Thread runner = new Thread( shrinker );
- runner.run();
-
- Thread.sleep( 500 );
-
- assertEquals( "Waterfall called the wrong number of times.", 3, memory.waterfallCallCount );
-
- // the shrinker delegates the the composite cache on the memory cache to put the
- // event on the queue. This make it hard to test. TODO we need to change this to make it easier to verify.
- //assertEquals( "Event handler ExceededIdleTimeBackground called the wrong number of times.", 3, handler.getExceededIdleTimeBackgroundCount() );
-
- assertEquals( "Wrong number of elements remain.", 7, memory.getSize() );
-}
-
-
-}
+package org.apache.jcs.engine.memory.shrinking;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.framework.TestCase;
+
+import org.apache.jcs.engine.CacheElement;
+import org.apache.jcs.engine.CompositeCacheAttributes;
+import org.apache.jcs.engine.ElementAttributes;
+import org.apache.jcs.engine.behavior.ICacheElement;
+import org.apache.jcs.engine.control.event.ElementEventHandlerMockImpl;
+import org.apache.jcs.engine.memory.MemoryCacheMockImpl;
+
+/**
+ * This tests the functionality of the shrinker thread.
+ *
+ * @author Aaron Smuts
+ *
+ */
+public class ShrinkerThreadUnitTest
+ extends TestCase
+{
+
+ /**
+ * Setup cache attributes in mock. Create the shrinker with the mock. Add
+ * some elements into the mock memory cache see that they get spooled.
+ *
+ * @throws Exception
+ *
+ */
+ public void testSimpleShrink()
+ throws Exception
+ {
+ MemoryCacheMockImpl memory = new MemoryCacheMockImpl();
+
+ CompositeCacheAttributes cacheAttr = new CompositeCacheAttributes();
+ cacheAttr.setMaxMemoryIdleTimeSeconds( 1 );
+ cacheAttr.setMaxSpoolPerRun( 10 );
+
+ memory.setCacheAttributes( cacheAttr );
+
+ String key = "key";
+ String value = "value";
+
+ ICacheElement element = new CacheElement( "testRegion", key, value );
+
+ ElementAttributes elementAttr = new ElementAttributes();
+ elementAttr.setIsEternal( false );
+ element.setElementAttributes( elementAttr );
+ element.getElementAttributes().setMaxLifeSeconds( 1 );
+ memory.update( element );
+
+ ICacheElement returnedElement1 = memory.get( key );
+ assertNotNull( "We should have received an element", returnedElement1 );
+
+ // set this to 2 seconds ago.
+ elementAttr.lastAccessTime = System.currentTimeMillis() - 2000;
+
+ ShrinkerThread shrinker = new ShrinkerThread( memory );
+ Thread runner = new Thread( shrinker );
+ runner.run();
+
+ Thread.sleep( 500 );
+
+ ICacheElement returnedElement2 = memory.get( key );
+ assertTrue( "Waterfall should have been called.", memory.waterfallCallCount > 0 );
+ assertNull( "We not should have received an element. It should have been spooled.", returnedElement2 );
+ }
+
+ /**
+ * Add 10 to the memory cache. Set the spool per run limit to 3.
+ *
+ * @throws Exception
+ */
+ public void testSimpleShrinkMutiple()
+ throws Exception
+ {
+ MemoryCacheMockImpl memory = new MemoryCacheMockImpl();
+
+ CompositeCacheAttributes cacheAttr = new CompositeCacheAttributes();
+ cacheAttr.setMaxMemoryIdleTimeSeconds( 1 );
+ cacheAttr.setMaxSpoolPerRun( 3 );
+
+ memory.setCacheAttributes( cacheAttr );
+
+ for ( int i = 0; i < 10; i++ )
+ {
+ String key = "key" + i;
+ String value = "value";
+
+ ICacheElement element = new CacheElement( "testRegion", key, value );
+
+ ElementAttributes elementAttr = new ElementAttributes();
+ elementAttr.setIsEternal( false );
+ element.setElementAttributes( elementAttr );
+ element.getElementAttributes().setMaxLifeSeconds( 1 );
+ memory.update( element );
+
+ ICacheElement returnedElement1 = memory.get( key );
+ assertNotNull( "We should have received an element", returnedElement1 );
+
+ // set this to 2 seconds ago.
+ elementAttr.lastAccessTime = System.currentTimeMillis() - 2000;
+ }
+
+ ShrinkerThread shrinker = new ShrinkerThread( memory );
+ Thread runner = new Thread( shrinker );
+ runner.run();
+
+ Thread.sleep( 500 );
+
+ assertEquals( "Waterfall called the wrong number of times.", 3, memory.waterfallCallCount );
+
+ assertEquals( "Wrong number of elements remain.", 7, memory.getSize() );
+ }
+
+ /**
+ * Add a mock event handler to the items. Verify that it gets called.
+ * <p>
+ * This is only testing the spooled background event
+ *
+ * @throws Exception
+ */
+ public void testSimpleShrinkMutipleWithEventHandler()
+ throws Exception
+{
+ MemoryCacheMockImpl memory = new MemoryCacheMockImpl();
+
+ CompositeCacheAttributes cacheAttr = new CompositeCacheAttributes();
+ cacheAttr.setMaxMemoryIdleTimeSeconds( 1 );
+ cacheAttr.setMaxSpoolPerRun( 3 );
+
+ memory.setCacheAttributes( cacheAttr );
+
+ ElementEventHandlerMockImpl handler = new ElementEventHandlerMockImpl();
+
+ for ( int i = 0; i < 10; i++ )
+ {
+ String key = "key" + i;
+ String value = "value";
+
+ ICacheElement element = new CacheElement( "testRegion", key, value );
+
+ ElementAttributes elementAttr = new ElementAttributes();
+ elementAttr.addElementEventHandler( handler );
+ elementAttr.setIsEternal( false );
+ element.setElementAttributes( elementAttr );
+ element.getElementAttributes().setMaxLifeSeconds( 1 );
+ memory.update( element );
+
+ ICacheElement returnedElement1 = memory.get( key );
+ assertNotNull( "We should have received an element", returnedElement1 );
+
+ // set this to 2 seconds ago.
+ elementAttr.lastAccessTime = System.currentTimeMillis() - 2000;
+ }
+
+ ShrinkerThread shrinker = new ShrinkerThread( memory );
+ Thread runner = new Thread( shrinker );
+ runner.run();
+
+ Thread.sleep( 500 );
+
+ assertEquals( "Waterfall called the wrong number of times.", 3, memory.waterfallCallCount );
+
+ // the shrinker delegates the the composite cache on the memory cache to put the
+ // event on the queue. This make it hard to test. TODO we need to change this to make it easier to verify.
+ //assertEquals( "Event handler ExceededIdleTimeBackground called the wrong number of times.", 3, handler.getExceededIdleTimeBackgroundCount() );
+
+ assertEquals( "Wrong number of elements remain.", 7, memory.getSize() );
+}
+
+
+}
Modified: jakarta/jcs/trunk/src/test/org/apache/jcs/servlet/ListTestCacheServlet.java
URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/servlet/ListTestCacheServlet.java?view=diff&rev=536904&r1=536903&r2=536904
==============================================================================
--- jakarta/jcs/trunk/src/test/org/apache/jcs/servlet/ListTestCacheServlet.java (original)
+++ jakarta/jcs/trunk/src/test/org/apache/jcs/servlet/ListTestCacheServlet.java Thu May 10 09:03:42 2007
@@ -1,26 +1,29 @@
package org.apache.jcs.servlet;
/*
- * Copyright 2001-2004 The Apache Software Foundation.
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
*
- * 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
*
- * 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.
+ * 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 javax.servlet.http.HttpServlet;
/**
* Description of the Class
- *
+ *
*/
public class ListTestCacheServlet
extends HttpServlet
Modified: jakarta/jcs/trunk/src/test/org/apache/jcs/utils/access/JCSWorkerUnitTest.java
URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/utils/access/JCSWorkerUnitTest.java?view=diff&rev=536904&r1=536903&r2=536904
==============================================================================
--- jakarta/jcs/trunk/src/test/org/apache/jcs/utils/access/JCSWorkerUnitTest.java (original)
+++ jakarta/jcs/trunk/src/test/org/apache/jcs/utils/access/JCSWorkerUnitTest.java Thu May 10 09:03:42 2007
@@ -1,51 +1,70 @@
-package org.apache.jcs.utils.access;
-
-import junit.framework.TestCase;
-
-/**
- * Test cases for the JCS worker.
- *
- * @author Aaron Smuts
- *
- */
-public class JCSWorkerUnitTest
- extends TestCase
-{
-
- /**
- * Test basic worker funtionality. This is a serial not a concurrent test.
- * <p>
- * Just verify that the worker will go to the cache before asking the helper.
- *
- * @throws Exception
- *
- */
- public void testSimpleGet()
- throws Exception
- {
- JCSWorker cachingWorker = new JCSWorker( "example region" );
-
- // This is the helper.
- JCSWorkerHelper helper = new AbstractJCSWorkerHelper()
- {
- int timesCalled = 0;
-
- public Object doWork()
- {
- Object results = new Long( ++timesCalled );
- return results;
- }
- };
-
- String key = "abc";
-
- Long result = (Long) cachingWorker.getResult( key, helper );
- assertEquals( "Called the wrong number of times", new Long( 1 ), result );
-
- // should get it fromthe cache.
- Long result2 = (Long) cachingWorker.getResult( key, helper );
- assertEquals( "Called the wrong number of times", new Long( 1 ), result2 );
-
- }
-
-}
+package org.apache.jcs.utils.access;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.framework.TestCase;
+
+/**
+ * Test cases for the JCS worker.
+ *
+ * @author Aaron Smuts
+ *
+ */
+public class JCSWorkerUnitTest
+ extends TestCase
+{
+
+ /**
+ * Test basic worker funtionality. This is a serial not a concurrent test.
+ * <p>
+ * Just verify that the worker will go to the cache before asking the helper.
+ *
+ * @throws Exception
+ *
+ */
+ public void testSimpleGet()
+ throws Exception
+ {
+ JCSWorker cachingWorker = new JCSWorker( "example region" );
+
+ // This is the helper.
+ JCSWorkerHelper helper = new AbstractJCSWorkerHelper()
+ {
+ int timesCalled = 0;
+
+ public Object doWork()
+ {
+ Object results = new Long( ++timesCalled );
+ return results;
+ }
+ };
+
+ String key = "abc";
+
+ Long result = (Long) cachingWorker.getResult( key, helper );
+ assertEquals( "Called the wrong number of times", new Long( 1 ), result );
+
+ // should get it fromthe cache.
+ Long result2 = (Long) cachingWorker.getResult( key, helper );
+ assertEquals( "Called the wrong number of times", new Long( 1 ), result2 );
+
+ }
+
+}
Modified: jakarta/jcs/trunk/src/test/org/apache/jcs/utils/serialization/SerializationConversionUtilUnitTest.java
URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/utils/serialization/SerializationConversionUtilUnitTest.java?view=diff&rev=536904&r1=536903&r2=536904
==============================================================================
--- jakarta/jcs/trunk/src/test/org/apache/jcs/utils/serialization/SerializationConversionUtilUnitTest.java (original)
+++ jakarta/jcs/trunk/src/test/org/apache/jcs/utils/serialization/SerializationConversionUtilUnitTest.java Thu May 10 09:03:42 2007
@@ -1,91 +1,110 @@
-package org.apache.jcs.utils.serialization;
-
-import java.io.IOException;
-
-import junit.framework.TestCase;
-
-import org.apache.jcs.engine.CacheElement;
-import org.apache.jcs.engine.ElementAttributes;
-import org.apache.jcs.engine.behavior.ICacheElement;
-import org.apache.jcs.engine.behavior.ICacheElementSerialized;
-import org.apache.jcs.engine.behavior.IElementAttributes;
-import org.apache.jcs.engine.behavior.IElementSerializer;
-
-/**
- * Tests the serialization conversion util.
- *
- * @author Aaron Smuts
- *
- */
-public class SerializationConversionUtilUnitTest
- extends TestCase
-{
-
- /**
- * Verify that we can go back and forth with the simplest of objects.
- *
- * @throws Exception
- */
- public void testSimpleConversion()
- throws Exception
- {
- String cacheName = "testName";
- String key = "key";
- String value = "value fdsadf dsafdsa fdsaf dsafdsaf dsafdsaf dsaf dsaf dsaf dsafa dsaf dsaf dsafdsaf";
-
- IElementSerializer elementSerializer = new StandardSerializer();
-
- IElementAttributes attr = new ElementAttributes();
- attr.setMaxLifeSeconds( 34 );
-
- ICacheElement before = new CacheElement( cacheName, key, value );
- before.setElementAttributes( attr );
-
- ICacheElementSerialized serialized = SerializationConversionUtil.getSerializedCacheElement( before,
- elementSerializer );
- assertNotNull( "Should have a serialized object.", serialized );
- System.out.println( "testSimpleConversion, " + serialized );
-
- ICacheElement after = SerializationConversionUtil.getDeSerializedCacheElement( serialized, elementSerializer );
-
- assertNotNull( "Should have a deserialized object.", after );
- assertEquals( "Values should be the same.", before.getVal(), after.getVal() );
- assertEquals( "Attributes should be the same.", before.getElementAttributes().getMaxLifeSeconds(), after
- .getElementAttributes().getMaxLifeSeconds() );
- assertEquals( "Keys should be the same.", before.getKey(), after.getKey() );
- assertEquals( "Cache name should be the same.", before.getCacheName(), after.getCacheName() );
-
- }
-
- /**
- * Verify that we get an IOException for a null serializer.
- *
- * @throws Exception
- */
- public void testNullSerializerConversion()
- {
- String cacheName = "testName";
- String key = "key";
- String value = "value fdsadf dsafdsa fdsaf dsafdsaf dsafdsaf dsaf dsaf dsaf dsafa dsaf dsaf dsafdsaf";
-
- IElementSerializer elementSerializer = null;// new StandardSerializer();
-
- IElementAttributes attr = new ElementAttributes();
- attr.setMaxLifeSeconds( 34 );
-
- ICacheElement before = new CacheElement( cacheName, key, value );
- before.setElementAttributes( attr );
-
- try
- {
- SerializationConversionUtil.getSerializedCacheElement( before, elementSerializer );
- fail( "We should have received an IOException." );
- }
- catch ( IOException e )
- {
- // expected
- }
-
- }
-
-}
+package org.apache.jcs.utils.serialization;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 junit.framework.TestCase;
+
+import org.apache.jcs.engine.CacheElement;
+import org.apache.jcs.engine.ElementAttributes;
+import org.apache.jcs.engine.behavior.ICacheElement;
+import org.apache.jcs.engine.behavior.ICacheElementSerialized;
+import org.apache.jcs.engine.behavior.IElementAttributes;
+import org.apache.jcs.engine.behavior.IElementSerializer;
+
+/**
+ * Tests the serialization conversion util.
+ *
+ * @author Aaron Smuts
+ *
+ */
+public class SerializationConversionUtilUnitTest
+ extends TestCase
+{
+
+ /**
+ * Verify that we can go back and forth with the simplest of objects.
+ *
+ * @throws Exception
+ */
+ public void testSimpleConversion()
+ throws Exception
+ {
+ String cacheName = "testName";
+ String key = "key";
+ String value = "value fdsadf dsafdsa fdsaf dsafdsaf dsafdsaf dsaf dsaf dsaf dsafa dsaf dsaf dsafdsaf";
+
+ IElementSerializer elementSerializer = new StandardSerializer();
+
+ IElementAttributes attr = new ElementAttributes();
+ attr.setMaxLifeSeconds( 34 );
+
+ ICacheElement before = new CacheElement( cacheName, key, value );
+ before.setElementAttributes( attr );
+
+ ICacheElementSerialized serialized = SerializationConversionUtil.getSerializedCacheElement( before,
+ elementSerializer );
+ assertNotNull( "Should have a serialized object.", serialized );
+ System.out.println( "testSimpleConversion, " + serialized );
+
+ ICacheElement after = SerializationConversionUtil.getDeSerializedCacheElement( serialized, elementSerializer );
+
+ assertNotNull( "Should have a deserialized object.", after );
+ assertEquals( "Values should be the same.", before.getVal(), after.getVal() );
+ assertEquals( "Attributes should be the same.", before.getElementAttributes().getMaxLifeSeconds(), after
+ .getElementAttributes().getMaxLifeSeconds() );
+ assertEquals( "Keys should be the same.", before.getKey(), after.getKey() );
+ assertEquals( "Cache name should be the same.", before.getCacheName(), after.getCacheName() );
+
+ }
+
+ /**
+ * Verify that we get an IOException for a null serializer.
+ *
+ * @throws Exception
+ */
+ public void testNullSerializerConversion()
+ {
+ String cacheName = "testName";
+ String key = "key";
+ String value = "value fdsadf dsafdsa fdsaf dsafdsaf dsafdsaf dsaf dsaf dsaf dsafa dsaf dsaf dsafdsaf";
+
+ IElementSerializer elementSerializer = null;// new StandardSerializer();
+
+ IElementAttributes attr = new ElementAttributes();
+ attr.setMaxLifeSeconds( 34 );
+
+ ICacheElement before = new CacheElement( cacheName, key, value );
+ before.setElementAttributes( attr );
+
+ try
+ {
+ SerializationConversionUtil.getSerializedCacheElement( before, elementSerializer );
+ fail( "We should have received an IOException." );
+ }
+ catch ( IOException e )
+ {
+ // expected
+ }
+
+ }
+
+}
Modified: jakarta/jcs/trunk/src/test/org/apache/jcs/utils/serialization/StandardSerializerUnitTest.java
URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/utils/serialization/StandardSerializerUnitTest.java?view=diff&rev=536904&r1=536903&r2=536904
==============================================================================
--- jakarta/jcs/trunk/src/test/org/apache/jcs/utils/serialization/StandardSerializerUnitTest.java (original)
+++ jakarta/jcs/trunk/src/test/org/apache/jcs/utils/serialization/StandardSerializerUnitTest.java Thu May 10 09:03:42 2007
@@ -1,57 +1,76 @@
-package org.apache.jcs.utils.serialization;
-
-import junit.framework.TestCase;
-
-/**
- * Tests the standard serializer.
- *
- * @author Aaron Smuts
- *
- */
-public class StandardSerializerUnitTest
- extends TestCase
-{
-
- /**
- * Test simple back and forth with a string.
- *
- * @throws Exception
- *
- */
- public void testSimpleBackAndForth()
- throws Exception
- {
- StandardSerializer serializer = new StandardSerializer();
-
- String before = "adsfdsafdsafdsafdsafdsafdsafdsagfdsafdsafdsfdsafdsafsa333 31231";
-
- String after = (String) serializer.deSerialize( serializer.serialize( before ) );
-
- assertEquals( "Before and after should be the same.", before, after );
- }
-
- /**
- * Test serialization with a null object. Verify that we don't get an error.
- *
- * @throws Exception
- *
- */
- public void testNullInput()
- throws Exception
- {
- StandardSerializer serializer = new StandardSerializer();
-
- String before = null;
-
- byte[] serialized = serializer.serialize( before );
-
- System.out.println( "testNullInput " + serialized );
-
- String after = (String) serializer.deSerialize( serialized );
-
- System.out.println( "testNullInput " + after );
-
- assertNull( "Should have nothing.", after );
-
- }
-}
+package org.apache.jcs.utils.serialization;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.framework.TestCase;
+
+/**
+ * Tests the standard serializer.
+ *
+ * @author Aaron Smuts
+ *
+ */
+public class StandardSerializerUnitTest
+ extends TestCase
+{
+
+ /**
+ * Test simple back and forth with a string.
+ *
+ * @throws Exception
+ *
+ */
+ public void testSimpleBackAndForth()
+ throws Exception
+ {
+ StandardSerializer serializer = new StandardSerializer();
+
+ String before = "adsfdsafdsafdsafdsafdsafdsafdsagfdsafdsafdsfdsafdsafsa333 31231";
+
+ String after = (String) serializer.deSerialize( serializer.serialize( before ) );
+
+ assertEquals( "Before and after should be the same.", before, after );
+ }
+
+ /**
+ * Test serialization with a null object. Verify that we don't get an error.
+ *
+ * @throws Exception
+ *
+ */
+ public void testNullInput()
+ throws Exception
+ {
+ StandardSerializer serializer = new StandardSerializer();
+
+ String before = null;
+
+ byte[] serialized = serializer.serialize( before );
+
+ System.out.println( "testNullInput " + serialized );
+
+ String after = (String) serializer.deSerialize( serialized );
+
+ System.out.println( "testNullInput " + after );
+
+ assertNull( "Should have nothing.", after );
+
+ }
+}
Modified: jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/BoundedQueueUnitTest.java
URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/BoundedQueueUnitTest.java?view=diff&rev=536904&r1=536903&r2=536904
==============================================================================
--- jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/BoundedQueueUnitTest.java (original)
+++ jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/BoundedQueueUnitTest.java Thu May 10 09:03:42 2007
@@ -1,85 +1,93 @@
-package org.apache.jcs.utils.struct;
-
-/*
- * 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.framework.TestCase;
-
-/**
- * Unit tests for the bounded queue.
- * <p>
- * @author Aaron Smuts
- */
-public class BoundedQueueUnitTest
- extends TestCase
-{
- /**
- * Verify null returned for empty.
- */
- public void testTakeLastEmpty()
- {
- // SETUP
- int maxSize = 10;
- BoundedQueue queue = new BoundedQueue( maxSize );
-
- // DO WORK
- Object result = queue.take();
-
- // VERIFY
- assertNull( "Result should be null", result );
- }
-
- /**
- * Verify that the queue returns the number of elements and the it does not exceed the max.
- */
- public void testSize()
- {
- // SETUP
- int maxSize = 10;
- BoundedQueue queue = new BoundedQueue( maxSize );
-
- // DO WORK
- for ( int i = 0; i < maxSize * 2; i++ )
- {
- queue.add( "adfadsf sad " + i );
- }
-
- int result = queue.size();
-
- // VERIFY
- assertEquals( "Result size not as expected", maxSize, result );
- }
-
- /**
- * Verify that the items come back in the order put in.
- */
- public void testFIFOOrderedTake()
- {
- // SETUP
- int maxSize = 10;
- BoundedQueue queue = new BoundedQueue( maxSize );
-
- // DO WORK
- for ( int i = 0; i < maxSize; i++ )
- {
- queue.add( String.valueOf( i ) );
- }
-
-
- // VERIFY
-
- for ( int i = 0; i < maxSize; i++ )
- {
- String result = (String)queue.take();
- assertEquals( "Result not as expected", String.valueOf( i ) , result );
- }
- }
-}
+package org.apache.jcs.utils.struct;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.framework.TestCase;
+
+/**
+ * Unit tests for the bounded queue.
+ * <p>
+ * @author Aaron Smuts
+ */
+public class BoundedQueueUnitTest
+ extends TestCase
+{
+ /**
+ * Verify null returned for empty.
+ */
+ public void testTakeLastEmpty()
+ {
+ // SETUP
+ int maxSize = 10;
+ BoundedQueue queue = new BoundedQueue( maxSize );
+
+ // DO WORK
+ Object result = queue.take();
+
+ // VERIFY
+ assertNull( "Result should be null", result );
+ }
+
+ /**
+ * Verify that the queue returns the number of elements and the it does not exceed the max.
+ */
+ public void testSize()
+ {
+ // SETUP
+ int maxSize = 10;
+ BoundedQueue queue = new BoundedQueue( maxSize );
+
+ // DO WORK
+ for ( int i = 0; i < maxSize * 2; i++ )
+ {
+ queue.add( "adfadsf sad " + i );
+ }
+
+ int result = queue.size();
+
+ // VERIFY
+ assertEquals( "Result size not as expected", maxSize, result );
+ }
+
+ /**
+ * Verify that the items come back in the order put in.
+ */
+ public void testFIFOOrderedTake()
+ {
+ // SETUP
+ int maxSize = 10;
+ BoundedQueue queue = new BoundedQueue( maxSize );
+
+ // DO WORK
+ for ( int i = 0; i < maxSize; i++ )
+ {
+ queue.add( String.valueOf( i ) );
+ }
+
+
+ // VERIFY
+
+ for ( int i = 0; i < maxSize; i++ )
+ {
+ String result = (String)queue.take();
+ assertEquals( "Result not as expected", String.valueOf( i ) , result );
+ }
+ }
+}
Modified: jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/JCSvsCommonsLRUMapPerformanceTest.java
URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/JCSvsCommonsLRUMapPerformanceTest.java?view=diff&rev=536904&r1=536903&r2=536904
==============================================================================
--- jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/JCSvsCommonsLRUMapPerformanceTest.java (original)
+++ jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/JCSvsCommonsLRUMapPerformanceTest.java Thu May 10 09:03:42 2007
@@ -1,5 +1,24 @@
package org.apache.jcs.utils.struct;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.util.Map;
import junit.framework.Test;
@@ -14,9 +33,9 @@
/**
* 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 JCSvsCommonsLRUMapPerformanceTest
extends TestCase
@@ -42,7 +61,7 @@
/**
* A unit test suite for JUnit
- *
+ *
* @return The test suite
*/
public static Test suite()
@@ -52,7 +71,7 @@
/**
* A unit test for JUnit
- *
+ *
* @exception Exception
* Description of the Exception
*/
@@ -72,7 +91,7 @@
}
/**
- *
+ *
*/
public void doWork()
{
Modified: jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/LRUMapConcurrentTest.java
URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/LRUMapConcurrentTest.java?view=diff&rev=536904&r1=536903&r2=536904
==============================================================================
--- jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/LRUMapConcurrentTest.java (original)
+++ jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/LRUMapConcurrentTest.java Thu May 10 09:03:42 2007
@@ -1,277 +1,296 @@
-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 LRUMapConcurrentTest
- extends TestCase
-{
-
- private static int items = 20000;
-
- /**
- * Constructor for the TestSimpleLoad object
- *
- * @param testName
- * Description of the Parameter
- */
- public LRUMapConcurrentTest( String testName )
- {
- super( testName );
- }
-
- /**
- * Description of the Method
- *
- * @param args
- * Description of the Parameter
- */
- public static void main( String args[] )
- {
- String[] testCaseName = { LRUMapConcurrentTest.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( LRUMapConcurrentTest.class );
-
- // run concurrent tests
- final LRUMap map = new LRUMap( 2000 );
- suite.addTest( new LRUMapConcurrentTest( "conc1" )
- {
- public void runTest()
- throws Exception
- {
- this.runConcurrentPutGetTests( map, 2000 );
- }
- } );
- suite.addTest( new LRUMapConcurrentTest( "conc2" )
- {
- public void runTest()
- throws Exception
- {
- this.runConcurrentPutGetTests( map, 2000 );
- }
- } );
- suite.addTest( new LRUMapConcurrentTest( "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 LRUMapConcurrentTest( "concB1" )
- {
- public void runTest()
- throws Exception
- {
- this.runConcurrentRangeTests( map2, 10000, max2 );
- }
- } );
- suite.addTest( new LRUMapConcurrentTest( "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
+package org.apache.jcs.utils.struct;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.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 LRUMapConcurrentTest
+ extends TestCase
+{
+
+ private static int items = 20000;
+
+ /**
+ * Constructor for the TestSimpleLoad object
+ *
+ * @param testName
+ * Description of the Parameter
+ */
+ public LRUMapConcurrentTest( String testName )
+ {
+ super( testName );
+ }
+
+ /**
+ * Description of the Method
+ *
+ * @param args
+ * Description of the Parameter
+ */
+ public static void main( String args[] )
+ {
+ String[] testCaseName = { LRUMapConcurrentTest.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( LRUMapConcurrentTest.class );
+
+ // run concurrent tests
+ final LRUMap map = new LRUMap( 2000 );
+ suite.addTest( new LRUMapConcurrentTest( "conc1" )
+ {
+ public void runTest()
+ throws Exception
+ {
+ this.runConcurrentPutGetTests( map, 2000 );
+ }
+ } );
+ suite.addTest( new LRUMapConcurrentTest( "conc2" )
+ {
+ public void runTest()
+ throws Exception
+ {
+ this.runConcurrentPutGetTests( map, 2000 );
+ }
+ } );
+ suite.addTest( new LRUMapConcurrentTest( "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 LRUMapConcurrentTest( "concB1" )
+ {
+ public void runTest()
+ throws Exception
+ {
+ this.runConcurrentRangeTests( map2, 10000, max2 );
+ }
+ } );
+ suite.addTest( new LRUMapConcurrentTest( "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" ) );
+
+ }
+
+}
Modified: jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/LRUMapConcurrentUnitTest.java
URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/LRUMapConcurrentUnitTest.java?view=diff&rev=536904&r1=536903&r2=536904
==============================================================================
--- jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/LRUMapConcurrentUnitTest.java (original)
+++ jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/LRUMapConcurrentUnitTest.java Thu May 10 09:03:42 2007
@@ -1,263 +1,282 @@
-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
- * <p>
- * @author aaron smuts
- *
- */
-public class LRUMapConcurrentUnitTest
- extends TestCase
-{
- /** number to test with */
- private static int items = 20000;
-
- /**
- * Constructor for the TestSimpleLoad object
- * <p>
- * @param testName
- * Description of the Parameter
- */
- public LRUMapConcurrentUnitTest( String testName )
- {
- super( testName );
- }
-
- /**
- * A unit test suite for JUnit
- * <p>
- * @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.
- * <p>
- * @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.
- * <p>
- * @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
+package org.apache.jcs.utils.struct;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.util.Iterator;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.jcs.utils.struct.LRUMap;
+
+/**
+ * Tests the LRUMap
+ * <p>
+ * @author aaron smuts
+ *
+ */
+public class LRUMapConcurrentUnitTest
+ extends TestCase
+{
+ /** number to test with */
+ private static int items = 20000;
+
+ /**
+ * Constructor for the TestSimpleLoad object
+ * <p>
+ * @param testName
+ * Description of the Parameter
+ */
+ public LRUMapConcurrentUnitTest( String testName )
+ {
+ super( testName );
+ }
+
+ /**
+ * A unit test suite for JUnit
+ * <p>
+ * @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.
+ * <p>
+ * @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.
+ * <p>
+ * @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" ) );
+ }
+}
Modified: jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/LRUMapPerformanceTest.java
URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/LRUMapPerformanceTest.java?view=diff&rev=536904&r1=536903&r2=536904
==============================================================================
--- jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/LRUMapPerformanceTest.java (original)
+++ jakarta/jcs/trunk/src/test/org/apache/jcs/utils/struct/LRUMapPerformanceTest.java Thu May 10 09:03:42 2007
@@ -1,187 +1,206 @@
-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.JCSvsHashtablePerformanceTest;
-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.
- * <p>
- * @author aaronsm
- *
- */
-public class LRUMapPerformanceTest
- extends TestCase
-{
- /** The put put ration after the test */
- float ratioPut = 0;
-
- /** The ratio after the test */
- float ratioGet = 0;
-
- /** put jcs / commons ratio */
- float targetPut = 1.2f;
-
- /** get jcs / commons ratio */
- float targetGet = .5f;
-
- /** Time to loop */
- int loops = 20;
-
- /** items to put and get per loop */
- int tries = 50000;
-
- /**
- * @param testName
- */
- public LRUMapPerformanceTest( String testName )
- {
- super( testName );
- }
-
- /**
- * A unit test suite for JUnit
- * <p>
- * @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 < targetPut );
- assertTrue( this.ratioGet < targetGet );
- }
-
- /**
- *
- */
- 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 <" + targetPut
- + "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 <" + targetGet
- + "x" );
- }
-
- /**
- * @param args
- */
- public static void main( String args[] )
- {
- JCSvsHashtablePerformanceTest test = new JCSvsHashtablePerformanceTest( "command" );
- test.doWork();
- }
-
-}
+package org.apache.jcs.utils.struct;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.util.Map;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.jcs.JCSvsHashtablePerformanceTest;
+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.
+ * <p>
+ * @author aaronsm
+ *
+ */
+public class LRUMapPerformanceTest
+ extends TestCase
+{
+ /** The put put ration after the test */
+ float ratioPut = 0;
+
+ /** The ratio after the test */
+ float ratioGet = 0;
+
+ /** put jcs / commons ratio */
+ float targetPut = 1.2f;
+
+ /** get jcs / commons ratio */
+ float targetGet = .5f;
+
+ /** Time to loop */
+ int loops = 20;
+
+ /** items to put and get per loop */
+ int tries = 50000;
+
+ /**
+ * @param testName
+ */
+ public LRUMapPerformanceTest( String testName )
+ {
+ super( testName );
+ }
+
+ /**
+ * A unit test suite for JUnit
+ * <p>
+ * @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 < targetPut );
+ assertTrue( this.ratioGet < targetGet );
+ }
+
+ /**
+ *
+ */
+ 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 <" + targetPut
+ + "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 <" + targetGet
+ + "x" );
+ }
+
+ /**
+ * @param args
+ */
+ public static void main( String args[] )
+ {
+ JCSvsHashtablePerformanceTest test = new JCSvsHashtablePerformanceTest( "command" );
+ test.doWork();
+ }
+
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: jcs-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jcs-dev-help@jakarta.apache.org