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