1 22 package org.jboss.test.bankiiop.test; 23 24 import java.util.Collection ; 25 import java.util.Iterator ; 26 import javax.rmi.PortableRemoteObject ; 27 28 import junit.framework.Test; 29 import org.jboss.logging.Logger; 30 import org.jboss.test.JBossIIOPTestCase; 31 import org.jboss.test.bankiiop.interfaces.Account; 32 import org.jboss.test.bankiiop.interfaces.AccountData; 33 import org.jboss.test.bankiiop.interfaces.AccountHome; 34 import org.jboss.test.bankiiop.interfaces.Bank; 35 import org.jboss.test.bankiiop.interfaces.BankHome; 36 import org.jboss.test.bankiiop.interfaces.Customer; 37 import org.jboss.test.bankiiop.interfaces.CustomerHome; 38 import org.jboss.test.bankiiop.interfaces.Teller; 39 import org.jboss.test.bankiiop.interfaces.TellerHome; 40 41 47 public class BankStressTestCase 48 extends JBossIIOPTestCase 49 { 50 52 int idx = 1; 54 int iter; 55 Exception exc; 56 57 59 public BankStressTestCase(String name) 61 { 62 super(name); 63 } 64 65 public void testTeller() 67 throws Exception 68 { 69 TellerHome home = (TellerHome)PortableRemoteObject.narrow( 70 getInitialContext().lookup(TellerHome.JNDI_NAME), 71 TellerHome.class); 72 Teller teller = home.create(); 73 74 BankHome bankHome = (BankHome)PortableRemoteObject.narrow( 75 getInitialContext().lookup(BankHome.JNDI_NAME), 76 BankHome.class); 77 Bank bank = bankHome.create(); 78 79 getLog().debug("Acquire customers"); 80 Customer marc = teller.getCustomer("Marc"); 81 Customer rickard = teller.getCustomer("Rickard"); 82 83 getLog().debug("Acquire accounts"); 84 Account from = teller.getAccount(marc, 200); 85 Account to = teller.getAccount(rickard, 200); 86 87 getLog().debug("Show balance"); 88 getLog().debug(from.getPrimaryKey()+":"+from.getBalance()); 89 getLog().debug(to.getPrimaryKey()+":"+to.getBalance()); 90 91 getLog().debug("Transfer money"); 92 93 long start = System.currentTimeMillis(); 94 int iter = 10; 95 for (int i = 0; i < iter; i++) 96 teller.transfer(from, to, 50); 97 long end = System.currentTimeMillis(); 98 getLog().info("Average call time: "+((end - start) / (iter*6))); 99 100 getLog().debug("Show balance"); 101 AccountHome accountHome = (AccountHome)PortableRemoteObject.narrow( 102 getInitialContext().lookup(AccountHome.JNDI_NAME), 103 AccountHome.class); 104 Collection accts = accountHome.findAll(); 105 Iterator i = accts.iterator(); 106 while(i.hasNext()) 107 { 108 Account acct = (Account)PortableRemoteObject.narrow(i.next(), 109 Account.class); 110 AccountData data = acct.getData(); 111 getLog().debug(data.getId()+"("+data.getOwner().getName()+"):"+data.getBalance()); 112 acct.withdraw(data.getBalance()); } 114 115 teller.remove(); 116 } 117 118 public void testBank() 119 throws Exception 120 { 121 getLog().debug("Get code"); 122 BankHome bankHome = (BankHome)PortableRemoteObject.narrow( 123 getInitialContext().lookup(BankHome.JNDI_NAME), 124 BankHome.class); 125 126 Bank bank = bankHome.create(); 127 128 getLog().debug("Bank id="+bank.getId()); 129 bank.remove(); 130 } 131 132 181 182 public void testMultiThread() 183 throws Exception 184 { 185 TellerHome home = (TellerHome)PortableRemoteObject.narrow( 186 getInitialContext().lookup(TellerHome.JNDI_NAME), 187 TellerHome.class); 188 final Teller teller = home.create(); 189 190 getLog().debug("Acquire customers"); 191 Customer marc = teller.getCustomer("Marc"); 192 Customer rickard = teller.getCustomer("Rickard"); 193 194 getLog().debug("Acquire accounts"); 195 final Account from = teller.getAccount(marc, 50); 196 final Account to = teller.getAccount(rickard, 0); 197 198 final Object lock = new Object (); 199 200 201 iter = getThreadCount(); 202 final int iterationCount = getIterationCount(); 203 getLog().info("Start test. "+getThreadCount()+ " threads, "+getIterationCount()+" iterations"); 204 long start = System.currentTimeMillis(); 205 206 for (int i = 0; i < getThreadCount(); i++) 207 { 208 Thread.sleep(50); 209 new Thread (new Runnable () 210 { 211 public void run() 212 { 213 Logger log = Logger.getLogger(getClass().getName()); 214 215 try 216 { 217 218 for (int j = 0; j < iterationCount; j++) 219 { 220 if (exc != null) break; 221 222 teller.transfer(from,to,50); 223 teller.transfer(from,to,-50); 224 } 227 } catch (Exception e) 228 { 229 exc = e; 230 } 231 232 synchronized(lock) 233 { 234 iter--; 235 log.info("Only "+iter+" left"); 236 lock.notifyAll(); 237 } 238 } 239 }).start(); 240 } 241 242 synchronized(lock) 243 { 244 while(iter>0) 245 { 246 lock.wait(); 247 } 248 } 249 250 if (exc != null) throw exc; 251 252 long end = System.currentTimeMillis(); 253 254 getLog().info("Show balance"); 255 getLog().info(from.getPrimaryKey()+":"+from.getBalance()); 256 getLog().info(to.getPrimaryKey()+":"+to.getBalance()); 257 getLog().info("Time:"+(end-start)); 258 getLog().info("Avg. time/call(ms):"+((end-start)/(getThreadCount()*getIterationCount()*6))); 259 } 260 261 339 340 public void testTransaction() 341 throws Exception 342 { 343 TellerHome home = (TellerHome)PortableRemoteObject.narrow( 344 getInitialContext().lookup(TellerHome.JNDI_NAME), 345 TellerHome.class); 346 Teller teller = home.create(); 347 348 getLog().debug("Acquire customers"); 349 Customer marc = teller.getCustomer("Marc"); 350 getLog().debug("Marc acquired"); 351 Customer rickard = teller.getCustomer("Rickard"); 352 getLog().debug("Rickard acquired"); 353 354 getLog().debug("Acquire accounts"); 355 Account from = teller.getAccount(marc, 50); 356 Account to = teller.getAccount(rickard, 0); 357 358 getLog().debug("Show balance"); 359 getLog().debug(from.getPrimaryKey()+":"+from.getBalance()); 360 getLog().debug(to.getPrimaryKey()+":"+to.getBalance()); 361 362 getLog().debug("Transfer money"); 363 teller.transfer(from, to, 50); 364 getLog().debug("Transfer done"); 365 366 getLog().debug("Show balance"); 367 getLog().debug(from.getPrimaryKey()+"("+from.getOwner().getName()+"):"+from.getBalance()); 368 getLog().debug(to.getPrimaryKey()+"("+to.getOwner().getName()+"):"+to.getBalance()); 369 370 teller.remove(); 371 372 } 373 374 public void testTransfer() 375 throws Exception 376 { 377 378 TellerHome home = (TellerHome)PortableRemoteObject.narrow( 379 getInitialContext().lookup(TellerHome.JNDI_NAME), 380 TellerHome.class); 381 Teller teller = home.create(); 382 383 getLog().debug("Acquire customers"); 384 Customer marc = teller.getCustomer("Marc"); 385 getLog().debug("Marc acquired"); 386 Customer rickard = teller.getCustomer("Rickard"); 387 getLog().debug("Rickard acquired"); 388 389 getLog().debug("Acquire accounts"); 390 Account from = teller.getAccount(marc, 50*getIterationCount()); 391 Account to = teller.getAccount(rickard, 0); 392 393 getLog().debug("Show balance"); 394 getLog().debug(from.getPrimaryKey()+":"+from.getBalance()); 395 getLog().debug(to.getPrimaryKey()+":"+to.getBalance()); 396 397 getLog().info("Transfer money"); 398 long start = System.currentTimeMillis(); 399 teller.transferTest(from, to, 50, getIterationCount()); 400 long end = System.currentTimeMillis(); 401 getLog().info("Transfer done"); 402 getLog().info("Total time(ms):"+(end-start)); 403 getLog().info("Avg. time/call(ms):"+((end-start)/(getIterationCount()*2))); 404 405 getLog().debug("Show balance"); 406 getLog().debug(from.getPrimaryKey()+":"+from.getBalance()); 407 getLog().debug(to.getPrimaryKey()+":"+to.getBalance()); 408 409 teller.remove(); 410 } 411 412 public void testReadOnly() 413 throws Exception 414 { 415 416 TellerHome home = (TellerHome)PortableRemoteObject.narrow( 417 getInitialContext().lookup(TellerHome.JNDI_NAME), 418 TellerHome.class); 419 Teller teller = home.create(); 420 421 getLog().debug("Acquire customers"); 422 Customer marc = teller.getCustomer("Marc"); 423 getLog().debug("Marc acquired"); 424 Customer rickard = teller.getCustomer("Rickard"); 425 getLog().debug("Rickard acquired"); 426 427 getLog().debug("Acquire accounts"); 428 Account from = teller.getAccount(marc, 50*getIterationCount()); 429 Account to = teller.getAccount(rickard, 0); 430 431 getLog().info("Do read calls"); 432 long start = System.currentTimeMillis(); 433 for (int i = 0; i < getIterationCount(); i++) 434 { 435 marc.getName(); 436 from.getBalance(); 437 rickard.getName(); 438 to.getBalance(); 439 } 440 long end = System.currentTimeMillis(); 441 442 getLog().info("Calls done"); 443 getLog().info("Total time(ms):"+(end-start)); 444 getLog().info("Avg. time/call(ms):"+((end-start)/(getIterationCount()*4))); 445 446 teller.remove(); 447 } 448 449 public void testPassivation() 450 throws Exception 451 { 452 454 CustomerHome home = (CustomerHome)PortableRemoteObject.narrow( 455 getInitialContext().lookup(CustomerHome.JNDI_NAME), 456 CustomerHome.class); 457 458 getLog().info("Create customers"); 459 460 for (int i = 0; i < getIterationCount(); i++) 461 home.create(i+"", "Smith_"+i); 462 getLog().debug("Customers created"); 463 464 } 465 466 public void testFinder() 467 throws Exception 468 { 469 testPassivation(); 471 AccountHome home = (AccountHome)PortableRemoteObject.narrow( 472 getInitialContext().lookup(AccountHome.JNDI_NAME), 473 AccountHome.class); 474 475 getLog().info("Get large accounts"); 476 Iterator i = home.findLargeAccounts(-1).iterator(); 477 while (i.hasNext()) 478 { 479 Account acct = (Account)PortableRemoteObject.narrow(i.next(), 480 Account.class); 481 getLog().debug(acct.getOwner().getName()+":"+acct.getBalance()); 482 } 483 } 484 485 protected void setUp() 486 throws Exception 487 { 488 if (System.getSecurityManager() == null) 489 System.setSecurityManager(new java.rmi.RMISecurityManager ()); 490 getLog().info("Remove accounts"); 491 { 492 AccountHome home = (AccountHome)PortableRemoteObject.narrow( 493 getInitialContext().lookup(AccountHome.JNDI_NAME), 494 AccountHome.class); 495 Collection accounts = home.findAll(); 496 Iterator i = accounts.iterator(); 497 while(i.hasNext()) 498 { 499 Account acct = (Account)PortableRemoteObject.narrow(i.next(), 500 Account.class); 501 getLog().debug("Removing "+acct.getPrimaryKey()); 502 acct.remove(); 503 } 504 } 505 getLog().info("Remove customers"); 506 { 507 CustomerHome home = (CustomerHome)PortableRemoteObject.narrow( 508 getInitialContext().lookup(CustomerHome.JNDI_NAME), 509 CustomerHome.class); 510 Collection customers = home.findAll(); 511 Iterator i = customers.iterator(); 512 while(i.hasNext()) 513 { 514 Customer cust = 515 (Customer)PortableRemoteObject.narrow(i.next(), 516 Customer.class); 517 getLog().debug("Removing "+cust.getPrimaryKey()); 518 cust.remove(); 519 } 520 } 521 } 522 523 524 public static Test suite() throws Exception 525 { 526 return getDeploySetup(BankStressTestCase.class, "bankiiop.jar"); 527 } 528 529 530 531 } 532 | Popular Tags |