001: package org.garret.bench;
002:
003: import java.io.PrintStream;
004:
005: import org.garret.perst.*;
006:
007: class Record extends Persistent {
008: String strKey;
009: long intKey;
010: };
011:
012: class Indices extends Persistent {
013: Index<Record> strIndex;
014: Index<Record> intIndex;
015: }
016:
017: public class PerstTest extends Test {
018: final static int nRecords = 10000;
019: final static int pagePoolSize = 2 * 1024 * 1024;
020:
021: String databaseName;
022: PrintStream out;
023:
024: PerstTest(String databaseName, PrintStream out) {
025: this .databaseName = databaseName;
026: this .out = out;
027: }
028:
029: public String getName() {
030: return "Perst";
031: }
032:
033: public void run() {
034: Storage db = StorageFactory.getInstance().createStorage();
035:
036: db.open(databaseName, pagePoolSize);
037: Indices root = (Indices) db.getRoot();
038: if (root == null) {
039: root = new Indices();
040: root.strIndex = db.<Record> createIndex(String.class, true);
041: root.intIndex = db.<Record> createIndex(long.class, true);
042: db.setRoot(root);
043: }
044: Index<Record> intIndex = root.intIndex;
045: Index<Record> strIndex = root.strIndex;
046: long start = System.currentTimeMillis();
047: long key = 1999;
048: int i;
049: for (i = 0; i < nRecords; i++) {
050: Record rec = new Record();
051: key = (3141592621L * key + 2718281829L) % 1000000007L;
052: rec.intKey = key;
053: rec.strKey = Long.toString(key);
054: intIndex.put(rec.intKey, rec);
055: strIndex.put(rec.strKey, rec);
056: }
057: db.commit();
058: out.println("Elapsed time for inserting " + nRecords
059: + " records: " + (System.currentTimeMillis() - start)
060: + " milliseconds");
061:
062: start = System.currentTimeMillis();
063: key = 1999;
064: for (i = 0; i < nRecords; i++) {
065: key = (3141592621L * key + 2718281829L) % 1000000007L;
066: Record rec1 = (Record) intIndex.get(key);
067: Record rec2 = (Record) strIndex.get(Long.toString(key));
068: assert (rec1 != null && rec1 == rec2);
069: }
070: out.println("Elapsed time for performing " + nRecords * 2
071: + " index searches: "
072: + (System.currentTimeMillis() - start)
073: + " milliseconds");
074:
075: start = System.currentTimeMillis();
076: key = Long.MIN_VALUE;
077: i = 0;
078: for (Record rec : intIndex) {
079: Assert.that(rec.intKey >= key);
080: key = rec.intKey;
081: i += 1;
082: }
083: assert (i == nRecords);
084: String strKey = "";
085: i = 0;
086: for (Record rec : strIndex) {
087: assert (rec.strKey.compareTo(strKey) >= 0);
088: strKey = rec.strKey;
089: i += 1;
090: }
091: assert (i == nRecords);
092: out.println("Elapsed time for iterating through "
093: + (nRecords * 2) + " records: "
094: + (System.currentTimeMillis() - start)
095: + " milliseconds");
096:
097: start = System.currentTimeMillis();
098: key = 1999;
099: for (i = 0; i < nRecords; i++) {
100: key = (3141592621L * key + 2718281829L) % 1000000007L;
101: Record rec = (Record) intIndex.get(key);
102: intIndex.remove(key, rec);
103: strIndex.remove(Long.toString(key));
104: rec.deallocate();
105: }
106: assert (!intIndex.iterator().hasNext());
107: assert (!strIndex.iterator().hasNext());
108: assert (!intIndex.iterator(null, null, Index.DESCENT_ORDER)
109: .hasNext());
110: assert (!strIndex.iterator(null, null, Index.DESCENT_ORDER)
111: .hasNext());
112: assert (!intIndex.iterator(null, null, Index.ASCENT_ORDER)
113: .hasNext());
114: assert (!strIndex.iterator(null, null, Index.ASCENT_ORDER)
115: .hasNext());
116: out.println("Elapsed time for deleting " + nRecords
117: + " records: " + (System.currentTimeMillis() - start)
118: + " milliseconds");
119: db.close();
120: out.flush();
121: done();
122: }
123: }
|