1 5 package org.apache.ojb.broker; 6 7 import org.apache.ojb.broker.query.Criteria; 8 import org.apache.ojb.broker.query.Query; 9 import org.apache.ojb.broker.query.QueryByCriteria; 10 import org.apache.ojb.broker.util.ObjectModificationDefaultImpl; 11 import org.apache.ojb.odmg.OJB; 12 import org.apache.ojb.otm.OTMConnection; 13 import org.apache.ojb.otm.TestKit; 14 import org.apache.ojb.otm.lock.LockType; 15 import org.odmg.DList; 16 import org.odmg.Database; 17 import org.odmg.Implementation; 18 import org.odmg.OQLQuery; 19 import org.odmg.Transaction; 20 21 import java.util.Iterator ; 22 23 29 public class PerformanceTest2 { 31 private TestKit _kit; 32 private static int iterationsPerThread = 1000; 33 private static int concurrentThreads = 10; 34 private static int s_id = 10001; 36 private static boolean clientKeyGeneration = false; 38 private static int whichTest = 3; 39 40 46 private long[] times; 47 private int threadCount; 48 private String databaseName = TestHelper.DEF_DATABASE_NAME; 49 52 private Thread threads[] = null; 53 54 public PerformanceTest2() 55 { 56 this.threadCount = concurrentThreads; 57 _kit = TestKit.getTestInstance(); 58 } 59 60 63 protected void interruptThreads() 64 { 65 if (threads != null) 66 { 67 for (int i = 0; i < threads.length; i++) 68 { 69 threads[i].interrupt(); 70 } 71 } 72 System.err.println("## Test failed! ##"); 73 System.err.println("## Test failed! ##"); 74 } 75 76 79 protected void runTestClients(final TestClient[] runnables) 80 { 81 if (runnables == null) 82 { 83 throw new IllegalArgumentException ("runnables is null"); 84 } 85 threads = new Thread [runnables.length]; 86 for (int i = 0; i < threads.length; i++) 87 { 88 threads[i] = new Thread (runnables[i]); 89 } 90 for (int i = 0; i < threads.length; i++) 91 { 92 threads[i].start(); 93 } 94 try 95 { 96 for (int i = 0; i < threads.length; i++) 97 { 98 threads[i].join(); 99 } 100 } 101 catch (InterruptedException ignore) 102 { 103 System.out.println("Thread join interrupted."); 104 } 105 threads = null; 106 } 107 108 111 public synchronized static int getId() 112 { 113 return ++s_id; 114 } 115 116 public int getArticleCount() 117 { 118 Criteria c = new Criteria(); 119 Query q = new QueryByCriteria(PerformanceArticle.class, c); 120 int count = 0; 121 try 122 { 123 PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 124 count = broker.getCount(q); 125 broker.close(); 126 } 127 catch (Exception e) 128 { 129 e.printStackTrace(); 130 } 131 return count; 132 } 133 134 public synchronized void addTime(int position, long time) 135 { 136 times[position] = times[position] + time; 137 } 138 139 142 public void init() throws Exception 143 { 144 times = new long[4]; 145 threadCount = concurrentThreads; 146 147 if ((whichTest & 1) == 1) 148 { 149 PerformanceArticle art = createArticle(1000); 150 PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 151 broker.beginTransaction(); 152 broker.store(art); 153 broker.commitTransaction(); 154 155 broker.beginTransaction(); 156 broker.delete(art); 157 broker.commitTransaction(); 158 broker.close(); 159 } 160 161 if ((whichTest & 2) == 2) 162 { 163 PerformanceArticle art2 = createArticle(1001); 164 Implementation ojb = OJB.getInstance(); 165 Database db = ojb.newDatabase(); 166 db.open(databaseName, Database.OPEN_READ_WRITE); 167 Transaction tx = ojb.newTransaction(); 168 tx.begin(); 169 tx.lock(art2, Transaction.WRITE); 170 tx.commit(); 171 172 tx.begin(); 173 db.deletePersistent(art2); 174 tx.commit(); 175 db.close(); 176 } 177 178 if ((whichTest & 4) == 4) 179 { 180 PerformanceArticle art3 = createArticle(1002); 181 OTMConnection conn = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey()); 182 org.apache.ojb.otm.core.Transaction tx = _kit.getTransaction(conn); 183 tx.begin(); 184 conn.makePersistent(art3); 185 tx.commit(); 186 187 tx = _kit.getTransaction(conn); 188 tx.begin(); 189 conn.deletePersistent(art3); 190 tx.commit(); 191 } 192 } 193 194 198 private PerformanceArticle createArticle(int id) 199 { 200 PerformanceArticle a = new PerformanceArticle(); 201 if (clientKeyGeneration) a.setArticleId(getId()); 202 a.setArticleName("New Performance Article " + id); 203 a.setMinimumStock(100); 204 a.setOrderedUnits(17); 205 a.setPrice(0.45); 206 a.setProductGroupId(1); 207 a.setStock(234); 208 a.setSupplierId(4); 209 a.setUnit("bottle"); 210 return a; 211 } 212 213 public static void main(String [] args) 214 { 215 int loops = args.length > 4 ? Integer.parseInt(args[4]) : 1; 216 System.out.println("#######################################" + 217 "\n### Start stress test - do " + loops + " loop ###" + 218 "\n#######################################"); 219 for (int i = 0; i < loops; i++) 220 { 221 System.out.println("\n## perform loop " + (i + 1) + " ##"); 222 performTest(args); 223 } 224 System.exit(0); 225 } 226 227 private static void performTest(String [] args) 228 { 229 if (args.length > 0) 230 { 231 concurrentThreads = Integer.parseInt(args[0]); 232 } 233 if (args.length > 1) 234 { 235 iterationsPerThread = Integer.parseInt(args[1]); 236 } 237 if (args.length > 2) 238 { 239 clientKeyGeneration = Boolean.valueOf(args[2]).booleanValue(); 240 } 241 if (args.length > 3) 242 { 243 whichTest = Integer.parseInt(args[3]); 244 } 245 try 246 { 247 PerformanceTest2 test = new PerformanceTest2(); 248 int objectCount = 0; 249 int objectCountAfter = 0; 250 251 if ((whichTest & 1) == 1) 252 { 253 test.init(); 254 objectCount = test.getArticleCount(); 255 test.testMultithreaded_PB_api(); 256 257 System.out.println("Test-Info: Objects in DB before PB test: " + objectCount); 258 objectCountAfter = test.getArticleCount(); 259 System.out.println("Test-Info: Objects in DB after PB test: " + objectCountAfter); 260 System.out.println("Test-Info: Stress test was successful? - " + (objectCount == objectCountAfter) + " -"); 261 } 262 263 264 if ((whichTest & 2) == 2) 265 { 266 test.init(); 267 objectCount = test.getArticleCount(); 268 test.testMultithreaded_ODMG_api(); 269 270 System.err.println("Test-Info: Objects in DB before ODMG test: " + objectCount); 271 objectCountAfter = test.getArticleCount(); 272 System.err.println("Test-Info: Objects in DB after ODMG test: " + objectCountAfter); 273 System.err.println("Test-Info: Stress test was successful? - " + (objectCount == objectCountAfter) + " -"); 274 } 275 276 if ((whichTest & 4) == 4) 277 { 278 test.init(); 279 objectCount = test.getArticleCount(); 280 test.testMultithreaded_OTM_api(); 281 282 System.err.println("Test-Info: Objects in DB before OTM test: " + objectCount); 283 objectCountAfter = test.getArticleCount(); 284 System.err.println("Test-Info: Objects in DB after OTM test: " + objectCountAfter); 285 System.err.println("Test-Info: Stress test was successful? - " + (objectCount == objectCountAfter) + " -"); 286 } 287 } 288 catch (Exception e) 289 { 290 e.printStackTrace(); 291 } 292 } 293 294 public void testMultithreaded_PB_api() 295 { 296 String sep = System.getProperty("line.separator"); 297 298 System.out.println(sep + sep + "++ Start thread generation for PB api test ++"); 299 System.out.println("Begin with performance test, " + concurrentThreads + 300 " concurrent threads, handle " + iterationsPerThread + " articles per thread"); 301 PerfomanceTestClientPB[] clientsPB = new PerfomanceTestClientPB[concurrentThreads]; 302 for (int i = 0; i < concurrentThreads; i++) 303 { 304 PerfomanceTestClientPB obj = new PerfomanceTestClientPB(this); 305 clientsPB[i] = obj; 306 } 307 System.out.println(""); 308 times[0] = System.currentTimeMillis(); 309 runTestClients(clientsPB); 310 times[0] = (long) (System.currentTimeMillis() - times[0]); 311 System.out.println(buildTestSummary("PB API")); 312 System.out.println("++ End of performance test PB api ++" + sep + sep); 313 } 314 315 public void testMultithreaded_ODMG_api() 316 { 317 String sep = System.getProperty("line.separator"); 318 System.out.println("++ Start thread generation for ODMG api test ++"); 319 System.out.println("Begin with performance test, " + concurrentThreads + 320 " concurrent threads, handle " + iterationsPerThread + " articles per thread"); 321 PerfomanceTestClientODMG[] clientsODMG = new PerfomanceTestClientODMG[concurrentThreads]; 322 for (int i = 0; i < concurrentThreads; i++) 323 { 324 PerfomanceTestClientODMG obj = new PerfomanceTestClientODMG(this); 325 clientsODMG[i] = obj; 326 } 327 System.out.println(""); 328 times[0] = System.currentTimeMillis(); 329 runTestClients(clientsODMG); 330 times[0] = (long) (System.currentTimeMillis() - times[0]); 331 System.out.println(buildTestSummary("ODMG")); 332 System.out.println("++ End of performance test ODMG api ++" + sep); 333 } 334 335 public void testMultithreaded_OTM_api() 336 { 337 String sep = System.getProperty("line.separator"); 338 System.out.println("++ Start thread generation for OTM api test ++"); 339 System.out.println("Begin with performance test, " + concurrentThreads + 340 " concurrent threads, handle " + iterationsPerThread + " articles per thread"); 341 PerfomanceTestClientOTM[] clientsOTM = new PerfomanceTestClientOTM[concurrentThreads]; 342 for (int i = 0; i < concurrentThreads; i++) 343 { 344 PerfomanceTestClientOTM obj = new PerfomanceTestClientOTM(this); 345 clientsOTM[i] = obj; 346 } 347 System.out.println(""); 348 times[0] = System.currentTimeMillis(); 349 runTestClients(clientsOTM); 350 times[0] = (long) (System.currentTimeMillis() - times[0]); 351 System.out.println(buildTestSummary("OTM")); 352 System.out.println("++ End of performance test OTM api ++" + sep); 353 } 354 355 private String buildTestSummary(String key) 356 { 357 String sep = System.getProperty("line.separator"); 358 StringBuffer buf = new StringBuffer (); 359 buf.append(sep); 360 buf.append("----------------------------------------------------"); 361 buf.append(sep); 362 buf.append("TEST SUMMARY - " + key); 363 buf.append(sep); 364 buf.append(concurrentThreads + " concurrent threads, handle " + iterationsPerThread + " articles per thread"); 365 buf.append(sep); 366 buf.append("Test period: " + (double) (((double) times[0]) / 1000) + " [sec]"); 367 buf.append(sep); 368 buf.append("Inserting period: " + (times[1] / (concurrentThreads)) + " [msec]"); 369 buf.append(sep); 370 buf.append("Fetching period: " + (times[2] / (concurrentThreads)) + " [msec]"); 371 buf.append(sep); 372 buf.append("Deleting period: " + (times[3] / (concurrentThreads)) + " [msec]"); 373 buf.append(sep); 374 buf.append("----------------------------------------------------"); 375 376 return buf.toString(); 377 } 378 379 abstract class TestClient implements Runnable 380 { 381 382 } 383 384 387 class PerfomanceTestClientODMG extends TestClient 388 { 389 private static final String PRE_NAME = "A_"; 390 private PerformanceArticle[] arr; 391 private Implementation odmg; 392 private String threadName; 393 private Database m_db; 394 private PerformanceTest2 test; 395 396 public PerfomanceTestClientODMG(PerformanceTest2 test) 397 { 398 this.test = test; 399 } 400 401 public void run() 402 { 403 odmg = OJB.getInstance(); 404 threadName = Thread.currentThread().toString(); 405 arr = new PerformanceArticle[iterationsPerThread]; 406 for (int i = 0; i < iterationsPerThread; i++) 407 { 408 PerformanceArticle a = createArticle(i, threadName); 409 arr[i] = a; 410 } 411 412 try 413 { 414 insertNewArticles(); 415 readArticlesByCursor(); 416 deleteArticles(); 417 } 418 catch (Throwable e) 419 { 420 System.out.println("Error in client " + this); 421 e.printStackTrace(); 422 test.interruptThreads(); 423 } 424 } 425 426 430 private PerformanceArticle createArticle(int id, String name) 431 { 432 PerformanceArticle a = new PerformanceArticle(); 433 if (clientKeyGeneration) a.setArticleId(getId()); 434 a.setArticleName(PRE_NAME + name); 435 a.setMinimumStock(100); 436 a.setOrderedUnits(17); 437 a.setPrice(0.45); 438 a.setProductGroupId(1); 439 a.setStock(234); 440 a.setSupplierId(4); 441 a.setUnit("bottle"); 442 return a; 443 } 444 445 protected void deleteArticles() throws Exception 446 { 447 long start = System.currentTimeMillis(); 448 Database db = odmg.newDatabase(); 449 db.open(databaseName, Database.OPEN_READ_WRITE); 450 Transaction tx = odmg.newTransaction(); 451 tx.begin(); 452 for (int i = 0; i < arr.length; i++) 453 { 454 db.deletePersistent(arr[i]); 455 } 456 tx.commit(); 457 db.close(); 458 long stop = System.currentTimeMillis(); 459 times[3] = times[3] + (stop - start); 460 } 461 462 protected void insertNewArticles() throws Exception 463 { 464 long start = System.currentTimeMillis(); 465 Database db = odmg.newDatabase(); 466 db.open(databaseName, Database.OPEN_READ_WRITE); 467 Transaction tx = odmg.newTransaction(); 468 tx.begin(); 469 for (int i = 0; i < arr.length; i++) 470 { 471 tx.lock(arr[i], Transaction.WRITE); 472 } 473 tx.commit(); 474 db.close(); 475 long stop = System.currentTimeMillis(); 476 times[1] = times[1] + (stop - start); 477 } 478 479 protected void readArticlesByCursor() throws Exception 480 { 481 long start = System.currentTimeMillis(); 482 int artId = arr[0].articleId; 483 484 Database db = odmg.newDatabase(); 485 db.open(databaseName, Database.OPEN_READ_WRITE); 486 OQLQuery query = odmg.newOQLQuery(); 490 String sql = "select allArticles from " + PerformanceArticle.class.getName() + 491 " where articleName = \"" + PRE_NAME + threadName + "\""; 492 query.create(sql); 493 DList allProducts = (DList) query.execute(); 494 Iterator iter = allProducts.iterator(); 496 int fetchCount = 0; 497 while (iter.hasNext()) 498 { 499 fetchCount++; 500 PerformanceArticle a = (PerformanceArticle) iter.next(); 501 } 502 db.close(); 503 long stop = System.currentTimeMillis(); 504 times[2] = times[2] + (stop - start); 505 } 506 } 507 508 511 class PerfomanceTestClientOTM extends TestClient 512 { 513 private PerformanceArticle[] arr; 514 private String threadName; 515 private PerformanceTest2 test; 516 517 public PerfomanceTestClientOTM(PerformanceTest2 test) 518 { 519 this.test = test; 520 } 521 522 public void run() 523 { 524 threadName = Thread.currentThread().toString(); 525 arr = new PerformanceArticle[iterationsPerThread]; 526 for (int i = 0; i < iterationsPerThread; i++) 527 { 528 PerformanceArticle a = createArticle(i, threadName); 529 arr[i] = a; 530 } 531 532 try 533 { 534 OTMConnection conn = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey()); 535 insertNewArticles(conn); 536 readArticlesByCursor(conn); 537 deleteArticles(conn); 538 conn.close(); 539 } 540 catch (Throwable e) 541 { 542 System.out.println("Error in client " + this); 543 e.printStackTrace(); 544 test.interruptThreads(); 545 } 546 } 547 548 552 private PerformanceArticle createArticle(int id, String name) 553 { 554 PerformanceArticle a = new PerformanceArticle(); 555 if (clientKeyGeneration) a.setArticleId(getId()); 556 a.setArticleName("New Performance Article " + id); 557 a.setMinimumStock(100); 558 a.setOrderedUnits(17); 559 a.setPrice(0.45); 560 a.setProductGroupId(1); 561 a.setStock(234); 562 a.setSupplierId(4); 563 a.setUnit("bottle"); 564 return a; 565 } 566 567 protected void deleteArticles(OTMConnection conn) throws Exception 568 { 569 long start = System.currentTimeMillis(); 570 org.apache.ojb.otm.core.Transaction tx = _kit.getTransaction(conn); 571 tx.begin(); 572 for (int i = 0; i < arr.length; i++) 573 { 574 conn.deletePersistent(arr[i]); 575 } 576 tx.commit(); 577 long stop = System.currentTimeMillis(); 578 times[3] = times[3] + (stop - start); 579 } 580 581 protected void insertNewArticles(OTMConnection conn) throws Exception 582 { 583 long start = System.currentTimeMillis(); 584 org.apache.ojb.otm.core.Transaction tx = _kit.getTransaction(conn); 585 tx.begin(); 586 for (int i = 0; i < arr.length; i++) 587 { 588 conn.makePersistent(arr[i]); 589 } 590 tx.commit(); 591 long stop = System.currentTimeMillis(); 592 times[1] = times[1] + (stop - start); 593 } 594 595 protected void readArticlesByCursor(OTMConnection conn) throws Exception 596 { 597 long start = System.currentTimeMillis(); 598 int artId = arr[0].articleId; 599 org.apache.ojb.otm.core.Transaction tx = _kit.getTransaction(conn); 600 Criteria c = new Criteria(); 601 c.addBetween("articleId", new Integer (artId), new Integer (artId + iterationsPerThread)); 602 Query q = new QueryByCriteria(PerformanceArticle.class, c); 603 Iterator iter = conn.getIteratorByQuery(q, LockType.NO_LOCK); 604 int fetchCount = 0; 605 while (iter.hasNext()) 606 { 607 fetchCount++; 608 PerformanceArticle a = (PerformanceArticle) iter.next(); 609 } 610 long stop = System.currentTimeMillis(); 611 times[2] = times[2] + (stop - start); 612 } 613 } 614 615 616 619 class PerfomanceTestClientPB extends TestClient 620 { 621 private PerformanceArticle[] arr; 622 private PerformanceTest2 test; 623 624 public PerfomanceTestClientPB(PerformanceTest2 test) 625 { 626 this.test = test; 627 arr = new PerformanceArticle[iterationsPerThread]; 628 for (int i = 0; i < iterationsPerThread; i++) 629 { 630 PerformanceArticle a = createArticle(i); 631 arr[i] = a; 632 } 633 } 634 635 public void run() 636 { 637 try 638 { 639 insertNewArticles(); 640 readArticlesByCursor(); 641 deleteArticles(); 642 } 643 catch (Throwable e) 644 { 645 System.out.println("Error in client " + this); 646 e.printStackTrace(); 647 test.interruptThreads(); 648 } 649 } 650 651 655 private PerformanceArticle createArticle(int id) 656 { 657 PerformanceArticle a = new PerformanceArticle(); 658 if (clientKeyGeneration) a.setArticleId(getId()); 659 a.setArticleName("New Performance Article " + id); 660 a.setMinimumStock(100); 661 a.setOrderedUnits(17); 662 a.setPrice(0.45); 663 a.setProductGroupId(1); 664 a.setStock(234); 665 a.setSupplierId(4); 666 a.setUnit("bottle"); 667 return a; 668 } 669 670 protected void deleteArticles() throws Exception 671 { 672 long start = System.currentTimeMillis(); 673 674 for (int i = 0; i < arr.length; i++) 675 { 676 PersistenceBroker broker = null; 677 try 678 { 679 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 680 broker.beginTransaction(); 681 broker.delete(arr[i]); 682 broker.commitTransaction(); 683 } 684 finally 685 { 686 if (broker != null) broker.close(); 687 } 688 } 689 690 long stop = System.currentTimeMillis(); 691 times[3] = times[3] + (stop - start); 692 } 693 694 protected void insertNewArticles() throws Exception 695 { 696 697 ObjectModificationDefaultImpl needsInsert = new ObjectModificationDefaultImpl(); 698 needsInsert.setNeedsInsert(true); 699 700 long start = System.currentTimeMillis(); 701 702 for (int i = 0; i < arr.length; i++) 703 { 704 PersistenceBroker broker = null; 705 try 706 { 707 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 708 broker.beginTransaction(); 709 broker.store(arr[i], needsInsert); 710 broker.commitTransaction(); 711 } 712 finally 713 { 714 if (broker != null) broker.close(); 715 } 716 717 } 718 719 long stop = System.currentTimeMillis(); 720 times[1] = times[1] + (stop - start); 721 } 722 723 protected void readArticlesByCursor() throws Exception 724 { 725 long start = System.currentTimeMillis(); 726 int artId = arr[0].articleId; 727 728 Criteria c = new Criteria(); 729 c.addBetween("articleId", new Integer (artId), new Integer (artId + iterationsPerThread)); 730 Query q = new QueryByCriteria(PerformanceArticle.class, c); 731 732 PersistenceBroker broker = null; 733 try 734 { 735 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 736 Iterator iter = broker.getIteratorByQuery(q); 737 int fetchCount = 0; 738 while (iter.hasNext()) 739 { 740 fetchCount++; 741 PerformanceArticle a = (PerformanceArticle) iter.next(); 742 } 743 } 744 finally 745 { 746 if (broker != null) broker.close(); 747 } 748 long stop = System.currentTimeMillis(); 749 times[2] = times[2] + (stop - start); 750 } 751 } 752 } 753 | Popular Tags |