/*
Copyright (C) 2007 Mobixess Inc. http://www.java-objects-database.com
This file is part of the JODB (Java Objects Database) open source project.
JODB is free software; you can redistribute it and/or modify it under
the terms of version 2 of the GNU General Public License as published
by the Free Software Foundation.
JODB is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package com.mobixess.jodb.tests.benchmarking;
import java.io.File;
import java.util.Random;
import com.mobixess.jodb.core.JODBSessionContainer;
import com.mobixess.jodb.core.JODB;
import com.mobixess.jodb.soda.api.ObjectContainer;
import com.mobixess.jodb.soda.api.ObjectSet;
import com.mobixess.jodb.soda.api.Query;
public class BenchmarkTest {
private final static int MAX_OBJECTS = 200000;
public static void main(String[] arg) throws Exception{
BenchmarkTest benchmarkTest = new BenchmarkTest();
System.err.println(benchmarkTest.run(".benchmark.test", false, false));
System.err.println(benchmarkTest.run(".benchmark.test", false, true));
System.err.println(benchmarkTest.run(".benchmark.test", true, false));
System.err.println(benchmarkTest.run(".benchmark.test", true, true));
}
private BenchmarkResults run(String fileName, boolean useIndex, boolean sort) throws Exception{
File file = new File(fileName);
file.delete();
if(file.exists()){
throw new RuntimeException();
}
BenchmarkResults benchmarkResults = new BenchmarkResults();
long startTime = System.currentTimeMillis();
ObjectContainer objectContainer = openOrCreateDatabase(fileName);
if(useIndex){
benchmarkResults._indexEnabled = true;
enableIndex(objectContainer);
}
fillTestData(objectContainer);
benchmarkResults._refillTime = System.currentTimeMillis() - startTime;
//form query for all elements
Query query = objectContainer.query();
query.constrain(BenchmarkObject.class);
query.descend("_val1").constrain(Integer.MIN_VALUE).greater();
query.descend("_val1").constrain(Integer.MAX_VALUE).smaller();
if(sort){
benchmarkResults._sorted = true;
query.descend("_val1").orderAscending();
}
startTime = System.currentTimeMillis();
ObjectSet objectSet = query.execute();
benchmarkResults._queryTime = System.currentTimeMillis() - startTime;
startTime = System.currentTimeMillis();
int total = objectSet.size();
if(total < MAX_OBJECTS){
throw new RuntimeException(""+total);
}
int processed = 0;
// try {
while (objectSet.hasNext()) {
Object next = objectSet.next();
processed++;
}
// } catch(Exception e){
// System.err.println("Processed = "+processed);
// e.printStackTrace();
// throw e;
// }
benchmarkResults._iteration = System.currentTimeMillis() - startTime;
objectContainer.close();
file.delete();
return benchmarkResults;
}
private ObjectContainer openOrCreateDatabase(String fileName) throws Exception{
return JODB.open(fileName);
}
private void fillTestData(ObjectContainer objectDataContainer) throws Exception{
Random random = new Random(3642902);
for (int i = 0; i < MAX_OBJECTS; i++) {
BenchmarkObject newObject = new BenchmarkObject(random.nextInt());
objectDataContainer.set(newObject);
}
objectDataContainer.commit();
}
private void enableIndex(ObjectContainer objectContainer) throws Exception{
JODBSessionContainer sessionContainer = (JODBSessionContainer) objectContainer;
sessionContainer.configureIndex(BenchmarkObject.class, "_val1", true);
}
private static class BenchmarkResults{
boolean _indexEnabled;
boolean _sorted;
long _refillTime;
long _queryTime;
long _iteration;
@Override
public String toString()
{
StringBuffer result = new StringBuffer();
result.append("Index =");
if(_indexEnabled){
result.append("on");
}else{
result.append("off");
}
result.append(" Sort=");
if(_sorted){
result.append("on");
}else{
result.append("off");
}
result.append(" Refill time ="+_refillTime);
result.append(" Query time ="+_queryTime);
result.append(" Iteration time ="+_iteration );
return result.toString();
}
}
}