001: /*
002: Copyright (C) 2007 Mobixess Inc. http://www.java-objects-database.com
003:
004: This file is part of the JODB (Java Objects Database) open source project.
005:
006: JODB is free software; you can redistribute it and/or modify it under
007: the terms of version 2 of the GNU General Public License as published
008: by the Free Software Foundation.
009:
010: JODB is distributed in the hope that it will be useful, but WITHOUT ANY
011: WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
013: for more details.
014:
015: You should have received a copy of the GNU General Public License along
016: with this program; if not, write to the Free Software Foundation, Inc.,
017: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: */
019: package com.mobixess.jodb.tests.benchmarking;
020:
021: import java.io.File;
022: import java.util.Random;
023:
024: import com.mobixess.jodb.core.JODB;
025: import com.mobixess.jodb.core.JODBSessionContainer;
026: import com.mobixess.jodb.soda.api.ObjectContainer;
027: import com.mobixess.jodb.soda.api.ObjectSet;
028: import com.mobixess.jodb.soda.api.Query;
029:
030: public class BenchmarkTest {
031:
032: private final static int MAX_OBJECTS = 200000;
033:
034: public static void main(String[] arg) throws Exception {
035: BenchmarkTest benchmarkTest = new BenchmarkTest();
036: System.err.println(benchmarkTest.run(".benchmark.test", false,
037: false));
038: System.err.println(benchmarkTest.run(".benchmark.test", false,
039: true));
040: System.err.println(benchmarkTest.run(".benchmark.test", true,
041: false));
042: System.err.println(benchmarkTest.run(".benchmark.test", true,
043: true));
044: }
045:
046: private BenchmarkResults run(String fileName, boolean useIndex,
047: boolean sort) throws Exception {
048: File file = new File(fileName);
049: file.delete();
050: if (file.exists()) {
051: throw new RuntimeException();
052: }
053: BenchmarkResults benchmarkResults = new BenchmarkResults();
054: long startTime = System.currentTimeMillis();
055: ObjectContainer objectContainer = openOrCreateDatabase(fileName);
056: if (useIndex) {
057: benchmarkResults._indexEnabled = true;
058: enableIndex(objectContainer);
059: }
060: fillTestData(objectContainer);
061: benchmarkResults._refillTime = System.currentTimeMillis()
062: - startTime;
063: //form query for all elements
064: Query query = objectContainer.query();
065: query.constrain(BenchmarkObject.class);
066: query.descend("_val1").constrain(Integer.MIN_VALUE).greater();
067: query.descend("_val1").constrain(Integer.MAX_VALUE).smaller();
068: if (sort) {
069: benchmarkResults._sorted = true;
070: query.descend("_val1").orderAscending();
071: }
072:
073: startTime = System.currentTimeMillis();
074: ObjectSet objectSet = query.execute();
075: benchmarkResults._queryTime = System.currentTimeMillis()
076: - startTime;
077:
078: startTime = System.currentTimeMillis();
079: int total = objectSet.size();
080: if (total < MAX_OBJECTS) {
081: throw new RuntimeException("" + total);
082: }
083: int processed = 0;
084: // try {
085: while (objectSet.hasNext()) {
086: Object next = objectSet.next();
087: processed++;
088: }
089: // } catch(Exception e){
090: // System.err.println("Processed = "+processed);
091: // e.printStackTrace();
092: // throw e;
093: // }
094: benchmarkResults._iteration = System.currentTimeMillis()
095: - startTime;
096: objectContainer.close();
097: file.delete();
098: return benchmarkResults;
099: }
100:
101: private ObjectContainer openOrCreateDatabase(String fileName)
102: throws Exception {
103: return JODB.open(fileName);
104: }
105:
106: private void fillTestData(ObjectContainer objectDataContainer)
107: throws Exception {
108: Random random = new Random(3642902);
109: for (int i = 0; i < MAX_OBJECTS; i++) {
110: BenchmarkObject newObject = new BenchmarkObject(random
111: .nextInt());
112: objectDataContainer.set(newObject);
113: }
114: objectDataContainer.commit();
115: }
116:
117: private void enableIndex(ObjectContainer objectContainer)
118: throws Exception {
119: JODBSessionContainer sessionContainer = (JODBSessionContainer) objectContainer;
120: sessionContainer.configureIndex(BenchmarkObject.class, "_val1",
121: true);
122: }
123:
124: private static class BenchmarkResults {
125: boolean _indexEnabled;
126: boolean _sorted;
127: long _refillTime;
128: long _queryTime;
129: long _iteration;
130:
131: @Override
132: public String toString() {
133: StringBuffer result = new StringBuffer();
134: result.append("Index =");
135: if (_indexEnabled) {
136: result.append("on");
137: } else {
138: result.append("off");
139: }
140: result.append(" Sort=");
141: if (_sorted) {
142: result.append("on");
143: } else {
144: result.append("off");
145: }
146:
147: result.append(" Refill time =" + _refillTime);
148: result.append(" Query time =" + _queryTime);
149: result.append(" Iteration time =" + _iteration);
150: return result.toString();
151: }
152: }
153: }
|