1 19 package org.netbeans.mdr.test; 20 21 import java.io.IOException ; 22 import java.util.*; 23 import junit.extensions.*; 24 import junit.framework.*; 25 import org.netbeans.mdr.persistence.*; 26 import org.netbeans.mdr.persistence.MOFID; 27 import org.netbeans.mdr.persistence.btreeimpl.btreeindex.Btree; 28 import org.netbeans.mdr.persistence.btreeimpl.btreeindex.TreeMetrics; 29 import org.netbeans.mdr.persistence.btreeimpl.btreestorage.*; 30 import org.netbeans.mdr.persistence.memoryimpl.*; 31 32 33 public class StorageWriteTest extends MDRTestCase { 34 35 static final int ROWS = 50000; 36 static long RAND_VAL = 666; 38 static Random random = new Random(RAND_VAL); 39 40 private Set keys; 41 private Set values; 42 43 public StorageWriteTest(String testName) { 44 super(testName); 45 } 46 47 public static Test suite() { 48 TestSuite suite = new TestSuite(); 49 suite.addTestSuite(StorageWriteTest.class); 50 51 TestSetup setup = new TestSetup(suite) { 52 53 public void setUp() {} 54 55 public void tearDown() {} 56 }; 57 return setup; 58 } 59 60 protected void setUp() { 61 keys = new HashSet(ROWS); 63 values = new HashSet(ROWS); 64 65 long time = System.currentTimeMillis(); 66 for (int x = 0; x < ROWS; ) { 67 MOFID key = generateMOFID(); 68 if (keys.add(key)) 69 x++; 70 } 71 for (int x = 0; x < ROWS; ) { 72 MOFID value = generateMOFID(); 73 if (values.add(value)) 74 x++; 75 } 76 long generatorTime = System.currentTimeMillis() - time; 77 getLog().println("Keys and values pair generator takes " + generatorTime + " ms."); 78 } 79 80 public void testSequentialWrite() { 83 StorageFactory factory; 84 Storage storage; 85 for (int i = 0; i < 5; i++) { 86 try { 87 factory = new BtreeFactory(); 88 storage = factory.createStorage(new HashMap()); 89 storage.create (true, new Resolver()); 90 sequentialWrite(storage, "btree", "seqWrite" + i); 91 storage.close(); 92 } catch (Exception e) { 93 e.printStackTrace(); 94 fail(e.getMessage()); 95 } 96 } 97 } 98 99 public void testSequentialRemove() { 100 StorageFactory factory; 101 Storage storage; 102 for (int i = 0; i < 5; i++) { 103 try { 104 factory = new BtreeFactory(); 105 storage = factory.createStorage(new HashMap()); 106 storage.create (true, new Resolver()); 107 random = new Random(RAND_VAL); 108 sequentialWrite(storage, "btree", "seqRemove" + i); 109 sequentialRemove(storage, "btree", "seqRemove" + i); 110 storage.close(); 111 } catch (Exception e) { 112 e.printStackTrace(); 113 fail(e.getMessage()); 114 } 115 } 116 } 117 120 private void sequentialWrite(Storage storage, String info, String prefix) throws StorageException { 121 Storage.EntryType entryType = Storage.EntryType.MOFID; 123 SinglevaluedIndex index = storage.createSinglevaluedIndex(prefix + "singleIndex", entryType, entryType); 124 Iterator k = keys.iterator(); 126 Iterator v = values.iterator(); 127 long time = System.currentTimeMillis(); 128 for (int x = 0; x < ROWS; x++) { 129 index.put(k.next(), v.next()); 130 } 131 long insertionsTime = System.currentTimeMillis() - time; 132 getLog().println("Insertions time: " + insertionsTime); 133 if (index instanceof Btree) { 134 TreeMetrics m = ((Btree) index).computeMetrics(); 135 m.print(getLog()); 136 } 137 } 138 139 private void sequentialRemove(Storage storage, String info, String prefix) throws StorageException { 140 SinglevaluedIndex index = storage.getSinglevaluedIndex(prefix + "singleIndex"); 142 Iterator k = keys.iterator(); 144 long time = System.currentTimeMillis(); 145 for (int x = 0; x < ROWS; x++) { 146 index.remove(k.next()); 147 } 148 long deletionsTime = System.currentTimeMillis() - time; 149 getLog().println("Deletions time: " + deletionsTime); 150 if (index instanceof Btree) { 151 TreeMetrics m = ((Btree) index).computeMetrics(); 152 m.print(getLog()); 153 } 154 } 155 156 public static String generateString(int maxLength) { 157 return randomString("", 10, Math.max(10, maxLength)); 158 } 159 160 public static MOFID generateMOFID() { 161 long serialNumber = Math.abs(random.nextLong()); 162 String storageId = randomString("", 16, 16); 163 return new MOFID(serialNumber, storageId); 164 } 165 166 public static String randomString(String prefix) { 167 final int minLength = 10; 168 final int maxLength = 20; 169 return randomString (prefix, minLength, maxLength); 170 } 171 172 public static String randomString(String prefix, int minLength, int maxLength) { 173 String res = ""; 174 int length = Math.max (minLength, random.nextInt (maxLength + 1)); 175 for (int x = prefix.length (); x <= length; x++) { 176 res = res + (char) (random.nextInt ('z' - 'a' + 1) + 'a'); 177 } 178 return prefix + res; 179 } 180 181 public static void main(String [] args) { 182 junit.textui.TestRunner.run(suite()); 183 } 184 185 private class Resolver implements ObjectResolver { 188 public Object resolve(String storageID, Object key) { 189 getLog().println("resolve object called"); 190 return new Object (); 191 } 192 } 193 194 private static class PrimaryItem implements Streamable { 195 196 private byte[] data; 197 198 PrimaryItem() { 199 int length = StorageTest.random.nextInt(256); 200 data = new byte[length]; 201 for (int x = 0; x < length; x++) { 202 data[x] = (byte)StorageTest.random.nextInt(256); 203 } 204 } 205 206 public void read(java.io.InputStream is) throws StorageException { 207 try { 208 int length = is.read(); 209 data = new byte[length]; 210 for (int x = 0; x < length; x++) { 211 data[x] = (byte)is.read(); 212 } 213 } catch (IOException e) { 214 throw new StorageIOException(e); 215 } 216 } 217 218 public void write(java.io.OutputStream os) throws StorageException { 219 try { 220 os.write(data.length); 221 for (int x = 0; x < data.length; x++) { 222 os.write(data[x]); 223 } 224 } catch (IOException e) { 225 throw new StorageIOException(e); 226 } 227 } 228 229 } 230 } 231 | Popular Tags |