1 19 20 package org.netbeans.mdr.test; 21 22 import java.util.*; 23 24 import junit.extensions.*; 25 import junit.framework.*; 26 27 import org.netbeans.mdr.persistence.*; 28 import org.netbeans.mdr.persistence.MOFID; 29 import org.netbeans.mdr.persistence.btreeimpl.btreeindex.Btree; 30 import org.netbeans.mdr.persistence.btreeimpl.btreeindex.TreeMetrics; 31 import org.netbeans.mdr.persistence.btreeimpl.btreestorage.*; 32 import org.netbeans.mdr.persistence.memoryimpl.*; 33 34 35 public class StorageTest extends MDRTestCase { 36 37 public static long RAND_VAL = 2123; 38 39 public static Random random; 40 41 public StorageTest(String testName) { 42 super (testName); 43 } 44 45 public static void main (String [] args) { 46 junit.textui.TestRunner.run (suite ()); 47 } 48 49 public static Test suite () { 50 TestSuite suite = new TestSuite (); 51 suite.addTestSuite (StorageTest.class); 52 53 TestSetup setup = new TestSetup (suite) { 54 public void setUp () { 55 } 56 public void tearDown () { 57 } 58 }; 59 return setup; 60 } 61 62 protected void setUp () { 63 } 64 65 67 public void test() { 68 StorageFactory factory; 69 Storage storage; 70 try { 71 System.out.println(); 73 System.out.println("*************************************************"); 74 System.out.println("btree storage, singlevalued index"); 75 System.out.println("-------------------------------------------------"); 76 factory = new BtreeFactory(); 77 storage = factory.createStorage(new HashMap()); 78 storage.create (true, new Resolver()); 79 random = new Random(RAND_VAL); 80 doSingleTest(storage, "btree"); 81 82 System.out.println(); 84 System.out.println("*************************************************"); 85 System.out.println("memory storage, singlevalued index"); 86 System.out.println("-------------------------------------------------"); 87 factory = new StorageFactoryImpl (); 88 storage = factory.createStorage(new HashMap()); 89 storage.create (true, new Resolver()); 90 random = new Random(RAND_VAL); 91 doSingleTest(storage, "memory"); 92 93 System.out.println(); 95 System.out.println("*************************************************"); 96 System.out.println("btree storage, multivalued test"); 97 System.out.println("-------------------------------------------------"); 98 factory = new BtreeFactory(); 99 storage = factory.createStorage(new HashMap()); 100 storage.create (true, new Resolver()); 101 random = new Random(RAND_VAL); 102 doMultiTest(storage, "btree"); 103 104 116 } catch (Exception e) { 117 e.printStackTrace (); 118 fail (e.getMessage ()); 119 } 120 } 121 122 public void doSingleTest(Storage storage, String info) throws StorageException { 123 final int KEYS_NUM = 10000; 124 final int VALUES_NUM = 2000; 125 final long OPS_NUM = 1000000; 126 127 Storage.EntryType entryType = Storage.EntryType.MOFID; 128 SinglevaluedIndex index = storage.createSinglevaluedIndex("singleIndex", entryType, entryType); 129 MOFID[] keys = new MOFID[KEYS_NUM]; 130 MOFID[] values = new MOFID[VALUES_NUM]; 131 for (int x = 0; x < KEYS_NUM; x++) { 132 keys[x] = generateMOFID(); 133 } 134 for (int x = 0; x < VALUES_NUM; x++) { 135 values[x] = generateMOFID(); 136 } 137 138 long time = System.currentTimeMillis(); 139 long totalTime = 0; 140 long insertions = 0; 141 long deletions = 0; 142 143 for (int x = 0; x < KEYS_NUM; x++) { 144 MOFID value = values[random.nextInt(VALUES_NUM)]; 145 index.put(keys[x], value); 146 } 147 148 totalTime += System.currentTimeMillis() - time; 149 System.out.println("initial insertions time: " + totalTime); 150 if (index instanceof Btree) { 151 TreeMetrics m = ((Btree) index).computeMetrics(); 152 m.print(System.out); 153 } 154 time = System.currentTimeMillis(); 155 156 for (long x = 0; x < OPS_NUM; x++) { 157 MOFID key = keys[random.nextInt(KEYS_NUM)]; 158 MOFID value = values[random.nextInt(VALUES_NUM)]; 159 Object val = index.getIfExists(key); 160 if (val == null) { 161 insertions++; 162 index.put(key, value); 163 } else { 164 deletions++; 165 index.remove(key); 166 } 167 } 168 totalTime += System.currentTimeMillis() - time; 169 if (index instanceof Btree) { 170 TreeMetrics m = ((Btree) index).computeMetrics(); 171 m.print(System.out); 172 } 173 time = System.currentTimeMillis(); 174 storage.close(); 175 176 totalTime += System.currentTimeMillis() - time; 177 System.out.println(); 178 System.out.println(info + ", test time: " + totalTime); 179 System.out.println("#insertions: " + insertions); 180 System.out.println("#deletions: " + deletions); 181 } 182 183 public void doMultiTest(Storage storage, String info) throws StorageException { 184 final int KEYS_NUM = 10000; 185 final int VALUES_NUM = 2000; 186 final long OPS_NUM = 1000000; 187 188 Storage.EntryType entryType = Storage.EntryType.MOFID; 189 MultivaluedIndex index = storage.createMultivaluedIndex("multiIndex", entryType, entryType, false); 190 MOFID[] keys = new MOFID[KEYS_NUM]; 191 MOFID[] values = new MOFID[VALUES_NUM]; 192 for (int x = 0; x < KEYS_NUM; x++) { 193 keys[x] = generateMOFID(); 194 } 195 for (int x = 0; x < VALUES_NUM; x++) { 196 values[x] = generateMOFID(); 197 } 198 199 long time = System.currentTimeMillis(); 200 long totalTime = 0; 201 long insertions = 0; 202 long deletions = 0; 203 204 for (long x = 0; x < OPS_NUM; x++) { 205 MOFID key = keys[random.nextInt(KEYS_NUM)]; 206 MOFID value = values[random.nextInt(VALUES_NUM)]; 207 List list = (List)index.getItems(key); 208 boolean flag = random.nextBoolean(); 209 int size = list.size(); 210 if (size == 0 || flag) { 211 list.add(value); 212 insertions++; 213 } else { 214 list.remove(random.nextInt(size)); 215 deletions++; 216 } 217 } 218 totalTime += System.currentTimeMillis() - time; 219 220 if (index instanceof Btree) { 221 TreeMetrics m = ((Btree) index).computeMetrics(); 222 m.print(System.out); 223 } 224 time = System.currentTimeMillis(); 225 storage.close(); 226 227 totalTime += System.currentTimeMillis() - time; 228 System.out.println(); 229 System.out.println(info + ", test time: " + totalTime); 230 System.out.println("#insertions: " + insertions); 231 System.out.println("#deletions: " + deletions); 232 } 233 234 316 317 public String generateString(int maxLength) { 318 return randomString("", 10, Math.max(10, maxLength)); 319 } 320 321 public MOFID generateMOFID() { 322 long serialNumber = Math.abs(random.nextLong()); 323 String storageId = randomString("", 16, 16); 324 return new MOFID(serialNumber, storageId); 325 } 326 327 public String randomString (String prefix) { 328 final int minLength = 10; 329 final int maxLength = 20; 330 return randomString (prefix, minLength, maxLength); 331 } 332 333 public String randomString (String prefix, int minLength, int maxLength) { 334 String res = ""; 335 int length = Math.max (minLength, random.nextInt (maxLength + 1)); 336 for (int x = prefix.length (); x <= length; x++) { 337 res = res + (char) (random.nextInt ('z' - 'a' + 1) + 'a'); 338 } 339 return prefix + res; 340 } 341 342 344 private class Resolver implements ObjectResolver { 345 346 public Object resolve(String storageID, Object key) { 347 System.out.println("resolve object called"); 348 return new Object (); 349 } 350 351 } 352 353 } 354 355 | Popular Tags |