1 32 33 package stockonline.client; 52 53 import javax.ejb.*; 54 import javax.naming.*; 55 import java.rmi.*; 56 import java.util.*; 57 import java.io.*; 58 59 import stockonline.util.*; 60 import stockonline.ejb.session.interf.*; 61 62 public class Client implements Runnable 63 { 64 final static boolean verbose = false; 65 66 ResultLog resultLogByID = new ResultLog(); 69 ResultLog resultLogNewAccount = new ResultLog(); 70 ResultLog resultLogBuyStock = new ResultLog(); 71 ResultLog resultLogUpdateAccount = new ResultLog(); 72 ResultLog resultLogSellStock = new ResultLog(); 73 ResultLog resultLogGetHolding = new ResultLog(); 74 ResultLog resultLogTPS = new ResultLog(); 75 76 static Synch synch; 77 static Properties runProperties = new Properties(); 78 79 BrokerHome home = null; 80 int threadNum = 0; 81 82 int testIteration = 0; 83 int warmupIteration = 0; 84 int cooldownIteration = 0; 85 int numRetry = 0; 86 87 public Client(Synch synch, BrokerHome home, int threadNum) 88 { 89 this.synch = synch; 90 this.home = home; 91 this.threadNum = threadNum; 92 93 this.testIteration = Integer.parseInt(runProperties.getProperty("client.test.iteration", "10")); 94 if(verbose) System.out.println(Thread.currentThread().getName() + ": testIteration = " + this.testIteration); 95 96 this.warmupIteration = Integer.parseInt(runProperties.getProperty("client.warmup.iteration", "2")); 97 if(verbose) System.out.println(Thread.currentThread().getName() + ": warmupIteration = " + this.warmupIteration); 98 99 this.cooldownIteration = Integer.parseInt(runProperties.getProperty("client.cooldown.iteration", "2")); 100 if(verbose) System.out.println(Thread.currentThread().getName() + ": cooldownIteration = " + this.cooldownIteration); 101 102 this.numRetry = Integer.parseInt(runProperties.getProperty("client.create.retry", "9")); 103 if(verbose) System.out.println(Thread.currentThread().getName() + ": numRetry = " + this.numRetry); 104 } 105 106 public static void main(String [] args) 107 { 108 args_proc (args); 111 112 BrokerHome myHome = getBeanHome(); 115 116 String testType = runProperties.getProperty("client.test.type"); 119 if(testType.equalsIgnoreCase("f")) 120 functionTest_proc(myHome); 121 122 126 int threadNum = Integer.parseInt(runProperties.getProperty("client.test.P1")); 129 if(verbose) System.out.println("threadNum = " + threadNum); 130 131 Synch mySynch = new Synch(threadNum); 135 136 Client t = new Client(mySynch, myHome, threadNum); 139 140 Thread threadArray[] = new Thread [threadNum]; 143 144 System.out.println("\nTo start testing......"); 145 for (int i = 0; i < threadNum; i++) 147 { 148 threadArray[i] = new Thread (t); 149 threadArray[i].start(); 150 } 151 152 while(!synch.setGo()) Synch.sleep(100); 154 155 for (int i = 0; i < threadNum; i++) 158 { 159 try { 160 threadArray[i].join(); 161 if(verbose) System.out.println("Thread-" + i + " : exited"); 162 } 163 catch (InterruptedException e) {} 164 } 165 166 System.out.println("\n--- All threads are finished ---\n"); 167 168 collectTestResult (t); 171 } 172 173 private static void collectTestResult ( Client t ) 174 { 175 try 176 { 177 System.out.println(); 180 System.out.println("----- Test Result --------"); 181 System.out.println("Buy (" + t.resultLogBuyStock.getCount() + ") " + t.resultLogBuyStock.getAverage()); 182 System.out.println("Create (" + t.resultLogNewAccount.getCount() + ") " + t.resultLogNewAccount.getAverage()); 183 System.out.println("Holding (" + t.resultLogGetHolding.getCount() + ") " + t.resultLogGetHolding.getAverage()); 184 System.out.println("Query (" + t.resultLogByID.getCount() + ") " + t.resultLogByID.getAverage()); 185 System.out.println("Sell (" + t.resultLogSellStock.getCount() + ") " + t.resultLogSellStock.getAverage()); 186 System.out.println("Update (" + t.resultLogUpdateAccount.getCount() + ") " + t.resultLogUpdateAccount.getAverage()); 187 System.out.println(); 188 System.out.println("TPS (" + t.resultLogTPS.getCount() + ") " + t.resultLogTPS.getTotal()); 189 System.out.println("--------------------------"); 190 191 String fileName = runProperties.getProperty("client.logfile"); 194 if(fileName==null) return; 195 196 System.out.println( t.resultLogBuyStock.getAverage()); 199 System.out.println( t.resultLogNewAccount.getAverage()); 200 System.out.println( t.resultLogGetHolding.getAverage()); 201 System.out.println( t.resultLogByID.getAverage()); 202 System.out.println( t.resultLogSellStock.getAverage()); 203 System.out.println( t.resultLogUpdateAccount.getAverage()); 204 System.out.println(); 205 System.out.println( t.resultLogTPS.getTotal()); 206 207 if(verbose) System.out.println("The test result is to be written to: " + fileName); 208 FileOutputStream fos = new FileOutputStream(fileName); 209 PrintWriter pw = new PrintWriter(fos); 210 211 pw.println( t.resultLogBuyStock.getAverage()); 212 pw.println( t.resultLogNewAccount.getAverage()); 213 pw.println( t.resultLogGetHolding.getAverage()); 214 pw.println( t.resultLogByID.getAverage()); 215 pw.println( t.resultLogSellStock.getAverage()); 216 pw.println( t.resultLogUpdateAccount.getAverage()); 217 pw.println( t.resultLogTPS.getTotal()); 218 219 pw.close (); 220 fos.close(); 221 } 222 catch(Exception e) 223 { 224 System.err.println("File I/O exception: " + e.toString()); 225 } 226 } 227 228 private static BrokerHome getBeanHome() 229 { 230 try 231 { 232 String clientType = runProperties.getProperty("client.type"); 235 if(verbose) System.out.println("clientType = " + clientType); 236 237 Context ctx = null; 240 ctx = new InitialContext(runProperties); 241 243 String ejbJNDIName = runProperties.getProperty("ejb.JNDIName"); 246 if(verbose) System.out.println("ejbJNDIName = " + ejbJNDIName); 247 248 251 Object obj = ctx.lookup(ejbJNDIName); 252 BrokerHome myHome = (BrokerHome)javax.rmi.PortableRemoteObject.narrow(obj, BrokerHome.class); 253 254 if(verbose) System.out.println("getBeanHome()............OK"); 255 ctx.close(); 256 257 return myHome; 258 } 259 catch (javax.naming.NamingException e) 260 { 261 System.err.println("Unable to find the object, due to: " + e.toString()); 262 return null; 263 } 264 catch (Exception e) 265 { 266 System.err.println(e.toString()); 267 return null; 268 } 269 } 270 271 public void run() 272 { 273 Broker broker = null; 274 275 int accountID; 276 QueryResult qr = null; 277 Collection list = null; 278 TranDeck tranDeck = null; 279 TranInfo tranInfo = null; 280 long startTime, endTime; 281 282 stockonline.util.Timer timer = new stockonline.util.Timer(); 283 int _testIteration = 0; 284 int _warmupIteration = warmupIteration; 285 int _cooldownIteration = cooldownIteration; 286 int _numRetry = numRetry; 287 288 _testIteration = testIteration + _warmupIteration + _cooldownIteration; 292 293 ResultLog _resultLogByID = new ResultLog(); 299 ResultLog _resultLogNewAccount = new ResultLog(); 300 ResultLog _resultLogBuyStock = new ResultLog(); 301 ResultLog _resultLogUpdateAccount = new ResultLog(); 302 ResultLog _resultLogSellStock = new ResultLog(); 303 ResultLog _resultLogGetHolding = new ResultLog(); 304 ResultLog _resultLogTPS = new ResultLog(); 305 306 try 307 { 308 tranDeck = new TranDeck(); 309 310 tranDeck.shuffle(); 313 tranInfo = tranDeck.getNext(); 314 sleep(tranInfo.accountNo); 315 316 for(int i=0; i<_numRetry; i++) 318 { 319 broker = home.create(); 320 if(broker!=null) break; 321 else System.out.println(Thread.currentThread().getName() + " : To retry " + i); 322 } 323 324 synch.increase(); 327 while(!synch.getGo()) Thread.sleep(99); 328 329 if(broker==null) 333 { 334 System.err.println(Thread.currentThread().getName() + " : failed to create the bean"); 335 return; 336 } 337 } 338 catch (Exception e) 339 { 340 System.err.println("Got exception: " + e.toString()); 341 e.printStackTrace(); 342 } 343 344 for (int i = 0; i < _warmupIteration; i++) 347 { 348 tranDeck.shuffle(); 350 if(verbose) System.out.println(Thread.currentThread().getName() + " - Loop " + i); 351 352 for (int j = 0; j < TranDeck.TRAN_SIZE; j++) 353 { 354 tranInfo = tranDeck.getNext(); 356 357 try 358 { 359 switch (tranInfo.tranType) 360 { 361 case TranDeck.TRAN_BUY: 362 startTime = ResultLog.getCurrentTime(); 363 broker.buyStock(tranInfo.accountNo, tranInfo.stockID, 5); 364 endTime = ResultLog.getCurrentTime(); 365 break; 367 368 case TranDeck.TRAN_SELL: 369 startTime = ResultLog.getCurrentTime(); 370 broker.sellStock(tranInfo.accountNo, tranInfo.stockID, 5); 371 endTime = ResultLog.getCurrentTime(); 372 break; 374 375 case TranDeck.TRAN_QUERY_ID: 376 startTime = ResultLog.getCurrentTime(); 377 qr = broker.queryStockValueByID(tranInfo.stockID); 378 endTime = ResultLog.getCurrentTime(); 379 break; 381 382 case TranDeck.TRAN_QUERY_CODE: 383 startTime = ResultLog.getCurrentTime(); 384 qr = broker.queryStockValueByID(tranInfo.stockID); 385 endTime = ResultLog.getCurrentTime(); 386 break; 388 389 case TranDeck.TRAN_GETHOLDING: 390 startTime = ResultLog.getCurrentTime(); 391 list = broker.getHoldingStatement(tranInfo.accountNo, 0); 392 endTime = ResultLog.getCurrentTime(); 393 break; 395 396 case TranDeck.TRAN_CREATE: 397 startTime = ResultLog.getCurrentTime(); 398 accountID = broker.newAccount("myName", "myAddress", 123456); 399 endTime = ResultLog.getCurrentTime(); 400 break; 402 403 case TranDeck.TRAN_UPDATE: 404 startTime = ResultLog.getCurrentTime(); 405 broker.updateAccount(tranInfo.accountNo, tranInfo.stockID*tranInfo.accountNo); 406 endTime = ResultLog.getCurrentTime(); 407 break; 409 } 410 411 415 Thread.yield(); 417 } 418 catch (Exception e) 419 { 420 e.printStackTrace(); 423 } } } 427 timer.start(); 430 431 for (int i = 0; i < _testIteration; i++) 432 { 433 tranDeck.shuffle(); 435 if(verbose) System.out.println(Thread.currentThread().getName() + " - Loop " + i); 436 437 for (int j = 0; j < TranDeck.TRAN_SIZE; j++) 438 { 439 tranInfo = tranDeck.getNext(); 441 442 try 443 { 444 switch (tranInfo.tranType) 445 { 446 case TranDeck.TRAN_BUY: 447 startTime = ResultLog.getCurrentTime(); 448 broker.buyStock(tranInfo.accountNo, tranInfo.stockID, 5); 449 endTime = ResultLog.getCurrentTime(); 450 _resultLogBuyStock.addSample(endTime - startTime); 451 break; 452 453 case TranDeck.TRAN_SELL: 454 startTime = ResultLog.getCurrentTime(); 455 broker.sellStock(tranInfo.accountNo, tranInfo.stockID, 5); 456 endTime = ResultLog.getCurrentTime(); 457 _resultLogSellStock.addSample(endTime - startTime); 458 break; 459 460 case TranDeck.TRAN_QUERY_ID: 461 startTime = ResultLog.getCurrentTime(); 462 qr = broker.queryStockValueByID(tranInfo.stockID); 463 endTime = ResultLog.getCurrentTime(); 464 _resultLogByID.addSample(endTime - startTime); 465 break; 466 467 case TranDeck.TRAN_QUERY_CODE: 468 startTime = ResultLog.getCurrentTime(); 469 qr = broker.queryStockValueByID(tranInfo.stockID); 470 endTime = ResultLog.getCurrentTime(); 471 _resultLogByID.addSample(endTime - startTime); 472 break; 473 474 case TranDeck.TRAN_GETHOLDING: 475 startTime = ResultLog.getCurrentTime(); 476 list = broker.getHoldingStatement(tranInfo.accountNo, 0); 477 endTime = ResultLog.getCurrentTime(); 478 _resultLogGetHolding.addSample(endTime - startTime); 479 break; 480 481 case TranDeck.TRAN_CREATE: 482 startTime = ResultLog.getCurrentTime(); 483 accountID = broker.newAccount("myName", "myAddress", 123456); 484 endTime = ResultLog.getCurrentTime(); 485 _resultLogNewAccount.addSample(endTime - startTime); 486 break; 487 488 case TranDeck.TRAN_UPDATE: 489 startTime = ResultLog.getCurrentTime(); 490 broker.updateAccount(tranInfo.accountNo, tranInfo.stockID*tranInfo.accountNo); 491 endTime = ResultLog.getCurrentTime(); 492 _resultLogUpdateAccount.addSample(endTime - startTime); 493 break; 494 } 495 496 500 } 503 catch (Exception e) 504 { 505 e.printStackTrace(); 508 } } } 512 timer.stop(); 513 514 for (int i = 0; i < _warmupIteration; i++) 516 { 517 tranDeck.shuffle(); 519 if(verbose) System.out.println(Thread.currentThread().getName() + " - Loop " + i); 520 521 for (int j = 0; j < TranDeck.TRAN_SIZE; j++) 522 { 523 tranInfo = tranDeck.getNext(); 525 526 try 527 { 528 switch (tranInfo.tranType) 529 { 530 case TranDeck.TRAN_BUY: 531 startTime = ResultLog.getCurrentTime(); 532 broker.buyStock(tranInfo.accountNo, tranInfo.stockID, 5); 533 endTime = ResultLog.getCurrentTime(); 534 break; 536 537 case TranDeck.TRAN_SELL: 538 startTime = ResultLog.getCurrentTime(); 539 broker.sellStock(tranInfo.accountNo, tranInfo.stockID, 5); 540 endTime = ResultLog.getCurrentTime(); 541 break; 543 544 case TranDeck.TRAN_QUERY_ID: 545 startTime = ResultLog.getCurrentTime(); 546 qr = broker.queryStockValueByID(tranInfo.stockID); 547 endTime = ResultLog.getCurrentTime(); 548 break; 550 551 case TranDeck.TRAN_QUERY_CODE: 552 startTime = ResultLog.getCurrentTime(); 553 qr = broker.queryStockValueByID(tranInfo.stockID); 554 endTime = ResultLog.getCurrentTime(); 555 break; 557 558 case TranDeck.TRAN_GETHOLDING: 559 startTime = ResultLog.getCurrentTime(); 560 list = broker.getHoldingStatement(tranInfo.accountNo, 0); 561 endTime = ResultLog.getCurrentTime(); 562 break; 564 565 case TranDeck.TRAN_CREATE: 566 startTime = ResultLog.getCurrentTime(); 567 accountID = broker.newAccount("myName", "myAddress", 123456); 568 endTime = ResultLog.getCurrentTime(); 569 break; 571 572 case TranDeck.TRAN_UPDATE: 573 startTime = ResultLog.getCurrentTime(); 574 broker.updateAccount(tranInfo.accountNo, tranInfo.stockID*tranInfo.accountNo); 575 endTime = ResultLog.getCurrentTime(); 576 break; 578 } 579 580 584 Thread.yield(); 586 } 587 catch (Exception e) 588 { 589 e.printStackTrace(); 592 } } } 596 try 599 { 600 broker.remove(); 601 } 602 catch (Exception e) 603 { 604 System.err.println("fail to remove the bean, due to: " + e.toString()); 605 e.printStackTrace(); 606 607 } 608 609 long numTrans = testIteration*43; 612 long TPS = 1000*numTrans/timer.getTime(); 613 _resultLogTPS.addSample(TPS); 614 615 resultLogByID.add(_resultLogByID); 618 resultLogNewAccount.add(_resultLogNewAccount); 619 resultLogBuyStock.add(_resultLogBuyStock); 620 resultLogUpdateAccount.add(_resultLogUpdateAccount); 621 resultLogSellStock.add(_resultLogSellStock); 622 resultLogGetHolding.add(_resultLogGetHolding); 623 resultLogTPS.add(_resultLogTPS); 624 625 return; 626 } 627 628 private static void args_proc(String [] args) 631 { 632 if (args.length < 1) 633 { 634 System.err.println("Usage: java stockonline.client.Client -test type -env run.properties [-log testResult.log] -param P1 [P2] [3] [P4]"); 635 System.err.println("where: type = f | p"); 636 System.err.println(" run.properties = name of the file where testing environment properties are set"); 637 System.err.println(" testResult.log = name of the file where test results will be put into, only for performance test"); 638 System.err.println(" P1 depending on test type:"); 639 System.err.println(" if type = p, P1 = numner of threads, no [P2] [3] and [P4] are required."); 640 System.err.println(" if type = f, P1 = create | query | buy | sell | holding | update | resetdb | testrollback"); 641 System.err.println(" [P2] [P3] [P4] are optional, depending on P1:"); 642 System.err.println(" if P1 = create, P2 = name, P3 = address and P4 = credit"); 643 System.err.println(" if P1 = update, P2 = accountID and P3 = credit"); 644 System.err.println(" if P1 = holding, P2 = accountID"); 645 System.err.println(" if P1 = query, P2 = stockID (1-3000)"); 646 System.err.println(" if P1 = buy | sell, P2 = accountID, P3 = stockID and P4 = amount "); 647 System.err.println(" if P1 = resetDB | testrollback, no [P2] [3] and [P4] are required. "); 648 System.err.println(" Note: For all funcational test, [P2] [3] and [P4] are optional. "); 649 System.err.println(" This means valid default parameters can be applied if they are not provided."); 650 651 System.exit(1); 652 } 653 654 try 655 { 656 String key = null; 657 int k = 2; 658 659 for(int i=0; i<args.length; i += k) 660 { 661 if(args[i].equalsIgnoreCase("-test") || args[i].equalsIgnoreCase("-t")) 662 { 663 key = "client.test.type"; 664 if(verbose) System.out.println(key + " = " + args[i+1]); 665 runProperties.setProperty(key, args[i+1]); 666 } 667 else if(args[i].equalsIgnoreCase("-env") || args[i].equalsIgnoreCase("-e")) 668 { 669 if(verbose) System.out.println("test properties fileName = " + args[i+1]); 670 java.io.FileInputStream in = new java.io.FileInputStream (args[i+1]); 671 runProperties.load(in); 672 if(verbose) runProperties.list( System.out ); 673 } 674 else if(args[i].equalsIgnoreCase("-log") || args[i].equalsIgnoreCase("-l")) 675 { 676 key = "client.logfile"; 677 if(verbose) System.out.println(key + " = " + args[i+1]); 678 runProperties.setProperty(key, args[i+1]); 679 } 680 else if(args[i].equalsIgnoreCase("-param") || args[i].equalsIgnoreCase("-p")) 681 { 682 for(k=1; k<args.length - i; k++) 683 { 684 key = "client.test.P" + k; 685 if(verbose) System.out.println(key + " = " + args[i+k]); 686 runProperties.setProperty(key, args[i+k]); 687 } 688 } 689 } 690 } 691 catch(java.io.FileNotFoundException e1) 692 { 693 e1.printStackTrace(); 694 System.exit(1); 695 } 696 catch(java.io.IOException e2) 697 { 698 e2.printStackTrace(); 699 System.exit(1); 700 } 701 catch(Exception e3) 702 { 703 e3.printStackTrace(); 704 System.exit(1); 705 } 706 } 707 708 private static void functionTest_proc(BrokerHome myHome) 709 { 710 System.out.println("\n To perform functional Test:\n"); 711 712 String testName = runProperties.getProperty("client.test.P1"); 713 System.out.println("To test: " + testName); 714 715 try 716 { 717 System.out.println("To create a broker bean instance"); 718 Broker broker = myHome.create(); 719 720 if ( testName.equalsIgnoreCase("create") ) 721 { 722 String name = runProperties.getProperty("client.test.P2", "test"); 723 String addr = runProperties.getProperty("client.test.P3", "CMIS"); 724 int credit = Integer.parseInt(runProperties.getProperty("P4", "10000")); 725 726 System.out.println("\t name = " + name); 727 System.out.println("\t addr = " + addr); 728 System.out.println("\t credit = " + credit); 729 730 int accountID = broker.newAccount(name, addr, credit); 731 System.out.println("the new accountID = " + accountID); 732 } 733 else if ( testName.equalsIgnoreCase("update") ) 734 { 735 int accountID = Integer.parseInt(runProperties.getProperty("client.test.P2", "1")); 736 int credit = Integer.parseInt(runProperties.getProperty("client.test.P3", "10000")); 737 System.out.println("\t accounID = " + accountID); 738 System.out.println("\t credit = " + credit); 739 740 broker.updateAccount(accountID, credit); 741 System.out.println("........................OK"); 742 } 743 else if ( testName.equalsIgnoreCase("query") ) 744 { 745 int stockID = Integer.parseInt(runProperties.getProperty("client.test.P2", "1")); 746 System.out.println("\t stockID = " + stockID); 747 748 QueryResult rs = (QueryResult) broker.queryStockValueByID(stockID); 749 750 System.out.println("current_val = " + rs.current_val); 751 System.out.println(" high_val = " + rs.high_val); 752 System.out.println(" low_val = " + rs.low_val); 753 } 754 else if ( testName.equalsIgnoreCase("buy") ) 755 { 756 int accountID = Integer.parseInt(runProperties.getProperty("client.test.P2", "1")); 757 int stockID = Integer.parseInt(runProperties.getProperty("client.test.P3", "10")); 758 int amount = Integer.parseInt(runProperties.getProperty("client.test.P4", "10")); 759 760 System.out.println("\t accountID = " + accountID); 761 System.out.println("\t stockID = " + stockID); 762 System.out.println("\t amount = " + amount); 763 764 broker.buyStock(accountID, stockID, amount); 765 System.out.println("Buy.........................OK"); 766 } 767 else if ( testName.equalsIgnoreCase("sell") ) 768 { 769 int accountID = Integer.parseInt(runProperties.getProperty("client.test.P2", "1")); 770 int stockID = Integer.parseInt(runProperties.getProperty("client.test.P3", "10")); 771 int amount = Integer.parseInt(runProperties.getProperty("client.test.P4", "10")); 772 773 System.out.println("\t accountID = " + accountID); 774 System.out.println("\t stockID = " + stockID); 775 System.out.println("\t amount = " + amount); 776 777 broker.sellStock(accountID, stockID, amount); 778 System.out.println("Sell........................OK"); 779 } 780 else if ( testName.equalsIgnoreCase("holding") ) 781 { 782 int accountID = Integer.parseInt(runProperties.getProperty("client.test.P2", "1")); 783 System.out.println("\t accountID = " + accountID); 784 785 Collection list = broker.getHoldingStatement(accountID, 0); 786 Iterator iterator = list.iterator(); 787 788 for(int i=0; iterator.hasNext(); i++) 789 { 790 Holding hold = (Holding) iterator.next(); 791 System.out.println("stockID = " + hold.stock_id + " amount = " + hold.amount); 792 } 793 } 794 else if ( testName.equalsIgnoreCase("testrollback") ) 795 { 796 int accountID = broker.newAccountForTestRollback("test", "rollback", 8888); 797 System.out.println("accountID = " + accountID); 798 799 if(broker.testRollback(accountID)) 800 System.out.println("Rollback................OK"); 801 else 802 System.out.println("Rollback................Failed"); 803 } 804 else 805 { 806 throw new Exception ("Invalid test name. Run the program with no args to get help."); 807 } 808 809 broker.remove(); 811 } 812 catch (Exception ex) 813 { 814 System.err.println(ex.toString()); 815 } 816 817 System.exit(0); 818 } 819 820 private static void sleep(int inval) 821 { 822 try 823 { 824 Thread.sleep(inval); 825 } catch(Exception ex1) {} 826 } 827 } 828 | Popular Tags |