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.PersistenceBrokerException;
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.broker.util.ObjectModification;
011:
012: /**
013: * This TestCase contains the OJB single-threaded performance benchmarks for the
014: * PersistenceBroker-API.
015: *
016: * @author Thomas Mahler
017: */
018: public class PerformancePBTest extends PerformanceBaseTest {
019: public PerformancePBTest(String name) {
020: super (name);
021: setNameOfTest("Test for PB-api");
022: }
023:
024: /**
025: * launches the TestCase.
026: * The number of Objects to work with and the number of iterations
027: * to be performed can be adjusted by setting them as commandline parameters.
028: *
029: * @param args the String[] holding the commandline parameters.
030: */
031: public static void main(String[] args) {
032: if (args.length > 0) {
033: articleCount = Integer.parseInt(args[0]);
034: }
035: if (args.length > 1) {
036: iterations = Integer.parseInt(args[1]);
037: }
038:
039: String[] arr = { PerformancePBTest.class.getName() };
040: junit.textui.TestRunner.main(arr);
041: }
042:
043: public void testBenchmark() throws Exception {
044: super .testBenchmark();
045: }
046:
047: /**
048: * deletes all PerformanceArticle created by <code>insertNewArticles</code>.
049: */
050: protected void deleteArticles() throws PersistenceBrokerException {
051: long start = System.currentTimeMillis();
052: broker.beginTransaction();
053: for (int i = 0; i < articleCount; i++) {
054: broker.delete(arr[i]);
055: }
056: broker.commitTransaction();
057: long stop = System.currentTimeMillis();
058: logger.info("deleting " + articleCount + " Objects: "
059: + (stop - start) + " msec");
060: }
061:
062: /**
063: * create new PerformanceArticle objects and insert them into the RDBMS.
064: * The number of objects to create is defined by <code>articleCount</code>.
065: */
066: protected void insertNewArticles()
067: throws PersistenceBrokerException {
068: long start = System.currentTimeMillis();
069: broker.beginTransaction();
070: for (int i = 0; i < articleCount; i++) {
071: broker.store(arr[i], ObjectModification.INSERT);
072: }
073: broker.commitTransaction();
074: long stop = System.currentTimeMillis();
075: logger.info("inserting " + articleCount + " Objects: "
076: + (stop - start) + " msec");
077:
078: }
079:
080: /**
081: * read in all the PerformanceArticles from the RDBMS that have
082: * been inserted by <code>insertNewArticles()</code>.
083: * The lookup is done one by one, that is: a primary key based lookup is used.
084: */
085: protected void readArticles() throws PersistenceBrokerException {
086: long start = System.currentTimeMillis();
087: broker.beginTransaction();
088: for (int i = 0; i < articleCount; i++) {
089: Identity oid = broker.serviceIdentity().buildIdentity(
090: PerformanceArticle.class, arr[i].getArticleId());
091: broker.getObjectByIdentity(oid);
092: }
093: broker.commitTransaction();
094: long stop = System.currentTimeMillis();
095: logger.info("querying " + articleCount + " Objects: "
096: + (stop - start) + " msec");
097:
098: }
099:
100: /**
101: * read in all the PerformanceArticles from the RDBMS that have
102: * been inserted by <code>insertNewArticles()</code>.
103: * The lookup is done with a cursor fetch,
104: * that is: a between Statement is used to select all inserted PerformanceArticles
105: * and Objects are read in by fetching from the cursor (JDBC ResultSet).
106: */
107: protected void readArticlesByCursor()
108: throws PersistenceBrokerException
109:
110: {
111: broker.clearCache();
112: Criteria c = new Criteria();
113: c.addBetween("articleId", new Integer(offsetId), new Integer(
114: offsetId + articleCount));
115: Query q = new QueryByCriteria(PerformanceArticle.class, c);
116:
117: long start = System.currentTimeMillis();
118: Iterator iter = broker.getIteratorByQuery(q);
119: int fetchCount = 0;
120: while (iter.hasNext()) {
121: fetchCount++;
122: iter.next();
123: }
124: long stop = System.currentTimeMillis();
125: logger.info("fetching " + fetchCount + " Objects: "
126: + (stop - start) + " msec");
127:
128: }
129:
130: /**
131: * updates all PerformanceArticles inserted by <code>insertNewArticles()</code>.
132: * All objects are modified and changes are written to the RDBMS with an UPDATE.
133: */
134: protected void updateExistingArticles()
135: throws PersistenceBrokerException {
136: // update all objects
137: for (int i = 0; i < articleCount; i++) {
138: arr[i].setPrice(arr[i].getPrice() * 1.95583);
139: }
140:
141: long start = System.currentTimeMillis();
142: broker.beginTransaction();
143: for (int i = 0; i < articleCount; i++) {
144: broker.store(arr[i], ObjectModification.UPDATE);
145: }
146: broker.commitTransaction();
147: long stop = System.currentTimeMillis();
148: logger.info("updating " + articleCount + " Objects: "
149: + (stop - start) + " msec");
150: }
151: }
|