Return-Path: Delivered-To: apmail-activemq-commits-archive@www.apache.org Received: (qmail 63187 invoked from network); 18 Jul 2008 15:51:03 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 18 Jul 2008 15:51:03 -0000 Received: (qmail 25842 invoked by uid 500); 18 Jul 2008 15:51:03 -0000 Delivered-To: apmail-activemq-commits-archive@activemq.apache.org Received: (qmail 25824 invoked by uid 500); 18 Jul 2008 15:51:03 -0000 Mailing-List: contact commits-help@activemq.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@activemq.apache.org Delivered-To: mailing list commits@activemq.apache.org Received: (qmail 25815 invoked by uid 99); 18 Jul 2008 15:51:03 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 18 Jul 2008 08:51:03 -0700 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 18 Jul 2008 15:50:16 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 8DA772388AA8; Fri, 18 Jul 2008 08:49:58 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r677944 [10/11] - in /activemq/sandbox/kahadb: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/kahadb/ src/main/java/org/apache/kahadb/impl/ src/main/java/org/apache/kahadb/impl/async/ ... Date: Fri, 18 Jul 2008 15:49:52 -0000 To: commits@activemq.apache.org From: chirino@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20080718154958.8DA772388AA8@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Added: activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/LoadTest.java URL: http://svn.apache.org/viewvc/activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/LoadTest.java?rev=677944&view=auto ============================================================================== --- activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/LoadTest.java (added) +++ activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/LoadTest.java Fri Jul 18 08:49:48 2008 @@ -0,0 +1,67 @@ +/** + * 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. + */ +package org.apache.kahadb; + +import java.io.IOException; +import java.util.concurrent.CountDownLatch; + +import junit.framework.TestCase; +import org.apache.kahadb.StoreFactory; +import org.apache.kahadb.impl.KahaStore; + +/** + * Store test + * + * @version $Revision: 1.2 $ + */ +public class LoadTest extends TestCase { + static final int COUNT = 10000; + static final int NUM_LOADERS = 5; + protected String name = "load.db"; + protected KahaStore store; + + /* + * Test method for 'org.apache.activemq.kaha.Store.close()' + */ + public void testLoad() throws Exception { + CountDownLatch start = new CountDownLatch(NUM_LOADERS); + CountDownLatch stop = new CountDownLatch(NUM_LOADERS); + for (int i = 0; i < NUM_LOADERS; i++) { + Loader loader = new Loader("loader:" + i, store, COUNT, start, stop); + loader.start(); + } + stop.await(); + } + + protected KahaStore getStore() throws IOException { + return (KahaStore)StoreFactory.open(name, "rw"); + } + + protected void setUp() throws Exception { + super.setUp(); + name = System.getProperty("basedir", ".") + "/target/activemq-data/load.db"; + StoreFactory.delete(name); + store = getStore(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + store.clear(); + store.close(); + assertTrue(StoreFactory.delete(name)); + } +} Added: activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/Loader.java URL: http://svn.apache.org/viewvc/activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/Loader.java?rev=677944&view=auto ============================================================================== --- activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/Loader.java (added) +++ activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/Loader.java Fri Jul 18 08:49:48 2008 @@ -0,0 +1,121 @@ +/** + * 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. + */ +package org.apache.kahadb; + +import java.util.Iterator; +import java.util.Set; +import java.util.concurrent.CountDownLatch; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.kahadb.BytesMarshaller; +import org.apache.kahadb.MapContainer; +import org.apache.kahadb.Marshaller; +import org.apache.kahadb.Store; +import org.apache.kahadb.StringMarshaller; + +/** + * Store test + * + * @version $Revision: 1.2 $ + */ +class Loader extends Thread { + + private static final Log LOG = LogFactory.getLog(Loader.class); + + private String name; + private Store store; + private int count; + private CountDownLatch start; + private CountDownLatch stop; + + public Loader(String name, Store store, int count, CountDownLatch start, CountDownLatch stop) { + this.name = name; + this.store = store; + this.count = count; + this.start = start; + this.stop = stop; + } + + public void run() { + try { + start.countDown(); + start.await(); + Marshaller keyMarshaller = new StringMarshaller(); + Marshaller valueMarshaller = new BytesMarshaller(); + MapContainer container = store.getMapContainer(name, store.getDefaultContainerName(), true); + + container.setKeyMarshaller(keyMarshaller); + container.setValueMarshaller(valueMarshaller); + container.load(); + // set data + Object value = getData(1024); + long startTime = System.currentTimeMillis(); + long startLoad = System.currentTimeMillis(); + for (int i = 0; i < count; i++) { + String key = "key:" + i; + container.put(key, value); + } + long finishLoad = System.currentTimeMillis(); + long totalLoadTime = finishLoad - startLoad; + LOG.info("name " + name + " load time = " + totalLoadTime + "(ms)"); + + Set keys = container.keySet(); + long startExtract = System.currentTimeMillis(); + + for (Iterator i = keys.iterator(); i.hasNext();) { + byte[] data = (byte[])container.get(i.next()); + } + long finishExtract = System.currentTimeMillis(); + long totalExtractTime = finishExtract - startExtract; + LOG.info("name " + name + " extract time = " + totalExtractTime + "(ms)"); + + long startRemove = System.currentTimeMillis(); + for (Iterator i = keys.iterator(); i.hasNext();) { + container.remove(i.next()); + } + long finishRemove = System.currentTimeMillis(); + long totalRemoveTime = finishRemove - startRemove; + LOG.info("name " + name + " remove time = " + totalRemoveTime + "(ms)"); + // re-insert data of longer length + startLoad = System.currentTimeMillis(); + value = getData(2048); + for (int i = 0; i < count; i++) { + // System.out.println(this + " Container size = " + + // container.size()); + String key = "key:" + i; + container.put(key, value); + } + finishLoad = System.currentTimeMillis(); + totalLoadTime = finishLoad - startLoad; + LOG.info("name " + name + " 2nd load time = " + totalLoadTime + "(ms)"); + + } catch (Exception e) { + e.printStackTrace(); + } finally { + stop.countDown(); + } + } + + byte[] getData(int size) { + byte[] result = new byte[size]; + for (int i = 0; i < size; i++) { + result[i] = 'a'; + } + return result; + } +} Added: activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/MapContainerTest.java URL: http://svn.apache.org/viewvc/activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/MapContainerTest.java?rev=677944&view=auto ============================================================================== --- activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/MapContainerTest.java (added) +++ activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/MapContainerTest.java Fri Jul 18 08:49:48 2008 @@ -0,0 +1,215 @@ +/** + * 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. + */ +package org.apache.kahadb; + +import java.io.IOException; +import java.util.Collection; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import org.apache.kahadb.MapContainer; +import org.apache.kahadb.Store; +import org.apache.kahadb.StoreFactory; + +import junit.framework.TestCase; + +public class MapContainerTest extends TestCase { + + protected static final int COUNT = 10; + + protected String name = "test"; + protected Store store; + protected MapContainer container; + protected Map testMap; + + public void testBasicAllocations() throws Exception { + String key = "key"; + Object value = testMap; + MapContainer test = store.getMapContainer("test", "test"); + test.put(key, value); + store.close(); + store = getStore(); + assertFalse(store.getMapContainerIds().isEmpty()); + test = store.getMapContainer("test", "test"); + assertEquals(value, test.get(key)); + + } + + /* + * Test method for 'org.apache.activemq.kaha.MapContainer.size()' + */ + public void testSize() throws Exception { + container.putAll(testMap); + assertTrue(container.size() == testMap.size()); + } + + /* + * Test method for 'org.apache.activemq.kaha.MapContainer.isEmpty()' + */ + public void testIsEmpty() throws Exception { + assertTrue(container.isEmpty()); + } + + /* + * Test method for 'org.apache.activemq.kaha.MapContainer.clear()' + */ + public void testClear() throws Exception { + container.putAll(testMap); + assertTrue(container.size() == testMap.size()); + container.clear(); + assertTrue(container.isEmpty()); + } + + /* + * Test method for + * 'org.apache.activemq.kaha.MapContainer.containsKey(Object)' + */ + public void testContainsKeyObject() throws Exception { + container.putAll(testMap); + for (Iterator i = testMap.entrySet().iterator(); i.hasNext();) { + Map.Entry entry = (Entry)i.next(); + assertTrue(container.containsKey(entry.getKey())); + } + } + + /* + * Test method for 'org.apache.activemq.kaha.MapContainer.get(Object)' + */ + public void testGetObject() throws Exception { + container.putAll(testMap); + for (Iterator i = testMap.entrySet().iterator(); i.hasNext();) { + Map.Entry entry = (Entry)i.next(); + Object value = container.get(entry.getKey()); + assertNotNull(value); + assertTrue(value.equals(entry.getValue())); + } + } + + /* + * Test method for + * 'org.apache.activemq.kaha.MapContainer.containsValue(Object)' + */ + public void testContainsValueObject() throws Exception { + container.putAll(testMap); + for (Iterator i = testMap.entrySet().iterator(); i.hasNext();) { + Map.Entry entry = (Entry)i.next(); + assertTrue(container.containsValue(entry.getValue())); + } + } + + /* + * Test method for 'org.apache.activemq.kaha.MapContainer.putAll(Map)' + */ + public void testPutAllMap() throws Exception { + container.putAll(testMap); + for (Iterator i = testMap.entrySet().iterator(); i.hasNext();) { + Map.Entry entry = (Entry)i.next(); + assertTrue(container.containsValue(entry.getValue())); + assertTrue(container.containsKey(entry.getKey())); + } + } + + /* + * Test method for 'org.apache.activemq.kaha.MapContainer.keySet()' + */ + public void testKeySet() throws Exception { + container.putAll(testMap); + Set keys = container.keySet(); + assertTrue(keys.size() == testMap.size()); + for (Iterator i = testMap.keySet().iterator(); i.hasNext();) { + Object key = i.next(); + assertTrue(keys.contains(key)); + keys.remove(key); + } + assertTrue(container.isEmpty()); + + } + + /* + * Test method for 'org.apache.activemq.kaha.MapContainer.values()' + */ + public void testValues() throws Exception { + container.putAll(testMap); + Collection values = container.values(); + assertTrue(values.size() == testMap.size()); + for (Iterator i = testMap.values().iterator(); i.hasNext();) { + Object value = i.next(); + assertTrue(values.contains(value)); + assertTrue(values.remove(value)); + } + assertTrue(container.isEmpty()); + } + + /* + * Test method for 'org.apache.activemq.kaha.MapContainer.entrySet()' + */ + public void testEntrySet() throws Exception { + container.putAll(testMap); + Set entries = container.entrySet(); + assertTrue(entries.size() == testMap.size()); + for (Iterator i = entries.iterator(); i.hasNext();) { + Map.Entry entry = (Entry)i.next(); + assertTrue(testMap.containsKey(entry.getKey())); + assertTrue(testMap.containsValue(entry.getValue())); + + } + + } + + /* + * Test method for 'org.apache.activemq.kaha.MapContainer.remove(Object)' + */ + public void testRemoveObject() throws Exception { + container.putAll(testMap); + for (Iterator i = testMap.keySet().iterator(); i.hasNext();) { + container.remove(i.next()); + } + assertTrue(container.isEmpty()); + } + + protected Store getStore() throws IOException { + return StoreFactory.open(name, "rw"); + } + + protected void setUp() throws Exception { + super.setUp(); + name = System.getProperty("basedir", ".") + "/target/activemq-data/map-container.db"; + store = getStore(); + container = store.getMapContainer("test", "test", true); + container.load(); + testMap = new HashMap(); + for (int i = 0; i < COUNT; i++) { + String key = "key:" + i; + String value = "value:" + i; + testMap.put(key, value); + } + + } + + protected void tearDown() throws Exception { + super.tearDown(); + if (store != null) { + store.close(); + store = null; + } + assertTrue(StoreFactory.delete(name)); + } + +} Added: activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/StoreTest.java URL: http://svn.apache.org/viewvc/activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/StoreTest.java?rev=677944&view=auto ============================================================================== --- activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/StoreTest.java (added) +++ activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/StoreTest.java Fri Jul 18 08:49:48 2008 @@ -0,0 +1,216 @@ +/** + * 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. + */ +package org.apache.kahadb; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import junit.framework.TestCase; +import org.apache.kahadb.ListContainer; +import org.apache.kahadb.MapContainer; +import org.apache.kahadb.Store; +import org.apache.kahadb.StoreFactory; +import org.apache.kahadb.impl.StoreLockedExcpetion; + +/** + * Store test + * + * @version $Revision: 1.2 $ + */ +public class StoreTest extends TestCase { + + protected String name; + protected Store store; + + /* + * Test method for 'org.apache.activemq.kaha.Store.close()' + */ + public void testClose() throws Exception { + store.close(); + try { + // access should throw an exception + store.getListContainer("fred"); + assertTrue("Should have got a enception", false); + } catch (Exception e) { + + } + } + + /* + * Test method for 'org.apache.activemq.kaha.Store.clear()' + */ + public void testClear() throws Exception { + int count = 100; + ListContainer list = store.getListContainer("testClear"); + list.load(); + for (int i = 0; i < count; i++) { + list.add("test " + i); + } + assertEquals(count, list.size()); + store.clear(); + assertTrue(list.isEmpty()); + } + + /* + * Test method for 'org.apache.activemq.kaha.Store.getMapContainer(Object)' + */ + public void testGetMapContainer() throws Exception { + String containerId = "test"; + MapContainer container = store.getMapContainer(containerId); + container.load(); + assertNotNull(container); + store.close(); + store = getStore(); + container = store.getMapContainer(containerId); + assertNotNull(container); + + } + + /* + * Test method for + * 'org.apache.activemq.kaha.Store.deleteMapContainer(Object)' + */ + public void testDeleteMapContainer() throws Exception { + String containerId = "test"; + MapContainer container = store.getMapContainer(containerId); + assertNotNull(container); + store.deleteMapContainer(containerId); + assertFalse(store.doesMapContainerExist(containerId)); + store.close(); + store = getStore(); + assertFalse(store.doesMapContainerExist(containerId)); + } + + /* + * Test method for 'org.apache.activemq.kaha.Store.getListContainer(Object)' + */ + public void testGetListContainer() throws Exception { + String containerId = "test"; + ListContainer container = store.getListContainer(containerId); + assertNotNull(container); + store.close(); + store = getStore(); + container = store.getListContainer(containerId); + assertNotNull(container); + } + + /* + * Test method for + * 'org.apache.activemq.kaha.Store.deleteListContainer(Object)' + */ + public void testDeleteListContainer() throws Exception { + String containerId = "test"; + ListContainer container = store.getListContainer(containerId); + assertNotNull(container); + store.deleteListContainer(containerId); + assertFalse(store.doesListContainerExist(containerId)); + store.close(); + store = getStore(); + assertFalse(store.doesListContainerExist(containerId)); + } + + public void testBasicAllocations() throws Exception { + Map testMap = new HashMap(); + int count = 1000; + for (int i = 0; i < count; i++) { + String key = "key:" + i; + String value = "value:" + i; + testMap.put(key, value); + } + List testList = new ArrayList(); + for (int i = 0; i < count; i++) { + testList.add("value:" + i); + } + String listId = "testList"; + String mapId1 = "testMap"; + String mapId2 = "testMap2"; + MapContainer mapContainer1 = store.getMapContainer(mapId1); + mapContainer1.load(); + mapContainer1.putAll(testMap); + + MapContainer mapContainer2 = store.getMapContainer(mapId2, mapId2); + mapContainer2.load(); + mapContainer2.putAll(testMap); + + ListContainer listContainer = store.getListContainer(listId); + listContainer.load(); + + listContainer.addAll(testList); + store.close(); + store = getStore(); + mapContainer1 = store.getMapContainer(mapId1); + mapContainer1.load(); + mapContainer2 = store.getMapContainer(mapId2, mapId2); + mapContainer2.load(); + listContainer = store.getListContainer(listId); + listContainer.load(); + for (Iterator i = testMap.keySet().iterator(); i.hasNext();) { + Object key = i.next(); + Object value = testMap.get(key); + assertTrue(mapContainer1.containsKey(key)); + assertEquals(value, mapContainer1.get(key)); + } + for (Iterator i = testMap.keySet().iterator(); i.hasNext();) { + Object key = i.next(); + Object value = testMap.get(key); + assertTrue(mapContainer2.containsKey(key)); + assertEquals(value, mapContainer2.get(key)); + } + assertEquals(testList.size(), listContainer.size()); + Iterator j = listContainer.iterator(); + for (Iterator i = testList.iterator(); i.hasNext();) { + assertEquals(i.next(), j.next()); + } + } + + public void testLock() throws Exception { + store.doesListContainerExist("fred"); + Store s = getStore(); + try { + s.doesListContainerExist("fred"); + } catch (StoreLockedExcpetion e) { + return; + } finally { + s.close(); + } + fail("Expected to catch an exception"); + } + + protected Store getStore() throws IOException { + return StoreFactory.open(name, "rw"); + } + + protected void setUp() throws Exception { + super.setUp(); + name = System.getProperty("basedir", ".") + "/target/activemq-data/store-test.db"; + store = getStore(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + if (store != null) { + store.close(); + store = null; + } + boolean rc = StoreFactory.delete(name); + assertTrue(rc); + } +} Added: activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/VolumeTest.java URL: http://svn.apache.org/viewvc/activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/VolumeTest.java?rev=677944&view=auto ============================================================================== --- activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/VolumeTest.java (added) +++ activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/VolumeTest.java Fri Jul 18 08:49:48 2008 @@ -0,0 +1,82 @@ +/** + * 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. + */ +package org.apache.kahadb; + +import java.io.IOException; +import java.util.Iterator; + +import junit.framework.TestCase; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.kahadb.ListContainer; +import org.apache.kahadb.Store; +import org.apache.kahadb.StoreFactory; + +public class VolumeTest extends TestCase { + + protected static final int NUMBER = 1; + private static final transient Log LOG = LogFactory.getLog(VolumeTest.class); + + protected Store store; + protected String name; + + /* + * dump a large number of messages into a list - then retreive them + */ + public void testListVolume() throws Exception { + ListContainer container = store.getListContainer("volume"); + container.setMarshaller(Store.BYTES_MARSHALLER); + byte[] data = new byte[10]; + for (int i = 0; i < NUMBER; i++) { + container.add(data); + if (i % 100000 == 0) { + LOG.error("persisted " + i); + } + + } + int count = 0; + + for (Iterator i = container.iterator(); i.hasNext();) { + assertNotNull(i.next()); + count++; + if (count % 100000 == 0) { + LOG.error("retrived " + count); + } + } + assertEquals("Different retrieved to stored", NUMBER, count); + } + + protected Store getStore() throws IOException { + return StoreFactory.open(name, "rw"); + } + + protected void setUp() throws Exception { + super.setUp(); + name = System.getProperty("basedir", ".") + "/target/activemq-data/volume-container.db"; + StoreFactory.delete(name); + store = StoreFactory.open(name, "rw"); + + } + + protected void tearDown() throws Exception { + super.tearDown(); + if (store != null) { + store.close(); + } + assertTrue(StoreFactory.delete(name)); + } +} Added: activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/impl/async/DataFileAppenderTest.java URL: http://svn.apache.org/viewvc/activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/impl/async/DataFileAppenderTest.java?rev=677944&view=auto ============================================================================== --- activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/impl/async/DataFileAppenderTest.java (added) +++ activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/impl/async/DataFileAppenderTest.java Fri Jul 18 08:49:48 2008 @@ -0,0 +1,135 @@ +/** + * 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. + */ +package org.apache.kahadb.impl.async; + +import java.io.File; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import junit.framework.TestCase; + +import org.apache.kahadb.impl.async.AsyncDataManager; +import org.apache.kahadb.impl.async.DataFileAppender; +import org.apache.kahadb.util.ByteSequence; + +public class DataFileAppenderTest extends TestCase { + AsyncDataManager dataManager; + File dir; + + @Override + public void setUp() throws Exception { + dir = new File("target/tests/DataFileAppenderTest"); + dir.mkdirs(); + dataManager = new AsyncDataManager(); + dataManager.setDirectory(dir); + configure(dataManager); + dataManager.start(); + } + + protected void configure(AsyncDataManager dataManager) { + dataManager.setUseNio(false); + } + + @Override + public void tearDown() throws Exception { + dataManager.close(); + deleteFilesInDirectory(dir); + dir.delete(); + } + + private void deleteFilesInDirectory(File directory) { + File[] files = directory.listFiles(); + for (int i=0; i= 0 + // as the Thread created in DataFileAppender.enqueue() may not have caught up. + assertTrue("queued data is written", latch.await(5, TimeUnit.SECONDS)); + } + + public void testBatchWriteCallbackCompleteAfterClose() throws Exception { + final int iterations = 10; + final CountDownLatch latch = new CountDownLatch(iterations); + ByteSequence data = new ByteSequence("DATA".getBytes()); + for (int i=0; i= list.size()) { + prev = list.getLast(); + if (prev==null) { + prev=root; + } + next = null; + } else { + prev = list.get(pos); + prev = prev != null ? prev : root; + next = list.getNextEntry(prev); + } + prev.setNextItem(item.getOffset()); + item.setPreviousItem(prev.getOffset()); + im.updateIndexes(prev); + if (next != null) { + next.setPreviousItem(item.getOffset()); + item.setNextItem(next.getOffset()); + im.updateIndexes(next); + } + im.storeIndex(item); + list.setRoot(root); + list.add(pos,item); + } + + protected void insertFirst(IndexLinkedList list,IndexItem item) throws IOException { + IndexItem root = list.getRoot(); + IndexItem prev = root; + IndexItem next = list.getNextEntry(prev); + prev.setNextItem(item.getOffset()); + item.setPreviousItem(prev.getOffset()); + im.updateIndexes(prev); + if (next != null) { + next.setPreviousItem(item.getOffset()); + item.setNextItem(next.getOffset()); + im.updateIndexes(next); + } + im.storeIndex(item); + list.addFirst(item); + } + + protected synchronized void remove(IndexLinkedList list,IndexItem item) throws IOException { + IndexItem root = list.getRoot(); + IndexItem prev = list.getPrevEntry(item); + IndexItem next = list.getNextEntry(item); + list.remove(item); + + prev = prev == null ? root : prev; + next = (next == null || !next.equals(root)) ? next : null; + + if (next != null) { + prev.setNextItem(next.getOffset()); + next.setPreviousItem(prev.getOffset()); + im.updateIndexes(next); + } else { + prev.setNextItem(Item.POSITION_NOT_SET); + } + im.updateIndexes(prev); + } +} Added: activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/impl/index/IndexBenchmark.java URL: http://svn.apache.org/viewvc/activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/impl/index/IndexBenchmark.java?rev=677944&view=auto ============================================================================== --- activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/impl/index/IndexBenchmark.java (added) +++ activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/impl/index/IndexBenchmark.java Fri Jul 18 08:49:48 2008 @@ -0,0 +1,236 @@ +/** + * 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. + */ +package org.apache.kahadb.impl.index; + +import java.io.File; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicLong; + +import junit.framework.TestCase; +import org.apache.kahadb.StoreEntry; +import org.apache.kahadb.impl.index.Index; +import org.apache.kahadb.impl.index.IndexItem; +import org.apache.kahadb.impl.index.IndexManager; +import org.apache.kahadb.util.IOHelper; + +/** + * @author chirino + */ +public abstract class IndexBenchmark extends TestCase { + + // Slower machines might need to make this bigger. + private static final long SAMPLE_DURATION = Integer.parseInt(System.getProperty("SAMPLES_DURATION", "" + 1000 * 5)); + // How many times do we sample? + private static final long SAMPLES = Integer.parseInt(System.getProperty("SAMPLES", "" + 60 * 1000 / SAMPLE_DURATION)); // Take + // enough + // samples + // to + // run + // for + // a + // minute. + + // How many indexes will we be benchmarking concurrently? + private static final int INDEX_COUNT = Integer.parseInt(System.getProperty("INDEX_COUNT", "" + 1)); + // Indexes tend to perform worse when they get big.. so how many items + // should we put into the index before we start sampling. + private static final int INDEX_PRE_LOAD_COUNT = Integer.parseInt(System.getProperty("INDEX_PRE_LOAD_COUNT", "" + 10000 / INDEX_COUNT)); + + protected File ROOT_DIR; + protected final HashMap indexes = new HashMap(); + protected IndexManager indexManager; + + public void setUp() throws Exception { + ROOT_DIR = new File(IOHelper.getDefaultDataDirectory()); + IOHelper.mkdirs(ROOT_DIR); + IOHelper.deleteChildren(ROOT_DIR); + indexManager = new IndexManager(ROOT_DIR, getClass().getName(), "rw", null, new AtomicLong()); + } + + protected void tearDown() throws Exception { + for (Index i : indexes.values()) { + try { + i.unload(); + } catch (Throwable ignore) { + } + } + indexManager.close(); + } + + abstract protected Index createIndex(File root, String name) throws Exception; + + synchronized private Index openIndex(String name) throws Exception { + Index index = indexes.get(name); + if (index == null) { + index = createIndex(ROOT_DIR, name); + index.load(); + indexes.put(name, index); + } + return index; + } + + class Producer extends Thread { + private final String name; + AtomicBoolean shutdown = new AtomicBoolean(); + + public Producer(String name) { + super("Producer: " + name); + this.name = name; + } + + public void shutdown() { + shutdown.set(true); + } + + @Override + public void run() { + try { + + IndexItem value = indexManager.createNewIndex(); + indexManager.storeIndex(value); + + Index index = openIndex(name); + long counter = 0; + while (!shutdown.get()) { + long c = counter; + + String key = "a-long-message-id-like-key-" + c; + index.store(key, value); + onProduced(counter++); + } + + } catch (Throwable e) { + e.printStackTrace(); + } + } + + public void onProduced(long counter) { + } + } + + class Consumer extends Thread { + private final String name; + AtomicBoolean shutdown = new AtomicBoolean(); + + public Consumer(String name) { + super("Consumer: " + name); + this.name = name; + } + + public void shutdown() { + shutdown.set(true); + } + + @Override + public void run() { + try { + Index index = openIndex(name); + long counter = 0; + while (!shutdown.get()) { + long c = counter; + String key = "a-long-message-id-like-key-" + c; + StoreEntry record; + record = index.get(key); + if (record != null) { + index.remove(key); + onConsumed(counter++); + } else { + Thread.sleep(0); + } + } + } catch (Throwable e) { + e.printStackTrace(); + } + } + + public void onConsumed(long counter) { + } + } + + public void testLoad() throws Exception { + + final Producer producers[] = new Producer[INDEX_COUNT]; + final Consumer consumers[] = new Consumer[INDEX_COUNT]; + final CountDownLatch preloadCountDown = new CountDownLatch(INDEX_COUNT); + final AtomicLong producedRecords = new AtomicLong(); + final AtomicLong consumedRecords = new AtomicLong(); + + System.out.println("Starting: " + INDEX_COUNT + " producers"); + for (int i = 0; i < INDEX_COUNT; i++) { + producers[i] = new Producer("test-" + i) { + private boolean prelaodDone; + + public void onProduced(long counter) { + if (!prelaodDone && counter >= INDEX_PRE_LOAD_COUNT) { + prelaodDone = true; + preloadCountDown.countDown(); + } + producedRecords.incrementAndGet(); + } + }; + producers[i].start(); + } + + long start = System.currentTimeMillis(); + System.out.println("Waiting for each producer create " + INDEX_PRE_LOAD_COUNT + " records before starting the consumers."); + preloadCountDown.await(); + long end = System.currentTimeMillis(); + System.out.println("Preloaded " + INDEX_PRE_LOAD_COUNT * INDEX_COUNT + " records at " + (INDEX_PRE_LOAD_COUNT * INDEX_COUNT * 1000f / (end - start)) + " records/sec"); + + System.out.println("Starting: " + INDEX_COUNT + " consumers"); + for (int i = 0; i < INDEX_COUNT; i++) { + consumers[i] = new Consumer("test-" + i) { + public void onConsumed(long counter) { + consumedRecords.incrementAndGet(); + } + }; + consumers[i].start(); + } + + long sample_start = System.currentTimeMillis(); + System.out.println("Taking " + SAMPLES + " performance samples every " + SAMPLE_DURATION + " ms"); + System.out.println("time (s), produced, produce rate (r/s), consumed, consume rate (r/s), used memory (k)"); + producedRecords.set(0); + consumedRecords.set(0); + for (int i = 0; i < SAMPLES; i++) { + start = System.currentTimeMillis(); + Thread.sleep(SAMPLE_DURATION); + end = System.currentTimeMillis(); + long p = producedRecords.getAndSet(0); + long c = consumedRecords.getAndSet(0); + + long usedMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); + + System.out.println(((end-sample_start)/1000f)+", "+p+", "+(p * 1000f / (end - start)) + ", "+ c+", " + (c * 1000f / (end - start))+", "+(usedMemory/(1024)) ); + } + System.out.println("Samples done... Shutting down the producers and consumers..."); + for (int i = 0; i < INDEX_COUNT; i++) { + producers[i].shutdown(); + consumers[i].shutdown(); + } + for (int i = 0; i < INDEX_COUNT; i++) { + producers[i].join(1000 * 5); + consumers[i].join(1000 * 5); + } + System.out.println("Shutdown."); + } + +} Propchange: activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/impl/index/IndexBenchmark.java ------------------------------------------------------------------------------ svn:executable = * Added: activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/impl/index/VMIndexLinkedListTest.java URL: http://svn.apache.org/viewvc/activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/impl/index/VMIndexLinkedListTest.java?rev=677944&view=auto ============================================================================== --- activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/impl/index/VMIndexLinkedListTest.java (added) +++ activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/impl/index/VMIndexLinkedListTest.java Fri Jul 18 08:49:48 2008 @@ -0,0 +1,319 @@ +/** + * 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. + */ +package org.apache.kahadb.impl.index; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import junit.framework.TestCase; +import org.apache.kahadb.StoreEntry; +import org.apache.kahadb.impl.data.Item; +import org.apache.kahadb.impl.index.IndexItem; +import org.apache.kahadb.impl.index.IndexLinkedList; +import org.apache.kahadb.impl.index.VMIndexLinkedList; + +/** + * @version $Revision: 1.2 $ + */ +public class VMIndexLinkedListTest extends TestCase { + static final int NUMBER = 30; + private IndexItem root; + private List testData = new ArrayList(); + private IndexLinkedList list; + + protected void setUp() throws Exception { + super.setUp(); + + IndexItem item = new IndexItem(); + list = createList(item); + this.root = list.getRoot(); + + for (int i = 0; i < NUMBER; i++) { + item = createIndex(list,i); + testData.add(item); + } + } + + protected void tearDown() throws Exception { + super.tearDown(); + testData.clear(); + list = null; + } + + IndexItem createIndex(IndexLinkedList list,int offset) throws IOException { + IndexItem result = new IndexItem(); + result.setOffset(offset); + return result; + } + protected IndexLinkedList createList(IndexItem root) throws IOException { + return new VMIndexLinkedList(root); + } + + protected void addToList(IndexLinkedList list,IndexItem item) throws IOException { + list.add(item); + } + + protected void insertToList(IndexLinkedList list,int pos,IndexItem item) throws IOException { + list.add(pos, item); + } + + protected void insertFirst(IndexLinkedList list,IndexItem item) throws IOException { + list.addFirst(item); + } + + protected synchronized void remove(IndexLinkedList list,IndexItem item) throws IOException { + IndexItem root = list.getRoot(); + IndexItem prev = list.getPrevEntry(item); + IndexItem next = list.getNextEntry(item); + list.remove(item); + } + + + /* + * test method for + * 'org.apache.activemq.kaha.impl.VMIndexLinkedList.getFirst()' + */ + public void testGetFirst() throws IOException { + for (int i = 0; i < testData.size(); i++) { + addToList(list,testData.get(i)); + } + assertNotNull(list.getFirst()); + assertTrue(list.getFirst().equals(testData.get(0))); + } + + /* + * test method for + * 'org.apache.activemq.kaha.impl.VMIndexLinkedList.getLast()' + */ + public void testGetLast() throws IOException { + for (int i = 0; i < testData.size(); i++) { + addToList(list,testData.get(i)); + } + assertTrue(list.getLast() == testData.get(testData.size() - 1)); + } + + /* + * test method for + * 'org.apache.activemq.kaha.impl.VMIndexLinkedList.removeFirst()' + */ + public void testRemoveFirst() throws IOException { + for (int i = 0; i < testData.size(); i++) { + addToList(list,testData.get(i)); + } + assertTrue(list.removeFirst().equals(testData.get(0))); + } + + /* + * test method for + * 'org.apache.activemq.kaha.impl.VMIndexLinkedList.removeLast()' + */ + public void testRemoveLast() throws IOException { + for (int i = 0; i < testData.size(); i++) { + addToList(list,testData.get(i)); + } + assertTrue(list.removeLast().equals(testData.get(testData.size() - 1))); + } + + /* + * test method for + * 'org.apache.activemq.kaha.impl.VMIndexLinkedList.addFirst(IndexItem)' + */ + public void testAddFirst() throws IOException { + for (int i = 0; i < testData.size(); i++) { + insertFirst(list, testData.get(i)); + } + int count = 0; + for (int i = testData.size() - 1; i >= 0; i--) { + assertTrue(testData.get(i).equals(list.get(count++))); + } + } + + /* + * test method for + * 'org.apache.activemq.kaha.impl.VMIndexLinkedList.addLast(IndexItem)' + */ + public void testAddLast() throws IOException { + for (int i = 0; i < testData.size(); i++) { + addToList(list,testData.get(i)); + } + for (int i = 0; i < testData.size(); i++) { + assertTrue(testData.get(i).equals(list.get(i))); + } + } + + /* + * test method for 'org.apache.activemq.kaha.impl.VMIndexLinkedList.size()' + */ + public void testSize() throws IOException { + for (int i = 0; i < testData.size(); i++) { + addToList(list,testData.get(i)); + assertTrue(list.size() == i + 1); + } + } + + /* + * test method for + * 'org.apache.activemq.kaha.impl.VMIndexLinkedList.isEmpty()' + */ + public void testIsEmpty() throws IOException { + for (int i = 0; i < testData.size(); i++) { + addToList(list,testData.get(i)); + assertTrue(list.size() == i + 1); + } + list.clear(); + assertTrue(list.isEmpty()); + } + + /* + * test method for + * 'org.apache.activemq.kaha.impl.VMIndexLinkedList.add(IndexItem)' + */ + public void testAddIndexItem() throws IOException { + for (int i = 0; i < testData.size(); i++) { + addToList(list,testData.get(i)); + } + for (int i = 0; i < testData.size(); i++) { + assertTrue(testData.get(i).equals(list.get(i))); + } + } + + /* + * test method for 'org.apache.activemq.kaha.impl.VMIndexLinkedList.clear()' + */ + public void testClear() throws IOException { + for (int i = 0; i < testData.size(); i++) { + addToList(list,testData.get(i)); + assertTrue(list.size() == i + 1); + } + list.clear(); + assertTrue(list.isEmpty()); + } + + /* + * test method for 'org.apache.activemq.kaha.impl.VMIndexLinkedList.add(int, + * IndexItem)' + */ + public void testAddIntIndexItem() throws IOException { + for (int i = 0; i < this.testData.size(); i++) { + insertToList(list, i, testData.get(i)); + } + for (int i = 0; i < testData.size(); i++) { + assertTrue(testData.get(i).equals(list.get(i))); + } + } + + /* + * test method for + * 'org.apache.activemq.kaha.impl.VMIndexLinkedList.remove(int)' + */ + public void testRemoveInt() throws IOException { + for (int i = 0; i < testData.size(); i++) { + insertToList(list, i, testData.get(i)); + } + for (int i = 0; i < testData.size(); i++) { + list.remove(0); + } + assertTrue(list.isEmpty()); + for (int i = 0; i < testData.size(); i++) { + insertToList(list, i, testData.get(i)); + } + for (int i = 0; i < testData.size(); i++) { + list.remove(list.size() - 1); + } + assertTrue(list.isEmpty()); + } + + /* + * test method for + * 'org.apache.activemq.kaha.impl.VMIndexLinkedList.indexOf(IndexItem)' + */ + public void testIndexOf() throws IOException { + for (int i = 0; i < testData.size(); i++) { + addToList(list,testData.get(i)); + } + for (int i = 0; i < testData.size(); i++) { + assertTrue(list.indexOf(testData.get(i)) == i); + } + } + + /* + * test method for + * 'org.apache.activemq.kaha.impl.VMIndexLinkedList.getNextEntry(IndexItem)' + */ + public void testGetNextEntry() throws IOException { + for (int i = 0; i < testData.size(); i++) { + addToList(list,testData.get(i)); + } + IndexItem next = list.getFirst(); + int count = 0; + while (next != null) { + assertTrue(next.equals(testData.get(count++))); + next = list.getNextEntry(next); + assertTrue(next == null || !next.equals(root)); + } + } + + /* + * test method for + * 'org.apache.activemq.kaha.impl.VMIndexLinkedList.getPrevEntry(IndexItem)' + */ + public void testGetPrevEntry() throws IOException { + for (int i = 0; i < testData.size(); i++) { + addToList(list,testData.get(i)); + } + IndexItem next = list.getLast(); + int count = testData.size() - 1; + while (next != null) { + assertTrue(next.equals(testData.get(count--))); + next = list.getPrevEntry(next); + assertTrue(next == null || !next.equals(root)); + } + } + + /* + * test method for + * 'org.apache.activemq.kaha.impl.VMIndexLinkedList.remove(IndexItem)' + */ + public void testRemoveIndexItem() throws IOException { + for (int i = 0; i < testData.size(); i++) { + addToList(list,testData.get(i)); + } + for (int i = 0; i < testData.size(); i++) { + list.remove(testData.get(i)); + assertTrue(list.size() == testData.size() - i - 1); + } + } + + public void testAddRemove() throws IOException { + IndexItem a = createIndex(list,0); + addToList(list, a); + IndexItem b = createIndex(list,1); + addToList(list, b); + IndexItem c = createIndex(list,2); + addToList(list, c); + IndexItem d = createIndex(list,3); + addToList(list, d); + remove(list, d); + assertTrue(list.getLast().equals(c)); + assertTrue(list.getNextEntry(b).equals(c)); + remove(list, b); + assertTrue(list.getNextEntry(a).equals(c)); + assertTrue(list.getLast().equals(c)); + + } +} Added: activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/impl/index/hash/HashIndexBenchMark.java URL: http://svn.apache.org/viewvc/activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/impl/index/hash/HashIndexBenchMark.java?rev=677944&view=auto ============================================================================== --- activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/impl/index/hash/HashIndexBenchMark.java (added) +++ activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/impl/index/hash/HashIndexBenchMark.java Fri Jul 18 08:49:48 2008 @@ -0,0 +1,37 @@ +/** + * 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. + */ +package org.apache.kahadb.impl.index.hash; + +import java.io.File; + +import org.apache.kahadb.Store; +import org.apache.kahadb.impl.index.Index; +import org.apache.kahadb.impl.index.IndexBenchmark; +import org.apache.kahadb.impl.index.hash.HashIndex; + +public class HashIndexBenchMark extends IndexBenchmark { + + @Override + protected Index createIndex(File root, String name) throws Exception { + HashIndex index = new HashIndex(root, name, indexManager); + //index.setNumberOfBins(12); + //index.setPageSize(32 * 1024); + index.setKeyMarshaller(Store.STRING_MARSHALLER); + return index; + } + +} Added: activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/impl/index/hash/HashTest.java URL: http://svn.apache.org/viewvc/activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/impl/index/hash/HashTest.java?rev=677944&view=auto ============================================================================== --- activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/impl/index/hash/HashTest.java (added) +++ activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/impl/index/hash/HashTest.java Fri Jul 18 08:49:48 2008 @@ -0,0 +1,154 @@ +/** + * 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. + */ +package org.apache.kahadb.impl.index.hash; + +import java.io.File; +import java.io.IOException; +import java.util.concurrent.atomic.AtomicLong; +import junit.framework.TestCase; +import org.apache.kahadb.Store; +import org.apache.kahadb.impl.index.IndexItem; +import org.apache.kahadb.impl.index.IndexManager; +import org.apache.kahadb.impl.index.hash.HashIndex; +import org.apache.kahadb.util.IOHelper; + +/** + * Test a HashIndex + */ +public class HashTest extends TestCase { + + private static final int COUNT = 10000; + + private HashIndex hashIndex; + + private File directory; + + private IndexManager indexManager; + + /** + * @throws java.lang.Exception + * @see junit.framework.TestCase#setUp() + */ + protected void setUp() throws Exception { + super.setUp(); + directory = new File(IOHelper.getDefaultDataDirectory()); + IOHelper.mkdirs(directory); + IOHelper.deleteChildren(directory); + indexManager = new IndexManager(directory, "im-hash-test", "rw", null, + new AtomicLong()); + this.hashIndex = new HashIndex(directory, "testHash", indexManager); + this.hashIndex.setNumberOfBins(12); + this.hashIndex.setPageSize(32 * 1024); + this.hashIndex.setKeyMarshaller(Store.STRING_MARSHALLER); + } + + public void testHashIndex() throws Exception { + doTest(300); + hashIndex.clear(); + hashIndex.unload(); + doTest(600); + hashIndex.clear(); + hashIndex.unload(); + doTest(128); + } + + public void doTest(int pageSize) throws Exception { + String keyRoot = "key:"; + hashIndex.setPageSize(pageSize); + this.hashIndex.load(); + doInsert(keyRoot); + this.hashIndex.unload(); + this.hashIndex.load(); + checkRetrieve(keyRoot); + doRemove(keyRoot); + this.hashIndex.unload(); + this.hashIndex.load(); + doInsert(keyRoot); + doRemoveHalf(keyRoot); + doInsertHalf(keyRoot); + this.hashIndex.unload(); + this.hashIndex.load(); + checkRetrieve(keyRoot); + this.hashIndex.unload(); + } + + void doInsert(String keyRoot) throws Exception { + for (int i = 0; i < COUNT; i++) { + IndexItem value = indexManager.createNewIndex(); + indexManager.storeIndex(value); + hashIndex.store(keyRoot + i, value); + } + } + + void checkRetrieve(String keyRoot) throws IOException { + for (int i = 0; i < COUNT; i++) { + IndexItem item = (IndexItem) hashIndex.get(keyRoot + i); + assertNotNull(item); + } + } + + void doRemoveHalf(String keyRoot) throws Exception { + for (int i = 0; i < COUNT; i++) { + if (i % 2 == 0) { + hashIndex.remove(keyRoot + i); + } + + } + } + + void doInsertHalf(String keyRoot) throws Exception { + for (int i = 0; i < COUNT; i++) { + if (i % 2 == 0) { + IndexItem value = indexManager.createNewIndex(); + indexManager.storeIndex(value); + hashIndex.store(keyRoot + i, value); + } + } + } + + void doRemove(String keyRoot) throws Exception { + for (int i = 0; i < COUNT; i++) { + hashIndex.remove(keyRoot + i); + } + for (int i = 0; i < COUNT; i++) { + IndexItem item = (IndexItem) hashIndex.get(keyRoot + i); + assertNull(item); + } + } + + void doRemoveBackwards(String keyRoot) throws Exception { + for (int i = COUNT - 1; i >= 0; i--) { + hashIndex.remove(keyRoot + i); + } + for (int i = 0; i < COUNT; i++) { + IndexItem item = (IndexItem) hashIndex.get(keyRoot + i); + assertNull(item); + } + } + + /** + * @throws java.lang.Exception + * @see junit.framework.TestCase#tearDown() + */ + protected void tearDown() throws Exception { + super.tearDown(); + File[] files = directory.listFiles(); + for (File file : files) { + file.delete(); + } + } +} Added: activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/impl/index/tree/TreeTest.java URL: http://svn.apache.org/viewvc/activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/impl/index/tree/TreeTest.java?rev=677944&view=auto ============================================================================== --- activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/impl/index/tree/TreeTest.java (added) +++ activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/impl/index/tree/TreeTest.java Fri Jul 18 08:49:48 2008 @@ -0,0 +1,137 @@ +/** + * 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. + */ +package org.apache.kahadb.impl.index.tree; + +import java.io.File; +import java.io.IOException; +import java.util.concurrent.atomic.AtomicLong; + +import junit.framework.TestCase; +import org.apache.kahadb.Store; +import org.apache.kahadb.impl.index.IndexItem; +import org.apache.kahadb.impl.index.IndexManager; +import org.apache.kahadb.impl.index.tree.TreeIndex; + +/** + * Test a TreeIndex + */ +public class TreeTest extends TestCase { + + private static final int COUNT = 55; + private TreeIndex tree; + private File directory; + private IndexManager indexManager; + private boolean dumpTree; + + /** + * @throws java.lang.Exception + * @see junit.framework.TestCase#setUp() + */ + protected void setUp() throws Exception { + super.setUp(); + directory = new File("activemq-data"); + directory.mkdirs(); + indexManager = new IndexManager(directory, "im-test", "rw", null,new AtomicLong()); + this.tree = new TreeIndex(directory, "testTree", indexManager); + this.tree.setKeyMarshaller(Store.STRING_MARSHALLER); + } + + public void testTreeWithCaching() throws Exception { + this.tree.setEnablePageCaching(true); + // doTest(); + } + + public void testTreeWithoutCaching() throws Exception { + this.tree.setEnablePageCaching(false); + // doTest(); + } + + public void doTest() throws Exception { + // doTest(300); + // tree.clear(); + // tree.unload(); + // count = 55 - this fails + doTest(600); + // tree.clear(); + // tree.unload(); + // doTest(1024*16); + } + + public void doTest(int pageSize) throws Exception { + String keyRoot = "key:"; + tree.setPageSize(pageSize); + this.tree.load(); + // doInsert(keyRoot); + // checkRetrieve(keyRoot); + // doRemove(keyRoot); + doInsert(keyRoot); + doRemoveBackwards(keyRoot); + } + + void doInsert(String keyRoot) throws Exception { + for (int i = 0; i < COUNT; i++) { + IndexItem value = indexManager.createNewIndex(); + indexManager.storeIndex(value); + tree.store(keyRoot + i, value); + } + } + + void checkRetrieve(String keyRoot) throws IOException { + for (int i = 0; i < COUNT; i++) { + IndexItem item = (IndexItem)tree.get(keyRoot + i); + assertNotNull(item); + } + } + + void doRemove(String keyRoot) throws Exception { + for (int i = 0; i < COUNT; i++) { + tree.remove(keyRoot + i); + // System.out.println("Removed " + keyRoot+i); + // tree.getRoot().dump(); + // System.out.println(""); + } + for (int i = 0; i < COUNT; i++) { + IndexItem item = (IndexItem)tree.get(keyRoot + i); + assertNull(item); + } + } + + void doRemoveBackwards(String keyRoot) throws Exception { + for (int i = COUNT - 1; i >= 0; i--) { + tree.remove(keyRoot + i); + System.out.println("BACK Removed " + keyRoot + i); + tree.getRoot().dump(); + System.out.println(""); + } + for (int i = 0; i < COUNT; i++) { + IndexItem item = (IndexItem)tree.get(keyRoot + i); + assertNull(item); + } + } + + /** + * @throws java.lang.Exception + * @see junit.framework.TestCase#tearDown() + */ + protected void tearDown() throws Exception { + super.tearDown(); + File[] files = directory.listFiles(); + for (File file : files) { + file.delete(); + } + } +} Added: activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/xindice/IndexBenchmark.java URL: http://svn.apache.org/viewvc/activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/xindice/IndexBenchmark.java?rev=677944&view=auto ============================================================================== --- activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/xindice/IndexBenchmark.java (added) +++ activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/xindice/IndexBenchmark.java Fri Jul 18 08:49:48 2008 @@ -0,0 +1,226 @@ +/** + * 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. + */ +package org.apache.kahadb.xindice; + +import java.io.File; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicLong; + +import junit.framework.TestCase; +import org.apache.kahadb.xindice.Index; +import org.apache.kahadb.xindice.IndexException; +import org.apache.kahadb.xindice.Key; +import org.apache.kahadb.xindice.Record; +import org.apache.kahadb.xindice.Value; + +/** + * @author chirino + */ +public abstract class IndexBenchmark extends TestCase { + + // Slower machines might need to make this bigger. + private static final long SAMPLE_DURATION = Integer.parseInt(System.getProperty("SAMPLES_DURATION", "" + 1000 * 5)); + // How many times do we sample? + private static final long SAMPLES = Integer.parseInt(System.getProperty("SAMPLES", "" + 60*1000/SAMPLE_DURATION )); // Take enough samples to run for a minute. + + private static final int FILER_COUNT = Integer.parseInt(System.getProperty("FILER_COUNT", "" + 1)); + private static final int FILER_PRE_LOAD_COUNT = Integer.parseInt(System.getProperty("FILER_PRE_LOAD_COUNT", "" + 10000 / FILER_COUNT)); + + protected File ROOT_DIR; + protected final HashMap indexes = new HashMap(); + + public void setUp() throws Exception { + ROOT_DIR = new File("target/test-data/" + getClass().getName()); + ROOT_DIR.mkdirs(); + } + + protected void tearDown() throws Exception { + for (Index f : indexes.values()) { + try { + f.close(); + } catch (Throwable ignore) { + } + } + } + + abstract protected Index createIndex(File root, String name) throws Exception; + + synchronized private Index openIndex(String name) throws Exception, IndexException { + Index index = indexes.get(name); + if( index == null ) { + index = createIndex(ROOT_DIR, name); + if (!index.exists()) { + index.create(); + } + index.open(); + indexes.put(name, index); + } + return index; + } + + class Producer extends Thread { + private final String name; + AtomicBoolean shutdown = new AtomicBoolean(); + + public Producer(String name) { + super("Producer: " + name); + this.name = name; + } + + public void shutdown() { + shutdown.set(true); + } + + @Override + public void run() { + try { + Index filer = openIndex(name); + long counter=0; + Value value = new Value(new byte[]{1,2,3,4,5,6,7,8}); + while( !shutdown.get() ) { + long c = counter; + Key key = new Key("a-long-message-id-like-key-"+c); +// synchronized(filer) { + filer.writeRecord(key, value); +// } + onProduced(counter++); + } + + } catch (Throwable e) { + e.printStackTrace(); + } + } + + public void onProduced(long counter) { + } + } + + class Consumer extends Thread { + private final String name; + AtomicBoolean shutdown = new AtomicBoolean(); + + public Consumer(String name) { + super("Consumer: " + name); + this.name = name; + } + + public void shutdown() { + shutdown.set(true); + } + + @Override + public void run() { + try { + Index filer = openIndex(name); + long counter=0; + while( !shutdown.get() ) { + long c = counter; + Key key = new Key("a-long-message-id-like-key-"+c); + Record record; +// synchronized(filer) { + record = filer.readRecord(key); +// } + if( record !=null ) { +// synchronized(filer) { + filer.deleteRecord(key); +// } + onConsumed(counter++); + } else { + Thread.sleep(0); + } + } + } catch (Throwable e) { + e.printStackTrace(); + } + } + + public void onConsumed(long counter) { + } + } + + public void testLoad() throws Exception { + + final Producer producers[] = new Producer[FILER_COUNT]; + final Consumer consumers[] = new Consumer[FILER_COUNT]; + final CountDownLatch preloadCountDown = new CountDownLatch(FILER_COUNT); + final AtomicLong producedRecords = new AtomicLong(); + final AtomicLong consumedRecords = new AtomicLong(); + + System.out.println("Starting: "+FILER_COUNT+" producers"); + for (int i = 0; i < FILER_COUNT; i++) { + producers[i] = new Producer("test-" + i) { + private boolean prelaodDone; + public void onProduced(long counter) { + if( !prelaodDone && counter >= FILER_PRE_LOAD_COUNT) { + prelaodDone=true; + preloadCountDown.countDown(); + } + producedRecords.incrementAndGet(); + } + }; + producers[i].start(); + } + + long start = System.currentTimeMillis(); + System.out.println("Waiting for each producer create "+FILER_PRE_LOAD_COUNT+" records before starting the consumers."); + preloadCountDown.await(); + long end = System.currentTimeMillis(); + System.out.println("Preloaded " + FILER_PRE_LOAD_COUNT*FILER_COUNT + " records at " + (FILER_PRE_LOAD_COUNT*FILER_COUNT * 1000f / (end - start)) + " records/sec"); + + System.out.println("Starting: "+FILER_COUNT+" consumers"); + for (int i = 0; i < FILER_COUNT; i++) { + consumers[i] = new Consumer("test-" + i){ + public void onConsumed(long counter) { + consumedRecords.incrementAndGet(); + } + }; + consumers[i].start(); + } + + long sample_start = System.currentTimeMillis(); + System.out.println("Taking "+SAMPLES+" performance samples every "+SAMPLE_DURATION+" ms"); + System.out.println("time (s), produced, produce rate (r/s), consumed, consume rate (r/s), used memory (k)"); + producedRecords.set(0); + consumedRecords.set(0); + for (int i = 0; i < SAMPLES; i++) { + start = System.currentTimeMillis(); + Thread.sleep(SAMPLE_DURATION); + end = System.currentTimeMillis(); + long p = producedRecords.getAndSet(0); + long c = consumedRecords.getAndSet(0); + + long usedMemory = Runtime.getRuntime().totalMemory() -Runtime.getRuntime().freeMemory(); + + System.out.println(((end-sample_start)/1000f)+", "+p+", "+(p * 1000f / (end - start)) + ", "+ c+", " + (c * 1000f / (end - start))+", "+(usedMemory/(1024)) ); + } + System.out.println("Samples done... Shutting down the producers and consumers..."); + for (int i = 0; i < FILER_COUNT; i++) { + producers[i].shutdown(); + consumers[i].shutdown(); + } + for (int i = 0; i < FILER_COUNT; i++) { + producers[i].join(1000 * 5); + consumers[i].join(1000 * 5); + } + System.out.println("Shutdown."); + } + +} Propchange: activemq/sandbox/kahadb/src/test/java/org/apache/kahadb/xindice/IndexBenchmark.java ------------------------------------------------------------------------------ svn:executable = *