001: package org.apache.ojb.compare;
002:
003: import java.sql.Connection;
004: import java.sql.PreparedStatement;
005: import java.sql.ResultSet;
006: import java.sql.Statement;
007: import java.util.ArrayList;
008: import java.util.Collection;
009: import java.util.Iterator;
010: import java.util.List;
011:
012: import org.apache.ojb.broker.Identity;
013: import org.apache.ojb.broker.PersistenceBroker;
014: import org.apache.ojb.broker.PersistenceBrokerFactory;
015: import org.apache.ojb.broker.TestHelper;
016: import org.apache.ojb.broker.accesslayer.LookupException;
017: import org.apache.ojb.broker.query.Criteria;
018: import org.apache.ojb.broker.query.Query;
019: import org.apache.ojb.broker.query.QueryByCriteria;
020: import org.apache.ojb.broker.util.ObjectModification;
021: import org.apache.ojb.odmg.OJB;
022: import org.apache.ojb.odmg.TransactionExt;
023: import org.apache.ojb.otm.OTMConnection;
024: import org.apache.ojb.otm.OTMKit;
025: import org.apache.ojb.otm.kit.SimpleKit;
026: import org.apache.ojb.otm.lock.LockType;
027: import org.apache.ojb.performance.PerfArticle;
028: import org.apache.ojb.performance.PerfArticleImpl;
029: import org.apache.ojb.performance.PerfTest;
030: import org.odmg.Database;
031: import org.odmg.Implementation;
032: import org.odmg.ODMGException;
033: import org.odmg.OQLQuery;
034: import org.odmg.Transaction;
035:
036: /**
037: * Multi-threaded performance test implementation classes for testing
038: * the PB-api, ODMG-api of OJB against native JDBC using
039: * the performance-package test classes.
040: *
041: * @version $Id: OJBPerfTest.java,v 1.1.2.5 2005/12/30 00:01:41 arminw Exp $
042: */
043: public class OJBPerfTest {
044: // =====================================================================================
045: // Inner class, test handle using native JDBC
046: // =====================================================================================
047: public static class JdbcPerfTest extends PerfTest {
048: private static final String TABLE_NAME = "PERF_ARTICLE";
049: // cast to int to avoid problems with DB field length
050: public static volatile int counter = (int) System
051: .currentTimeMillis();
052: private PersistenceBroker broker;
053:
054: public synchronized static Long nextKey() {
055: return new Long(++counter);
056: }
057:
058: public void init() throws Exception {
059: if (broker == null) {
060: broker = PersistenceBrokerFactory
061: .defaultPersistenceBroker();
062: }
063: }
064:
065: public void tearDown() throws Exception {
066: if (broker != null) {
067: broker.close();
068: broker = null;
069: }
070: }
071:
072: public String testName() {
073: return "JDBC";
074: }
075:
076: public int articleCount() {
077: int result = 0;
078: try {
079: String sql = "SELECT COUNT(*) FROM " + TABLE_NAME;
080: Connection con = getConnection();
081: Statement stmt = con.createStatement();
082: ResultSet rs = stmt.executeQuery(sql);
083: rs.next();
084: result = rs.getInt(1);
085: rs.close();
086: stmt.close();
087: releaseConnection();
088: } catch (Exception e) {
089: e.printStackTrace();
090: }
091: return result;
092: }
093:
094: public void insertNewArticles(PerfArticle[] arr)
095: throws Exception {
096: StringBuffer buf = new StringBuffer();
097: buf.append("INSERT INTO ").append(TABLE_NAME);
098: buf
099: .append(" (ARTICLE_ID, ARTICLE_NAME, MINIMUM_STOCK, PRICE, UNIT, STOCK, SUPPLIER_ID)");
100: buf.append(" VALUES (?,?,?,?,?,?,?)");
101: // lookup the connection (using OJB's con pooling support to make the test more fair)
102: Connection con = getConnection();
103: con.setAutoCommit(false);
104: for (int i = 0; i < arr.length; i++) {
105: // OJB doesn't use a PS-pool (normally the JDBC driver supports statement pooling)
106: // thus to make this test more fair lookup a new PS for each object
107: PreparedStatement stmt = con.prepareStatement(buf
108: .toString());
109: PerfArticle article = arr[i];
110: // generate PK value
111: article.setArticleId(nextKey());
112: stmt.setLong(1, article.getArticleId().longValue());
113: stmt.setString(2, article.getArticleName());
114: stmt.setInt(3, article.getMinimumStock());
115: stmt.setDouble(4, article.getPrice());
116: stmt.setString(5, article.getUnit());
117: stmt.setInt(6, article.getStock());
118: stmt.setInt(7, article.getSupplierId());
119: stmt.executeUpdate();
120: stmt.close();
121: }
122: con.commit();
123: con.setAutoCommit(true);
124: releaseConnection();
125: }
126:
127: public void insertNewArticlesStress(PerfArticle[] arr)
128: throws Exception {
129: System.out.println("Stress-Mode is NOT supported");
130: }
131:
132: public Collection readArticlesByCursor(String articleName)
133: throws Exception {
134: String sql = "SELECT * FROM " + TABLE_NAME
135: + " WHERE ARTICLE_NAME LIKE '" + articleName + "'";
136: Connection con = getConnection();
137: Statement stmt = con.createStatement();
138: ResultSet rs = stmt.executeQuery(sql);
139: ArrayList list = new ArrayList();
140: while (rs.next()) {
141: PerfArticle article = new PerfArticleImpl();
142: article
143: .setArticleId(new Long(rs.getLong("ARTICLE_ID")));
144: article.setArticleName(rs.getString("ARTICLE_NAME"));
145: article.setMinimumStock(rs.getInt("MINIMUM_STOCK"));
146: article.setPrice(rs.getDouble("PRICE"));
147: article.setUnit(rs.getString("UNIT"));
148: article.setStock(rs.getInt("STOCK"));
149: article.setSupplierId(rs.getInt("SUPPLIER_ID"));
150: list.add(article);
151: }
152: rs.close();
153: stmt.close();
154: releaseConnection();
155: return list;
156: }
157:
158: public PerfArticle getArticleByIdentity(Long articleId)
159: throws Exception {
160: String sql = "SELECT * FROM " + TABLE_NAME
161: + " WHERE ARTICLE_ID=" + articleId.longValue() + "";
162: Connection con = getConnection();
163: Statement stmt = con.createStatement();
164: ResultSet rs = stmt.executeQuery(sql);
165: PerfArticle result = null;
166: while (rs.next()) {
167: result = new PerfArticleImpl();
168: result.setArticleId(new Long(rs.getLong("ARTICLE_ID")));
169: result.setArticleName(rs.getString("ARTICLE_NAME"));
170: result.setMinimumStock(rs.getInt("MINIMUM_STOCK"));
171: result.setPrice(rs.getDouble("PRICE"));
172: result.setUnit(rs.getString("UNIT"));
173: result.setStock(rs.getInt("STOCK"));
174: result.setSupplierId(rs.getInt("SUPPLIER_ID"));
175: }
176: rs.close();
177: stmt.close();
178: releaseConnection();
179: return result;
180: }
181:
182: public void updateArticles(PerfArticle[] arr) throws Exception {
183: // we don't know which field is to update, thus do do all
184: StringBuffer buf = new StringBuffer();
185: buf.append("UPDATE ").append(TABLE_NAME);
186: buf
187: .append(" SET ARTICLE_NAME = ?, MINIMUM_STOCK = ?, PRICE = ?, UNIT = ?, STOCK = ?, SUPPLIER_ID = ?");
188: buf.append(" WHERE ARTICLE_ID = ?");
189: Connection con = getConnection();
190: con.setAutoCommit(false);
191: for (int i = 0; i < arr.length; i++) {
192: // OJB doesn't use a PS-pool (normally the JDBC driver supports statement pooling)
193: // thus to make this test more fair lookup a new PS for each object
194: PreparedStatement stmt = con.prepareStatement(buf
195: .toString());
196: PerfArticle article = arr[i];
197: stmt.setString(1, article.getArticleName());
198: stmt.setInt(2, article.getMinimumStock());
199: stmt.setDouble(3, article.getPrice());
200: stmt.setString(4, article.getUnit());
201: stmt.setInt(5, article.getStock());
202: stmt.setInt(6, article.getSupplierId());
203: stmt.setLong(7, article.getArticleId().longValue());
204: stmt.executeUpdate();
205: stmt.close();
206: }
207: con.commit();
208: con.setAutoCommit(true);
209: releaseConnection();
210: }
211:
212: public void updateArticlesStress(PerfArticle[] arr)
213: throws Exception {
214: System.out.println("Stress-Mode is NOT supported");
215: }
216:
217: public void deleteArticles(PerfArticle[] arr) throws Exception {
218: String sql = "DELETE FROM " + TABLE_NAME
219: + " WHERE ARTICLE_ID = ?";
220: Connection con = getConnection();
221: con.setAutoCommit(false);
222: for (int i = 0; i < arr.length; i++) {
223: // OJB doesn't use a PS-pool (normally the JDBC driver supports statement pooling)
224: // thus to make this test more fair lookup a new PS for each object
225: PreparedStatement stmt = con.prepareStatement(sql);
226: PerfArticle article = arr[i];
227: stmt.setLong(1, article.getArticleId().longValue());
228: stmt.execute();
229: stmt.close();
230: }
231: con.commit();
232: con.setAutoCommit(true);
233: releaseConnection();
234: }
235:
236: public void deleteArticlesStress(PerfArticle[] arr)
237: throws Exception {
238: System.out.println("Stress-Mode is NOT supported");
239: }
240:
241: private Connection getConnection() throws LookupException {
242: // don't let OJB handle batching
243: broker.serviceConnectionManager().setBatchMode(false);
244: return broker.serviceConnectionManager().getConnection();
245: }
246:
247: private void releaseConnection() {
248: broker.serviceConnectionManager().releaseConnection();
249: }
250: }
251:
252: // =====================================================================================
253: // Inner class, test handle using PB-api
254: // =====================================================================================
255: public static class PBPerfTest extends PerfTest {
256: public void init() throws Exception {
257: }
258:
259: public void tearDown() throws Exception {
260: }
261:
262: public String testName() {
263: return "PB";
264: }
265:
266: public int articleCount() {
267: Criteria c = new Criteria();
268: Query q = new QueryByCriteria(PerfArticleImpl.class, c);
269: int count = 0;
270: try {
271: PersistenceBroker broker = PersistenceBrokerFactory
272: .defaultPersistenceBroker();
273: count = broker.getCount(q);
274: broker.close();
275: } catch (Exception e) {
276: e.printStackTrace();
277: }
278: return count;
279: }
280:
281: /**
282: * A resource cumbering insert-method implementation,
283: * this was used to test implementation.
284: */
285: public void insertNewArticlesStress(PerfArticle[] arr)
286: throws Exception {
287: for (int i = 0; i < arr.length; i++) {
288: PersistenceBroker broker = null;
289: try {
290: broker = PersistenceBrokerFactory
291: .defaultPersistenceBroker();
292: broker.beginTransaction();
293: broker.store(arr[i]);
294: broker.commitTransaction();
295: } finally {
296: if (broker != null)
297: broker.close();
298: }
299: }
300: }
301:
302: /**
303: * A performance optimized insert-method implementation,
304: * used to test performance.
305: */
306: public void insertNewArticles(PerfArticle[] arr)
307: throws Exception {
308: PersistenceBroker broker = null;
309: try {
310: broker = PersistenceBrokerFactory
311: .defaultPersistenceBroker();
312: broker.serviceConnectionManager().setBatchMode(true);
313: broker.beginTransaction();
314: for (int i = 0; i < arr.length; i++) {
315: broker.store(arr[i], ObjectModification.INSERT);
316: }
317: broker.commitTransaction();
318: } finally {
319: if (broker != null)
320: broker.close();
321: }
322: }
323:
324: public Collection readArticlesByCursor(String articleName)
325: throws Exception {
326: Criteria c = new Criteria();
327: c.addLike("articleName", articleName);
328: Query q = new QueryByCriteria(PerfArticleImpl.class, c);
329:
330: Collection col = null;
331: PersistenceBroker broker = null;
332: try {
333: broker = PersistenceBrokerFactory
334: .defaultPersistenceBroker();
335: col = broker.getCollectionByQuery(q);
336: } finally {
337: if (broker != null)
338: broker.close();
339: }
340: return col;
341: }
342:
343: public PerfArticle getArticleByIdentity(Long articleId)
344: throws Exception {
345: PersistenceBroker broker = null;
346: try {
347: broker = PersistenceBrokerFactory
348: .defaultPersistenceBroker();
349: return (PerfArticle) broker.getObjectByIdentity(broker
350: .serviceIdentity().buildIdentity(
351: PerfArticleImpl.class, articleId));
352: } finally {
353: if (broker != null)
354: broker.close();
355: }
356: }
357:
358: public void updateArticles(PerfArticle[] arr) throws Exception {
359: PersistenceBroker broker = null;
360: try {
361: broker = PersistenceBrokerFactory
362: .defaultPersistenceBroker();
363: broker.serviceConnectionManager().setBatchMode(true);
364: broker.beginTransaction();
365: for (int i = 0; i < arr.length; i++) {
366: broker.store(arr[i], ObjectModification.UPDATE);
367: // broker.store(arr[i]);
368: }
369: broker.commitTransaction();
370: } finally {
371: if (broker != null)
372: broker.close();
373: }
374: }
375:
376: public void updateArticlesStress(PerfArticle[] arr)
377: throws Exception {
378: for (int i = 0; i < arr.length; i++) {
379: PersistenceBroker broker = null;
380: try {
381: broker = PersistenceBrokerFactory
382: .defaultPersistenceBroker();
383: broker.beginTransaction();
384: broker.store(arr[i]);
385: broker.commitTransaction();
386: } finally {
387: if (broker != null)
388: broker.close();
389: }
390: }
391: }
392:
393: /**
394: * A resource cumbering delete-method implementation,
395: * used to test implementation
396: */
397: public void deleteArticlesStress(PerfArticle[] arr)
398: throws Exception {
399: for (int i = 0; i < arr.length; i++) {
400: PersistenceBroker broker = null;
401: try {
402: broker = PersistenceBrokerFactory
403: .defaultPersistenceBroker();
404: broker.beginTransaction();
405: broker.delete(arr[i]);
406: broker.commitTransaction();
407: } finally {
408: if (broker != null)
409: broker.close();
410: }
411: }
412: }
413:
414: /**
415: * A performance optimized delete-method implementation,
416: * used to test performance
417: */
418: public void deleteArticles(PerfArticle[] arr) throws Exception {
419: PersistenceBroker broker = PersistenceBrokerFactory
420: .defaultPersistenceBroker();
421: try {
422: broker.serviceConnectionManager().setBatchMode(true);
423: broker.beginTransaction();
424: for (int i = 0; i < arr.length; i++) {
425: broker.delete(arr[i]);
426: }
427: broker.commitTransaction();
428: } finally {
429: if (broker != null)
430: broker.close();
431: }
432: }
433: }
434:
435: // =====================================================================================
436: // Inner class, test handle using ODMG-api
437: // =====================================================================================
438: public static class ODMGPerfTest extends PerfTest {
439: private Implementation odmg;
440: private Database db;
441: private TransactionExt m_tx;
442:
443: public void init() {
444: try {
445: odmg = OJB.getInstance();
446: db = odmg.newDatabase();
447: db.open(TestHelper.DEF_DATABASE_NAME,
448: Database.OPEN_READ_WRITE);
449: m_tx = (TransactionExt) odmg.newTransaction();
450: } catch (ODMGException e) {
451: e.printStackTrace();
452: }
453: }
454:
455: public void tearDown() throws Exception {
456: if (m_tx.isOpen())
457: m_tx.abort();
458: //db.close();
459: }
460:
461: public String testName() {
462: return "ODMG";
463: }
464:
465: public int articleCount() {
466: Criteria c = new Criteria();
467: Query q = new QueryByCriteria(PerfArticleImpl.class, c);
468: int count = 0;
469: try {
470: PersistenceBroker broker = PersistenceBrokerFactory
471: .defaultPersistenceBroker();
472: count = broker.getCount(q);
473: broker.close();
474: } catch (Exception e) {
475: e.printStackTrace();
476: }
477: return count;
478: }
479:
480: /**
481: * A performance optimized insert-method implementation,
482: * used to test performance
483: */
484: public void insertNewArticles(PerfArticle[] arr)
485: throws Exception {
486: m_tx.begin();
487: for (int i = 0; i < arr.length; i++) {
488: db.makePersistent(arr[i]);
489: }
490: m_tx.commit();
491: }
492:
493: /**
494: * A resource cumbering insert-method implementation,
495: * used to test implementation
496: */
497: public void insertNewArticlesStress(PerfArticle[] arr)
498: throws Exception {
499: for (int i = 0; i < arr.length; i++) {
500: Transaction tx = odmg.newTransaction();
501: tx.begin();
502: db.makePersistent(arr[i]);
503: tx.commit();
504: }
505: }
506:
507: public Collection readArticlesByCursor(String articleName)
508: throws Exception {
509: m_tx.setImplicitLocking(false);
510: m_tx.begin();
511: OQLQuery query = odmg.newOQLQuery();
512: String sql = "select allArticles from "
513: + PerfArticleImpl.class.getName()
514: + " where articleName like $1";
515: query.create(sql);
516: query.bind(articleName);
517: List allProducts = (List) query.execute();
518: m_tx.commit();
519: return allProducts;
520: }
521:
522: public PerfArticle getArticleByIdentity(Long articleId)
523: throws Exception {
524: // m_tx.setImplicitLocking(false);
525: // m_tx.begin();
526: // OQLQuery query = odmg.newOQLQuery();
527: // String sql = "select allArticles from " + PerfArticleImpl.class.getName() +
528: // " where articleId=$";
529: // query.create(sql);
530: // query.bind(articleId);
531: // List result = (List) query.execute();
532: // m_tx.commit();
533: // return (PerfArticle) result.get(0);
534: // use OJB's extension for faster Identity lookup
535: PerfArticle result;
536: m_tx.setImplicitLocking(false);
537: m_tx.begin();
538: PersistenceBroker pb = m_tx.getBroker();
539: result = (PerfArticle) pb.getObjectByIdentity(pb
540: .serviceIdentity().buildIdentity(
541: PerfArticleImpl.class, articleId));
542: m_tx.commit();
543: return result;
544: }
545:
546: public void updateArticles(PerfArticle[] arr) throws Exception {
547: m_tx.begin();
548: for (int i = 0; i < arr.length; i++) {
549: m_tx.lock(arr[i], Transaction.WRITE);
550: m_tx.markDirty(arr[i]);
551: }
552: m_tx.commit();
553: }
554:
555: public void updateArticlesStress(PerfArticle[] arr)
556: throws Exception {
557: for (int i = 0; i < arr.length; i++) {
558: Transaction tx = odmg.newTransaction();
559: tx.begin();
560: tx.lock(arr[i], Transaction.WRITE);
561: m_tx.markDirty(arr[i]);
562: tx.commit();
563: }
564: }
565:
566: /**
567: * A performance optimized delete-method implementation,
568: * use to test performance
569: */
570: public void deleteArticles(PerfArticle[] arr) throws Exception {
571: m_tx.begin();
572: for (int i = 0; i < arr.length; i++) {
573: db.deletePersistent(arr[i]);
574: }
575: m_tx.commit();
576: }
577:
578: /**
579: * A resource cumbering insert-method implementation,
580: * use to test implementation
581: */
582: public void deleteArticlesStress(PerfArticle[] arr)
583: throws Exception {
584: for (int i = 0; i < arr.length; i++) {
585: Transaction tx = odmg.newTransaction();
586: tx.begin();
587: db.deletePersistent(arr[i]);
588: tx.commit();
589: }
590: }
591: }
592:
593: // =====================================================================================
594: // Inner class, test handle using OTM-api
595: // =====================================================================================
596: public static class OTMPerfTest extends PerfTest {
597: private OTMKit _kit;
598:
599: private OTMConnection _conn;
600:
601: private org.apache.ojb.otm.core.Transaction _tx;
602:
603: public void init() {
604: _kit = SimpleKit.getInstance();
605: _conn = _kit.acquireConnection(PersistenceBrokerFactory
606: .getDefaultKey());
607: }
608:
609: public void tearDown() throws Exception {
610: if ((_tx != null) && _tx.isInProgress()) {
611: _tx.rollback();
612: }
613: _conn.close();
614: }
615:
616: public String testName() {
617: return "OTM";
618: }
619:
620: public int articleCount() {
621: Criteria c = new Criteria();
622: Query q = new QueryByCriteria(PerfArticleImpl.class, c);
623: int count = 0;
624: try {
625: PersistenceBroker broker = PersistenceBrokerFactory
626: .defaultPersistenceBroker();
627: count = broker.getCount(q);
628: broker.close();
629: } catch (Exception e) {
630: e.printStackTrace();
631: }
632: return count;
633: }
634:
635: /**
636: * A resource cumbering insert-method implementation,
637: * this was used to test implementation.
638: */
639: public void insertNewArticlesStress(PerfArticle[] arr)
640: throws Exception {
641: for (int i = 0; i < arr.length; i++) {
642: _tx = _kit.getTransaction(_conn);
643: _tx.begin();
644: _conn.makePersistent(arr[i]);
645: _tx.commit();
646: }
647: }
648:
649: /**
650: * A performance optimized insert-method implementation,
651: * used to test performance.
652: */
653: public void insertNewArticles(PerfArticle[] arr)
654: throws Exception {
655: _tx = _kit.getTransaction(_conn);
656: _tx.begin();
657: for (int i = 0; i < arr.length; i++) {
658: _conn.makePersistent(arr[i]);
659: }
660: _tx.commit();
661: }
662:
663: public Collection readArticlesByCursor(String articleName)
664: throws Exception {
665: Criteria c = new Criteria();
666: c.addLike("articleName", articleName);
667: Query q = new QueryByCriteria(PerfArticleImpl.class, c);
668:
669: _tx = _kit.getTransaction(_conn);
670: _tx.begin();
671: Collection col = _conn.getCollectionByQuery(q,
672: LockType.NO_LOCK);
673: _tx.commit();
674: return col;
675: }
676:
677: public PerfArticle getArticleByIdentity(Long articleId)
678: throws Exception {
679: Criteria c = new Criteria();
680: c.addEqualTo("articleId", articleId);
681: Query q = new QueryByCriteria(PerfArticleImpl.class, c);
682:
683: _tx = _kit.getTransaction(_conn);
684: _tx.begin();
685: // the getByIdeneityMethod() needs Identity and this is currently not supported
686: Collection col = _conn.getCollectionByQuery(q,
687: LockType.NO_LOCK);
688: _tx.commit();
689: Iterator it = col.iterator();
690: return it.hasNext() ? (PerfArticle) it.next() : null;
691: }
692:
693: public void updateArticles(PerfArticle[] arr) throws Exception {
694: _tx = _kit.getTransaction(_conn);
695: _tx.begin();
696: for (int i = 0; i < arr.length; i++) {
697: Identity oid = _conn.getIdentity(arr[i]);
698: PerfArticle a = (PerfArticle) _conn
699: .getObjectByIdentity(oid, LockType.WRITE_LOCK);
700: a.setArticleName("" + System.currentTimeMillis());
701: }
702: _tx.commit();
703: }
704:
705: public void updateArticlesStress(PerfArticle[] arr)
706: throws Exception {
707: for (int i = 0; i < arr.length; i++) {
708: _tx = _kit.getTransaction(_conn);
709: _tx.begin();
710: Identity oid = _conn.getIdentity(arr[i]);
711: PerfArticle a = (PerfArticle) _conn
712: .getObjectByIdentity(oid, LockType.WRITE_LOCK);
713: a.setArticleName("" + System.currentTimeMillis());
714: _tx.commit();
715: }
716: }
717:
718: /**
719: * A resource cumbering delete-method implementation,
720: * used to test implementation
721: */
722: public void deleteArticlesStress(PerfArticle[] arr)
723: throws Exception {
724: for (int i = 0; i < arr.length; i++) {
725: _tx = _kit.getTransaction(_conn);
726: _tx.begin();
727: _conn.deletePersistent(arr[i]);
728: _tx.commit();
729: }
730: }
731:
732: /**
733: * A performance optimized delete-method implementation,
734: * used to test performance
735: */
736: public void deleteArticles(PerfArticle[] arr) throws Exception {
737: _tx = _kit.getTransaction(_conn);
738: _tx.begin();
739: for (int i = 0; i < arr.length; i++) {
740: _conn.deletePersistent(arr[i]);
741: }
742: _tx.commit();
743: }
744: }
745: }
|