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