1 package org.apache.ojb.compare; 2 3 import java.sql.Connection ; 4 import java.sql.PreparedStatement ; 5 import java.sql.ResultSet ; 6 import java.sql.Statement ; 7 import java.util.ArrayList ; 8 import java.util.Collection ; 9 import java.util.Iterator ; 10 import java.util.List ; 11 12 import org.apache.ojb.broker.Identity; 13 import org.apache.ojb.broker.PersistenceBroker; 14 import org.apache.ojb.broker.PersistenceBrokerFactory; 15 import org.apache.ojb.broker.TestHelper; 16 import org.apache.ojb.broker.accesslayer.LookupException; 17 import org.apache.ojb.broker.query.Criteria; 18 import org.apache.ojb.broker.query.Query; 19 import org.apache.ojb.broker.query.QueryByCriteria; 20 import org.apache.ojb.broker.util.ObjectModification; 21 import org.apache.ojb.odmg.OJB; 22 import org.apache.ojb.odmg.TransactionExt; 23 import org.apache.ojb.otm.OTMConnection; 24 import org.apache.ojb.otm.OTMKit; 25 import org.apache.ojb.otm.kit.SimpleKit; 26 import org.apache.ojb.otm.lock.LockType; 27 import org.apache.ojb.performance.PerfArticle; 28 import org.apache.ojb.performance.PerfArticleImpl; 29 import org.apache.ojb.performance.PerfTest; 30 import org.odmg.Database; 31 import org.odmg.Implementation; 32 import org.odmg.ODMGException; 33 import org.odmg.OQLQuery; 34 import org.odmg.Transaction; 35 36 43 public class OJBPerfTest 44 { 45 public static class JdbcPerfTest extends PerfTest 49 { 50 private static final String TABLE_NAME = "PERF_ARTICLE"; 51 public static volatile int counter = (int) System.currentTimeMillis(); 53 private PersistenceBroker broker; 54 55 public synchronized static Long nextKey() 56 { 57 return new Long (++counter); 58 } 59 60 public void init() throws Exception 61 { 62 if (broker == null) 63 { 64 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 65 } 66 } 67 68 public void tearDown() throws Exception 69 { 70 if (broker != null) 71 { 72 broker.close(); 73 broker = null; 74 } 75 } 76 77 public String testName() 78 { 79 return "JDBC"; 80 } 81 82 public int articleCount() 83 { 84 int result = 0; 85 try 86 { 87 String sql = "SELECT COUNT(*) FROM " + TABLE_NAME; 88 Connection con = getConnection(); 89 Statement stmt = con.createStatement(); 90 ResultSet rs = stmt.executeQuery(sql); 91 rs.next(); 92 result = rs.getInt(1); 93 rs.close(); 94 stmt.close(); 95 releaseConnection(); 96 } 97 catch (Exception e) 98 { 99 e.printStackTrace(); 100 } 101 return result; 102 } 103 104 public void insertNewArticles(PerfArticle[] arr) throws Exception 105 { 106 StringBuffer buf = new StringBuffer (); 107 buf.append("INSERT INTO ").append(TABLE_NAME); 108 buf.append(" (ARTICLE_ID, ARTICLE_NAME, MINIMUM_STOCK, PRICE, UNIT, STOCK, SUPPLIER_ID)"); 109 buf.append(" VALUES (?,?,?,?,?,?,?)"); 110 Connection con = getConnection(); 112 con.setAutoCommit(false); 113 for (int i = 0; i < arr.length; i++) 114 { 115 PreparedStatement stmt = con.prepareStatement(buf.toString()); 118 PerfArticle article = arr[i]; 119 article.setArticleId(nextKey()); 121 stmt.setLong(1, article.getArticleId().longValue()); 122 stmt.setString(2, article.getArticleName()); 123 stmt.setInt(3, article.getMinimumStock()); 124 stmt.setDouble(4, article.getPrice()); 125 stmt.setString(5, article.getUnit()); 126 stmt.setInt(6, article.getStock()); 127 stmt.setInt(7, article.getSupplierId()); 128 stmt.executeUpdate(); 129 stmt.close(); 130 } 131 con.commit(); 132 con.setAutoCommit(true); 133 releaseConnection(); 134 } 135 136 public void insertNewArticlesStress(PerfArticle[] arr) throws Exception 137 { 138 System.out.println("Stress-Mode is NOT supported"); 139 } 140 141 public Collection readArticlesByCursor(String articleName) throws Exception 142 { 143 String sql = "SELECT * FROM " + TABLE_NAME + " WHERE ARTICLE_NAME LIKE '" + articleName + "'"; 144 Connection con = getConnection(); 145 Statement stmt = con.createStatement(); 146 ResultSet rs = stmt.executeQuery(sql); 147 ArrayList list = new ArrayList (); 148 while (rs.next()) 149 { 150 PerfArticle article = new PerfArticleImpl(); 151 article.setArticleId(new Long (rs.getLong("ARTICLE_ID"))); 152 article.setArticleName(rs.getString("ARTICLE_NAME")); 153 article.setMinimumStock(rs.getInt("MINIMUM_STOCK")); 154 article.setPrice(rs.getDouble("PRICE")); 155 article.setUnit(rs.getString("UNIT")); 156 article.setStock(rs.getInt("STOCK")); 157 article.setSupplierId(rs.getInt("SUPPLIER_ID")); 158 list.add(article); 159 } 160 rs.close(); 161 stmt.close(); 162 releaseConnection(); 163 return list; 164 } 165 166 public PerfArticle getArticleByIdentity(Long articleId) throws Exception 167 { 168 String sql = "SELECT * FROM " + TABLE_NAME + " WHERE ARTICLE_ID=" + articleId.longValue() + ""; 169 Connection con = getConnection(); 170 Statement stmt = con.createStatement(); 171 ResultSet rs = stmt.executeQuery(sql); 172 PerfArticle result = null; 173 while (rs.next()) 174 { 175 result = new PerfArticleImpl(); 176 result.setArticleId(new Long (rs.getLong("ARTICLE_ID"))); 177 result.setArticleName(rs.getString("ARTICLE_NAME")); 178 result.setMinimumStock(rs.getInt("MINIMUM_STOCK")); 179 result.setPrice(rs.getDouble("PRICE")); 180 result.setUnit(rs.getString("UNIT")); 181 result.setStock(rs.getInt("STOCK")); 182 result.setSupplierId(rs.getInt("SUPPLIER_ID")); 183 } 184 rs.close(); 185 stmt.close(); 186 releaseConnection(); 187 return result; 188 } 189 190 public void updateArticles(PerfArticle[] arr) throws Exception 191 { 192 StringBuffer buf = new StringBuffer (); 194 buf.append("UPDATE ").append(TABLE_NAME); 195 buf.append(" SET ARTICLE_NAME = ?, MINIMUM_STOCK = ?, PRICE = ?, UNIT = ?, STOCK = ?, SUPPLIER_ID = ?"); 196 buf.append(" WHERE ARTICLE_ID = ?"); 197 Connection con = getConnection(); 198 con.setAutoCommit(false); 199 for (int i = 0; i < arr.length; i++) 200 { 201 PreparedStatement stmt = con.prepareStatement(buf.toString()); 204 PerfArticle article = arr[i]; 205 stmt.setString(1, article.getArticleName()); 206 stmt.setInt(2, article.getMinimumStock()); 207 stmt.setDouble(3, article.getPrice()); 208 stmt.setString(4, article.getUnit()); 209 stmt.setInt(5, article.getStock()); 210 stmt.setInt(6, article.getSupplierId()); 211 stmt.setLong(7, article.getArticleId().longValue()); 212 stmt.executeUpdate(); 213 stmt.close(); 214 } 215 con.commit(); 216 con.setAutoCommit(true); 217 releaseConnection(); 218 } 219 220 public void updateArticlesStress(PerfArticle[] arr) throws Exception 221 { 222 System.out.println("Stress-Mode is NOT supported"); 223 } 224 225 public void deleteArticles(PerfArticle[] arr) throws Exception 226 { 227 String sql = "DELETE FROM " + TABLE_NAME + " WHERE ARTICLE_ID = ?"; 228 Connection con = getConnection(); 229 con.setAutoCommit(false); 230 for (int i = 0; i < arr.length; i++) 231 { 232 PreparedStatement stmt = con.prepareStatement(sql); 235 PerfArticle article = arr[i]; 236 stmt.setLong(1, article.getArticleId().longValue()); 237 stmt.execute(); 238 stmt.close(); 239 } 240 con.commit(); 241 con.setAutoCommit(true); 242 releaseConnection(); 243 } 244 245 public void deleteArticlesStress(PerfArticle[] arr) throws Exception 246 { 247 System.out.println("Stress-Mode is NOT supported"); 248 } 249 250 private Connection getConnection() throws LookupException 251 { 252 broker.serviceConnectionManager().setBatchMode(false); 254 return broker.serviceConnectionManager().getConnection(); 255 } 256 257 private void releaseConnection() 258 { 259 broker.serviceConnectionManager().releaseConnection(); 260 } 261 } 262 263 264 265 public static class PBPerfTest extends PerfTest 269 { 270 public void init() throws Exception 271 { 272 } 273 274 public void tearDown() throws Exception 275 { 276 } 277 278 public String testName() 279 { 280 return "PB"; 281 } 282 283 public int articleCount() 284 { 285 Criteria c = new Criteria(); 286 Query q = new QueryByCriteria(PerfArticleImpl.class, c); 287 int count = 0; 288 try 289 { 290 PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 291 count = broker.getCount(q); 292 broker.close(); 293 } 294 catch (Exception e) 295 { 296 e.printStackTrace(); 297 } 298 return count; 299 } 300 301 305 public void insertNewArticlesStress(PerfArticle[] arr) throws Exception 306 { 307 for (int i = 0; i < arr.length; i++) 308 { 309 PersistenceBroker broker = null; 310 try 311 { 312 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 313 broker.beginTransaction(); 314 broker.store(arr[i]); 315 broker.commitTransaction(); 316 } 317 finally 318 { 319 if (broker != null) broker.close(); 320 } 321 } 322 } 323 324 328 public void insertNewArticles(PerfArticle[] arr) throws Exception 329 { 330 PersistenceBroker broker = null; 331 try 332 { 333 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 334 broker.serviceConnectionManager().setBatchMode(true); 335 broker.beginTransaction(); 336 for (int i = 0; i < arr.length; i++) 337 { 338 broker.store(arr[i], ObjectModification.INSERT); 339 } 340 broker.commitTransaction(); 341 } 342 finally 343 { 344 if (broker != null) broker.close(); 345 } 346 } 347 348 public Collection readArticlesByCursor(String articleName) throws Exception 349 { 350 Criteria c = new Criteria(); 351 c.addLike("articleName", articleName); 352 Query q = new QueryByCriteria(PerfArticleImpl.class, c); 353 354 Collection col = null; 355 PersistenceBroker broker = null; 356 try 357 { 358 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 359 col = broker.getCollectionByQuery(q); 360 } 361 finally 362 { 363 if (broker != null) broker.close(); 364 } 365 return col; 366 } 367 368 public PerfArticle getArticleByIdentity(Long articleId) throws Exception 369 { 370 PersistenceBroker broker = null; 371 try 372 { 373 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 374 return (PerfArticle) broker.getObjectByIdentity(broker.serviceIdentity().buildIdentity(PerfArticleImpl.class, articleId)); 375 } 376 finally 377 { 378 if (broker != null) broker.close(); 379 } 380 } 381 382 public void updateArticles(PerfArticle[] arr) throws Exception 383 { 384 PersistenceBroker broker = null; 385 try 386 { 387 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 388 broker.serviceConnectionManager().setBatchMode(true); 389 broker.beginTransaction(); 390 for (int i = 0; i < arr.length; i++) 391 { 392 broker.store(arr[i], ObjectModification.UPDATE); 393 } 395 broker.commitTransaction(); 396 } 397 finally 398 { 399 if (broker != null) broker.close(); 400 } 401 } 402 403 public void updateArticlesStress(PerfArticle[] arr) throws Exception 404 { 405 for (int i = 0; i < arr.length; i++) 406 { 407 PersistenceBroker broker = null; 408 try 409 { 410 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 411 broker.beginTransaction(); 412 broker.store(arr[i]); 413 broker.commitTransaction(); 414 } 415 finally 416 { 417 if (broker != null) broker.close(); 418 } 419 } 420 } 421 422 426 public void deleteArticlesStress(PerfArticle[] arr) throws Exception 427 { 428 for (int i = 0; i < arr.length; i++) 429 { 430 PersistenceBroker broker = null; 431 try 432 { 433 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 434 broker.beginTransaction(); 435 broker.delete(arr[i]); 436 broker.commitTransaction(); 437 } 438 finally 439 { 440 if (broker != null) broker.close(); 441 } 442 } 443 } 444 445 449 public void deleteArticles(PerfArticle[] arr) throws Exception 450 { 451 PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 452 try 453 { 454 broker.serviceConnectionManager().setBatchMode(true); 455 broker.beginTransaction(); 456 for (int i = 0; i < arr.length; i++) 457 { 458 broker.delete(arr[i]); 459 } 460 broker.commitTransaction(); 461 } 462 finally 463 { 464 if (broker != null) broker.close(); 465 } 466 } 467 } 468 469 470 public static class ODMGPerfTest extends PerfTest 474 { 475 private Implementation odmg; 476 private Database db; 477 private TransactionExt m_tx; 478 479 public void init() 480 { 481 try 482 { 483 odmg = OJB.getInstance(); 484 db = odmg.newDatabase(); 485 db.open(TestHelper.DEF_DATABASE_NAME, Database.OPEN_READ_WRITE); 486 m_tx = (TransactionExt) odmg.newTransaction(); 487 } 488 catch (ODMGException e) 489 { 490 e.printStackTrace(); 491 } 492 } 493 494 public void tearDown() throws Exception 495 { 496 if (m_tx.isOpen()) m_tx.abort(); 497 } 499 500 public String testName() 501 { 502 return "ODMG"; 503 } 504 505 public int articleCount() 506 { 507 Criteria c = new Criteria(); 508 Query q = new QueryByCriteria(PerfArticleImpl.class, c); 509 int count = 0; 510 try 511 { 512 PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 513 count = broker.getCount(q); 514 broker.close(); 515 } 516 catch (Exception e) 517 { 518 e.printStackTrace(); 519 } 520 return count; 521 } 522 523 527 public void insertNewArticles(PerfArticle[] arr) throws Exception 528 { 529 m_tx.begin(); 530 for (int i = 0; i < arr.length; i++) 531 { 532 db.makePersistent(arr[i]); 533 } 534 m_tx.commit(); 535 } 536 537 541 public void insertNewArticlesStress(PerfArticle[] arr) throws Exception 542 { 543 for (int i = 0; i < arr.length; i++) 544 { 545 Transaction tx = odmg.newTransaction(); 546 tx.begin(); 547 db.makePersistent(arr[i]); 548 tx.commit(); 549 } 550 } 551 552 public Collection readArticlesByCursor(String articleName) throws Exception 553 { 554 m_tx.setImplicitLocking(false); 555 m_tx.begin(); 556 OQLQuery query = odmg.newOQLQuery(); 557 String sql = "select allArticles from " + PerfArticleImpl.class.getName() + 558 " where articleName like $1"; 559 query.create(sql); 560 query.bind(articleName); 561 List allProducts = (List ) query.execute(); 562 m_tx.commit(); 563 return allProducts; 564 } 565 566 public PerfArticle getArticleByIdentity(Long articleId) throws Exception 567 { 568 PerfArticle result; 580 m_tx.setImplicitLocking(false); 581 m_tx.begin(); 582 PersistenceBroker pb = m_tx.getBroker(); 583 result = (PerfArticle) pb.getObjectByIdentity(pb.serviceIdentity().buildIdentity(PerfArticleImpl.class, articleId)); 584 m_tx.commit(); 585 return result; 586 } 587 588 public void updateArticles(PerfArticle[] arr) throws Exception 589 { 590 m_tx.begin(); 591 for (int i = 0; i < arr.length; i++) 592 { 593 m_tx.lock(arr[i], Transaction.WRITE); 594 m_tx.markDirty(arr[i]); 595 } 596 m_tx.commit(); 597 } 598 599 public void updateArticlesStress(PerfArticle[] arr) throws Exception 600 { 601 for (int i = 0; i < arr.length; i++) 602 { 603 Transaction tx = odmg.newTransaction(); 604 tx.begin(); 605 tx.lock(arr[i], Transaction.WRITE); 606 m_tx.markDirty(arr[i]); 607 tx.commit(); 608 } 609 } 610 611 615 public void deleteArticles(PerfArticle[] arr) throws Exception 616 { 617 m_tx.begin(); 618 for (int i = 0; i < arr.length; i++) 619 { 620 db.deletePersistent(arr[i]); 621 } 622 m_tx.commit(); 623 } 624 625 629 public void deleteArticlesStress(PerfArticle[] arr) throws Exception 630 { 631 for (int i = 0; i < arr.length; i++) 632 { 633 Transaction tx = odmg.newTransaction(); 634 tx.begin(); 635 db.deletePersistent(arr[i]); 636 tx.commit(); 637 } 638 } 639 } 640 641 public static class OTMPerfTest extends PerfTest 645 { 646 private OTMKit _kit; 647 648 private OTMConnection _conn; 649 650 private org.apache.ojb.otm.core.Transaction _tx; 651 652 public void init() 653 { 654 _kit = SimpleKit.getInstance(); 655 _conn = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey()); 656 } 657 658 public void tearDown() throws Exception 659 { 660 if ((_tx != null) && _tx.isInProgress()) 661 { 662 _tx.rollback(); 663 } 664 _conn.close(); 665 } 666 667 public String testName() 668 { 669 return "OTM"; 670 } 671 672 public int articleCount() 673 { 674 Criteria c = new Criteria(); 675 Query q = new QueryByCriteria(PerfArticleImpl.class, c); 676 int count = 0; 677 try 678 { 679 PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 680 count = broker.getCount(q); 681 broker.close(); 682 } 683 catch (Exception e) 684 { 685 e.printStackTrace(); 686 } 687 return count; 688 } 689 690 694 public void insertNewArticlesStress(PerfArticle[] arr) throws Exception 695 { 696 for (int i = 0; i < arr.length; i++) 697 { 698 _tx = _kit.getTransaction(_conn); 699 _tx.begin(); 700 _conn.makePersistent(arr[i]); 701 _tx.commit(); 702 } 703 } 704 705 709 public void insertNewArticles(PerfArticle[] arr) throws Exception 710 { 711 _tx = _kit.getTransaction(_conn); 712 _tx.begin(); 713 for (int i = 0; i < arr.length; i++) 714 { 715 _conn.makePersistent(arr[i]); 716 } 717 _tx.commit(); 718 } 719 720 public Collection readArticlesByCursor(String articleName) throws Exception 721 { 722 Criteria c = new Criteria(); 723 c.addLike("articleName", articleName); 724 Query q = new QueryByCriteria(PerfArticleImpl.class, c); 725 726 _tx = _kit.getTransaction(_conn); 727 _tx.begin(); 728 Collection col = _conn.getCollectionByQuery(q, LockType.NO_LOCK); 729 _tx.commit(); 730 return col; 731 } 732 733 public PerfArticle getArticleByIdentity(Long articleId) throws Exception 734 { 735 Criteria c = new Criteria(); 736 c.addEqualTo("articleId", articleId); 737 Query q = new QueryByCriteria(PerfArticleImpl.class, c); 738 739 _tx = _kit.getTransaction(_conn); 740 _tx.begin(); 741 Collection col = _conn.getCollectionByQuery(q, LockType.NO_LOCK); 743 _tx.commit(); 744 Iterator it = col.iterator(); 745 return it.hasNext() ? (PerfArticle) it.next() : null; 746 } 747 748 public void updateArticles(PerfArticle[] arr) throws Exception 749 { 750 _tx = _kit.getTransaction(_conn); 751 _tx.begin(); 752 for (int i = 0; i < arr.length; i++) 753 { 754 Identity oid = _conn.getIdentity(arr[i]); 755 PerfArticle a = (PerfArticle) _conn.getObjectByIdentity(oid, LockType.WRITE_LOCK); 756 a.setArticleName("" + System.currentTimeMillis()); 757 } 758 _tx.commit(); 759 } 760 761 public void updateArticlesStress(PerfArticle[] arr) throws Exception 762 { 763 for (int i = 0; i < arr.length; i++) 764 { 765 _tx = _kit.getTransaction(_conn); 766 _tx.begin(); 767 Identity oid = _conn.getIdentity(arr[i]); 768 PerfArticle a = (PerfArticle) _conn.getObjectByIdentity(oid, LockType.WRITE_LOCK); 769 a.setArticleName("" + System.currentTimeMillis()); 770 _tx.commit(); 771 } 772 } 773 774 778 public void deleteArticlesStress(PerfArticle[] arr) throws Exception 779 { 780 for (int i = 0; i < arr.length; i++) 781 { 782 _tx = _kit.getTransaction(_conn); 783 _tx.begin(); 784 _conn.deletePersistent(arr[i]); 785 _tx.commit(); 786 } 787 } 788 789 793 public void deleteArticles(PerfArticle[] arr) throws Exception 794 { 795 _tx = _kit.getTransaction(_conn); 796 _tx.begin(); 797 for (int i = 0; i < arr.length; i++) 798 { 799 _conn.deletePersistent(arr[i]); 800 } 801 _tx.commit(); 802 } 803 } 804 } 805 | Popular Tags |