KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > test > StorageTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc testName) {
42         super (testName);
43     }
44     
45     public static void main (String JavaDoc[] 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     // **************************************************************************
66

67     public void test() {
68         StorageFactory factory;
69         Storage storage;
70         try {
71             // btree storage, singlevalued index
72
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             // memory storage, singlevalued index
83
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             // btree storage, multivalued index
94
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             /*
105             // memory storage, multivalued index
106             System.out.println();
107             System.out.println("*************************************************");
108             System.out.println("memory storage, multivalued test");
109             System.out.println("-------------------------------------------------");
110             factory = new StorageFactoryImpl ();
111             storage = factory.createStorage(new HashMap());
112             storage.create (true, new Resolver());
113             random = new Random(RAND_VAL);
114             doMultiTest(storage, "memory");
115              */

116         } catch (Exception JavaDoc e) {
117             e.printStackTrace ();
118             fail (e.getMessage ());
119         }
120     }
121     
122     public void doSingleTest(Storage storage, String JavaDoc 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 JavaDoc 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 JavaDoc 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     /*
235     public void doTest3(Storage storage, String info) throws StorageException {
236         final int KEYS_NUM = 100;
237         final int VALUES_NUM = 2000;
238         final long OPS_NUM = 100000;
239         
240         Storage.EntryType entryType = Storage.EntryType.STRING;
241         MultivaluedIndex index = storage.createMultivaluedIndex("multiIndex", entryType, entryType, false);
242         String[] keys = new String[KEYS_NUM];
243         String[] values = new String[VALUES_NUM];
244         for (int x = 0; x < KEYS_NUM; x++) {
245             keys[x] = generateString(60);
246         }
247         for (int x = 0; x < VALUES_NUM; x++) {
248             values[x] = generateString(200);
249         }
250         
251         long time = System.currentTimeMillis();
252         long totalTime = 0;
253         for (long x = 0; x < OPS_NUM / 10; x++) {
254             index.add(keys[random.nextInt(KEYS_NUM)], values[random.nextInt(VALUES_NUM)]);
255         }
256         for (long x = 0; x < OPS_NUM; x++) {
257             String key = keys[random.nextInt(KEYS_NUM)];
258             String value = values[random.nextInt(VALUES_NUM)];
259             List coll = (List)index.getItems(key);
260             int size = coll.size();
261             if (size == 0) {
262                 coll.add(value);
263             } else {
264                 coll.remove(random.nextInt(size));
265             }
266         }
267         totalTime += System.currentTimeMillis() - time;
268         time = System.currentTimeMillis();
269         
270         if (index instanceof Btree) {
271             TreeMetrics m = ((Btree) index).computeMetrics();
272             m.print();
273         }
274         storage.close();
275         
276         totalTime += System.currentTimeMillis() - time;
277         System.out.println();
278         System.out.println(info + ", test time: " + totalTime);
279     }
280     
281     public void doTest2(Storage storage, String info) throws StorageException {
282         final int KEYS_NUM = 100;
283         final int VALUES_NUM = 2000;
284         final long OPS_NUM = 150000;
285         
286         Storage.EntryType entryType = Storage.EntryType.STRING;
287         MultivaluedIndex index = storage.createMultivaluedIndex("multiIndex", entryType, entryType, false);
288         String[] keys = new String[KEYS_NUM];
289         String[] values = new String[VALUES_NUM];
290         for (int x = 0; x < KEYS_NUM; x++) {
291             keys[x] = generateString(60);
292         }
293         for (int x = 0; x < VALUES_NUM; x++) {
294             values[x] = generateString(200);
295         }
296         
297         long time = System.currentTimeMillis();
298         for (long x = 0; x < OPS_NUM / 10; x++) {
299             index.add(keys[random.nextInt(KEYS_NUM)], values[random.nextInt(VALUES_NUM)]);
300         }
301         for (long x = 0; x < OPS_NUM; x++) {
302             String key = keys[random.nextInt(KEYS_NUM)];
303             String value = values[random.nextInt(VALUES_NUM)];
304             List coll = (List)index.getItems(key);
305             int size = coll.size();
306             if (size == 0) {
307                 coll.add(value);
308             } else {
309                 coll.remove(random.nextInt(size));
310             }
311         }
312         storage.close();
313         System.out.println(info + ", test time: " + (System.currentTimeMillis() - time));
314     }
315      */

316     
317     public String JavaDoc 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 JavaDoc storageId = randomString("", 16, 16);
324         return new MOFID(serialNumber, storageId);
325     }
326     
327     public String JavaDoc randomString (String JavaDoc prefix) {
328         final int minLength = 10;
329         final int maxLength = 20;
330         return randomString (prefix, minLength, maxLength);
331     }
332     
333     public String JavaDoc randomString (String JavaDoc prefix, int minLength, int maxLength) {
334         String JavaDoc 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     // ..........................................................................
343

344     private class Resolver implements ObjectResolver {
345         
346         public Object JavaDoc resolve(String JavaDoc storageID, Object JavaDoc key) {
347             System.out.println("resolve object called");
348             return new Object JavaDoc();
349         }
350         
351     }
352     
353 }
354
355
Popular Tags