1.1 --- a/mdr/test/unit/src/org/netbeans/mdr/test/StorageTest.java Sat Nov 27 20:52:30 2004 +0000
1.2 +++ b/mdr/test/unit/src/org/netbeans/mdr/test/StorageTest.java Mon Nov 29 09:55:14 2004 +0000
1.3 @@ -13,6 +13,7 @@
1.4
1.5 package org.netbeans.mdr.test;
1.6
1.7 +import java.io.IOException;
1.8 import java.util.*;
1.9
1.10 import junit.extensions.*;
1.11 @@ -94,6 +95,28 @@
1.12 storage.create (true, new Resolver());
1.13 random = new Random(RAND_VAL);
1.14 doMultiTest(storage, "btree");
1.15 +
1.16 + // btree storage, several indexes
1.17 + getLog().println();
1.18 + getLog().println("*************************************************");
1.19 + getLog().println("btree storage, several indexes");
1.20 + getLog().println("-------------------------------------------------");
1.21 + factory = new BtreeFactory();
1.22 + storage = factory.createStorage(new HashMap());
1.23 + storage.create (true, new Resolver());
1.24 + random = new Random(RAND_VAL);
1.25 + doSeveralIndexesTest(storage, "btree");
1.26 +
1.27 + // btree storage, primary index
1.28 + getLog().println();
1.29 + getLog().println("*************************************************");
1.30 + getLog().println("btree storage, primary index");
1.31 + getLog().println("-------------------------------------------------");
1.32 + factory = new BtreeFactory();
1.33 + storage = factory.createStorage(new HashMap());
1.34 + storage.create (true, new Resolver());
1.35 + random = new Random(RAND_VAL);
1.36 + doPrimaryIndexTest(storage, "btree");
1.37
1.38 /*
1.39 // memory storage, multivalued index
1.40 @@ -143,7 +166,7 @@
1.41 getLog().println("initial insertions time: " + totalTime);
1.42 if (index instanceof Btree) {
1.43 TreeMetrics m = ((Btree) index).computeMetrics();
1.44 - m.print();
1.45 + m.print(getLog());
1.46 }
1.47 time = System.currentTimeMillis();
1.48
1.49 @@ -162,7 +185,7 @@
1.50 totalTime += System.currentTimeMillis() - time;
1.51 if (index instanceof Btree) {
1.52 TreeMetrics m = ((Btree) index).computeMetrics();
1.53 - m.print();
1.54 + m.print(getLog());
1.55 }
1.56 time = System.currentTimeMillis();
1.57 storage.close();
1.58 @@ -213,7 +236,7 @@
1.59
1.60 if (index instanceof Btree) {
1.61 TreeMetrics m = ((Btree) index).computeMetrics();
1.62 - m.print();
1.63 + m.print(getLog());
1.64 }
1.65 time = System.currentTimeMillis();
1.66 storage.close();
1.67 @@ -225,6 +248,130 @@
1.68 getLog().println("#deletions: " + deletions);
1.69 }
1.70
1.71 + public void doSeveralIndexesTest(Storage storage, String info) throws StorageException {
1.72 + final int KEYS_NUM = 10000;
1.73 + final int VALUES_NUM = 1000;
1.74 + final long OPS_NUM = 500000;
1.75 + final int INDEXES_NUM = 50;
1.76 +
1.77 + MOFID[] keys = new MOFID[KEYS_NUM];
1.78 + MOFID[] values = new MOFID[VALUES_NUM];
1.79 + for (int x = 0; x < KEYS_NUM; x++) {
1.80 + keys[x] = generateMOFID();
1.81 + }
1.82 + for (int x = 0; x < VALUES_NUM; x++) {
1.83 + values[x] = generateMOFID();
1.84 + }
1.85 +
1.86 + Index[] indexes = new Index[INDEXES_NUM];
1.87 + Storage.EntryType entryType = Storage.EntryType.MOFID;
1.88 + for (int x = 0; x < INDEXES_NUM; x++) {
1.89 + if (random.nextBoolean()) {
1.90 + // singlevalued index
1.91 + indexes[x] = storage.createSinglevaluedIndex("index" + x, entryType, entryType);
1.92 + } else {
1.93 + // multivalued index
1.94 + boolean unique = random.nextBoolean();
1.95 + boolean ordered = random.nextBoolean();
1.96 + if (ordered) {
1.97 + indexes[x] = storage.createMultivaluedOrderedIndex("index" + x, entryType, entryType, unique);
1.98 + } else {
1.99 + indexes[x] = storage.createMultivaluedIndex("index" + x, entryType, entryType, unique);
1.100 + }
1.101 + }
1.102 + } // for
1.103 +
1.104 + long time = System.currentTimeMillis();
1.105 + long totalTime = 0;
1.106 + long insertions = 0;
1.107 + long deletions = 0;
1.108 +
1.109 + for (long x = 0; x < OPS_NUM; x++) {
1.110 + MOFID key = keys[random.nextInt(KEYS_NUM)];
1.111 + MOFID value = values[random.nextInt(VALUES_NUM)];
1.112 + Index index = indexes[random.nextInt(INDEXES_NUM)];
1.113 + if (index instanceof MultivaluedIndex) {
1.114 + boolean remove = random.nextBoolean();
1.115 + MultivaluedIndex multi = (MultivaluedIndex)index;
1.116 + List list;
1.117 + if (multi instanceof MultivaluedOrderedIndex) {
1.118 + list = (List)multi.getItems(key);
1.119 + } else {
1.120 + list = (List)multi.getItems(key);
1.121 + }
1.122 + boolean contained = list.contains(value);
1.123 + int size = list.size();
1.124 + if ((size > 0 && remove) || (multi.isUnique() && contained)) {
1.125 + list.remove(random.nextInt(size));
1.126 + deletions++;
1.127 + } else {
1.128 + list.add(random.nextInt(size + 1), value);
1.129 + insertions++;
1.130 + }
1.131 + } else {
1.132 + SinglevaluedIndex single = (SinglevaluedIndex)index;
1.133 + if (single.getIfExists(key) != null) {
1.134 + single.remove(key);
1.135 + deletions++;
1.136 + } else {
1.137 + single.add(key, value);
1.138 + insertions++;
1.139 + }
1.140 + }
1.141 + }
1.142 + storage.close();
1.143 + totalTime = System.currentTimeMillis() - time;
1.144 + getLog().println();
1.145 + getLog().println(info + ", test time: " + totalTime);
1.146 + getLog().println("#insertions: " + insertions);
1.147 + getLog().println("#deletions: " + deletions);
1.148 + }
1.149 +
1.150 + public void doPrimaryIndexTest(Storage storage, String info) throws StorageException {
1.151 + final int ITEMS_NUM = 8000;
1.152 + final long OPS_NUM = 500000;
1.153 +
1.154 + SinglevaluedIndex index = storage.getPrimaryIndex();
1.155 + MOFID[] keys = new MOFID[ITEMS_NUM];
1.156 + PrimaryItem[] values = new PrimaryItem[ITEMS_NUM];
1.157 + for (int x = 0; x < ITEMS_NUM; x++) {
1.158 + keys[x] = generateMOFID();
1.159 + values[x] = new PrimaryItem();
1.160 + }
1.161 +
1.162 + long time = System.currentTimeMillis();
1.163 + long totalTime = 0;
1.164 + long insertions = 0;
1.165 + long deletions = 0;
1.166 +
1.167 + for (long x = 0; x < OPS_NUM; x++) {
1.168 + int pos = random.nextInt(ITEMS_NUM);
1.169 + MOFID key = keys[pos];
1.170 + PrimaryItem value = values[pos];
1.171 + Object val = index.getIfExists(key);
1.172 + if (val == null) {
1.173 + insertions++;
1.174 + index.put(key, value);
1.175 + } else {
1.176 + deletions++;
1.177 + index.remove(key);
1.178 + }
1.179 + }
1.180 + totalTime = System.currentTimeMillis() - time;
1.181 + if (index instanceof Btree) {
1.182 + TreeMetrics m = ((Btree) index).computeMetrics();
1.183 + m.print(getLog());
1.184 + }
1.185 + time = System.currentTimeMillis();
1.186 + storage.close();
1.187 +
1.188 + totalTime += System.currentTimeMillis() - time;
1.189 + getLog().println();
1.190 + getLog().println(info + ", test time: " + totalTime);
1.191 + getLog().println("#insertions: " + insertions);
1.192 + getLog().println("#deletions: " + deletions);
1.193 + }
1.194 +
1.195 /*
1.196 public void doTest3(Storage storage, String info) throws StorageException {
1.197 final int KEYS_NUM = 100;
1.198 @@ -344,5 +491,42 @@
1.199
1.200 }
1.201
1.202 + private static class PrimaryItem implements Streamable {
1.203 +
1.204 + private byte[] data;
1.205 +
1.206 + PrimaryItem() {
1.207 + int length = StorageTest.random.nextInt(256);
1.208 + data = new byte[length];
1.209 + for (int x = 0; x < length; x++) {
1.210 + data[x] = (byte)StorageTest.random.nextInt(256);
1.211 + }
1.212 + }
1.213 +
1.214 + public void read(java.io.InputStream is) throws StorageException {
1.215 + try {
1.216 + int length = is.read();
1.217 + data = new byte[length];
1.218 + for (int x = 0; x < length; x++) {
1.219 + data[x] = (byte)is.read();
1.220 + }
1.221 + } catch (IOException e) {
1.222 + throw new StorageIOException(e);
1.223 + }
1.224 + }
1.225 +
1.226 + public void write(java.io.OutputStream os) throws StorageException {
1.227 + try {
1.228 + os.write(data.length);
1.229 + for (int x = 0; x < data.length; x++) {
1.230 + os.write(data[x]);
1.231 + }
1.232 + } catch (IOException e) {
1.233 + throw new StorageIOException(e);
1.234 + }
1.235 + }
1.236 +
1.237 + }
1.238 +
1.239 }
1.240