001: package org.apache.ojb.compare;
002:
003: import java.util.Iterator;
004:
005: import org.apache.ojb.broker.ManageableCollection;
006: import org.apache.ojb.broker.TestHelper;
007: import org.apache.ojb.odmg.OJB;
008: import org.apache.ojb.odmg.TransactionExt;
009: import org.odmg.Database;
010: import org.odmg.Implementation;
011: import org.odmg.OQLQuery;
012: import org.odmg.Transaction;
013:
014: /**
015: * This TestCase contains the OJB performance benchmarks for the
016: * ODMG API.
017: *
018: * @author Matthew Baird, borrowing heavily from Thomas Mahler
019: */
020: public class PerformanceODMGTest extends PerformanceBaseTest {
021: private Implementation odmg;
022: private Database db;
023:
024: public PerformanceODMGTest(String name) {
025: super (name);
026: setNameOfTest("Test for ODMG-api");
027: }
028:
029: /**
030: * launches the TestCase.
031: * The number of Objects to work with and the number of iterations
032: * to be performed can be adjusted by setting them as commandline parameters.
033: *
034: * @param args the String[] holding the commandline parameters.
035: */
036: public static void main(String[] args) {
037: if (args.length > 0) {
038: articleCount = Integer.parseInt(args[0]);
039: }
040: if (args.length > 1) {
041: iterations = Integer.parseInt(args[1]);
042: }
043: String[] arr = { PerformanceODMGTest.class.getName() };
044: junit.textui.TestRunner.main(arr);
045: }
046:
047: public void testBenchmark() throws Exception {
048: super .testBenchmark();
049: }
050:
051: public void setUp() throws Exception {
052: // madatory to call super class method
053: super .setUp();
054:
055: odmg = OJB.getInstance();
056: db = odmg.newDatabase();
057: db.open(TestHelper.DEF_DATABASE_NAME, Database.OPEN_READ_WRITE);
058: }
059:
060: public void tearDown() throws Exception {
061: super .tearDown();
062: }
063:
064: /**
065: * deletes all PerformanceArticle created by <code>insertNewArticles</code>.
066: */
067: protected void deleteArticles() throws Exception {
068: Transaction tx = odmg.newTransaction();
069: long start = System.currentTimeMillis();
070: tx.begin();
071: for (int i = 0; i < articleCount; i++) {
072: db.deletePersistent(arr[i]);
073: }
074: tx.commit();
075: long stop = System.currentTimeMillis();
076: logger.info("deleting " + articleCount + " Objects: "
077: + (stop - start) + " msec");
078: }
079:
080: /**
081: * create new PerformanceArticle objects and insert them into the RDBMS.
082: * <p/>
083: * The number of objects to create is defined by <code>articleCount</code>.
084: */
085: protected void insertNewArticles() throws Exception {
086: Transaction tx = odmg.newTransaction();
087: long start = System.currentTimeMillis();
088: tx.begin();
089: for (int i = 0; i < articleCount; i++) {
090: db.makePersistent(arr[i]);
091: }
092: tx.commit();
093: long stop = System.currentTimeMillis();
094: logger.info("inserting " + articleCount + " Objects: "
095: + (stop - start) + " msec");
096: }
097:
098: /**
099: * read in all the PerformanceArticles from the RDBMS that have
100: * been inserted by <code>insertNewArticles()</code>.
101: * The lookup is done one by one, that is: a primary key based lookup is used.
102: */
103: protected void readArticles() throws Exception {
104: TransactionExt tx = (TransactionExt) odmg.newTransaction();
105: // we don't want implicite locks when compare performance
106: tx.setImplicitLocking(false);
107: String sql = "select allArticles from "
108: + PerformanceArticle.class.getName()
109: + " where articleId=$1";
110: long start = System.currentTimeMillis();
111: tx.begin();
112: for (int i = 0; i < articleCount; i++) {
113: OQLQuery query = odmg.newOQLQuery();
114: query.create(sql);
115: query.bind(arr[i].getArticleId());
116: query.execute();
117: }
118: tx.commit();
119: long stop = System.currentTimeMillis();
120: logger.info("querying " + articleCount + " Objects: "
121: + (stop - start) + " msec");
122: }
123:
124: /**
125: * read in all the PerformanceArticles from the RDBMS that have
126: * been inserted by <code>insertNewArticles()</code>.
127: * The lookup is done with a cursor fetch,
128: * that is: a between Statement is used to select all inserted PerformanceArticles
129: * and Objects are read in by fetching from the cursor (JDBC ResultSet).
130: */
131: protected void readArticlesByCursor() throws Exception {
132: TransactionExt tx = (TransactionExt) odmg.newTransaction();
133: // we don't want implicite locks when compare performance
134: tx.setImplicitLocking(false);
135: tx.begin();
136: // clear cache to read from DB
137: tx.getBroker().clearCache();
138:
139: long start = System.currentTimeMillis();
140: OQLQuery query = odmg.newOQLQuery();
141: String sql = "select allArticles from "
142: + PerformanceArticle.class.getName()
143: + " where articleId between " + new Integer(offsetId)
144: + " and " + new Integer(offsetId + articleCount);
145: query.create(sql);
146: ManageableCollection collection = (ManageableCollection) query
147: .execute();
148: Iterator iter = collection.ojbIterator();
149: int fetchCount = 0;
150: while (iter.hasNext()) {
151: fetchCount++;
152: iter.next();
153: }
154: long stop = System.currentTimeMillis();
155: logger.info("fetching " + fetchCount + " Objects: "
156: + (stop - start) + " msec");
157: }
158:
159: /**
160: * updates all PerformanceArticles inserted by <code>insertNewArticles()</code>.
161: * All objects are modified and changes are written to the RDBMS with an UPDATE.
162: */
163: protected void updateExistingArticles() throws Exception {
164: Transaction tx = odmg.newTransaction();
165: long start = System.currentTimeMillis();
166: tx.begin();
167: // update all objects
168: for (int i = 0; i < articleCount; i++) {
169: tx.lock(arr[i], Transaction.WRITE);
170: arr[i].setPrice(arr[i].getPrice() * 1.95583);
171: }
172: tx.commit();
173: long stop = System.currentTimeMillis();
174: logger.info("updating " + articleCount + " Objects: "
175: + (stop - start) + " msec");
176: }
177: }
|