KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.netbeans.mdr.test;
20
21 import java.io.IOException JavaDoc;
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; // satan's number
37

38     static Random random = new Random(RAND_VAL);
39     
40     private Set keys;
41     private Set values;
42
43     public StorageWriteTest(String JavaDoc 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         // generate keys/values pairs
62
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     ////////////////////////////////////////////////////////////////////////////
81
// tests
82
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 JavaDoc 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 JavaDoc e) {
112                 e.printStackTrace();
113                 fail(e.getMessage());
114             }
115         }
116     }
117     // end tests
118
////////////////////////////////////////////////////////////////////////////
119

120     private void sequentialWrite(Storage storage, String JavaDoc info, String JavaDoc prefix) throws StorageException {
121         // index
122
Storage.EntryType entryType = Storage.EntryType.MOFID;
123         SinglevaluedIndex index = storage.createSinglevaluedIndex(prefix + "singleIndex", entryType, entryType);
124         // insertations
125
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 JavaDoc info, String JavaDoc prefix) throws StorageException {
140         // index
141
SinglevaluedIndex index = storage.getSinglevaluedIndex(prefix + "singleIndex");
142         // deletions
143
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 JavaDoc 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 JavaDoc storageId = randomString("", 16, 16);
163         return new MOFID(serialNumber, storageId);
164     }
165     
166     public static String JavaDoc randomString(String JavaDoc prefix) {
167         final int minLength = 10;
168         final int maxLength = 20;
169         return randomString (prefix, minLength, maxLength);
170     }
171     
172     public static String JavaDoc randomString(String JavaDoc prefix, int minLength, int maxLength) {
173         String JavaDoc 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 JavaDoc[] args) {
182         junit.textui.TestRunner.run(suite());
183     }
184     
185     // ..........................................................................
186
// INNER CLASSES
187
private class Resolver implements ObjectResolver {
188         public Object JavaDoc resolve(String JavaDoc storageID, Object JavaDoc key) {
189             getLog().println("resolve object called");
190             return new Object JavaDoc();
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 JavaDoc 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 JavaDoc e) {
214                 throw new StorageIOException(e);
215             }
216         }
217
218         public void write(java.io.OutputStream JavaDoc 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 JavaDoc e) {
225                 throw new StorageIOException(e);
226             }
227         }
228         
229     }
230 }
231
Popular Tags