1 22 package org.jboss.test.bank.test; 23 24 import java.util.*; 25 import java.lang.reflect.*; 26 import javax.ejb.*; 27 import javax.naming.*; 28 import javax.management.*; 29 import org.jboss.test.bank.interfaces.*; 30 31 import junit.framework.Test; 32 import junit.framework.TestCase; 33 import junit.framework.TestSuite; 34 35 import org.jboss.test.JBossTestCase; 36 37 import org.apache.log4j.Category; 38 39 45 public class BankStressTestCase 46 extends JBossTestCase 47 { 48 50 int idx = 1; 52 int iter; 53 Exception exc; 54 55 57 public BankStressTestCase(String name) 59 { 60 super(name); 61 } 62 63 public void testTeller() 65 throws Exception 66 { 67 TellerHome home = (TellerHome)new InitialContext().lookup(TellerHome.JNDI_NAME); 68 Teller teller = home.create(); 69 70 BankHome bankHome = (BankHome)new InitialContext().lookup(BankHome.JNDI_NAME); 71 Bank bank = bankHome.create(); 72 73 getLog().debug("Acquire customers"); 74 Customer marc = teller.getCustomer("Marc"); 75 Customer rickard = teller.getCustomer("Rickard"); 76 77 getLog().debug("Acquire accounts"); 78 Account from = teller.getAccount(marc, 200); 79 Account to = teller.getAccount(rickard, 200); 80 81 getLog().debug("Show balance"); 82 getLog().debug(from.getPrimaryKey()+":"+from.getBalance()); 83 getLog().debug(to.getPrimaryKey()+":"+to.getBalance()); 84 85 getLog().debug("Transfer money"); 86 87 long start = System.currentTimeMillis(); 88 int iter = 10; 89 for (int i = 0; i < iter; i++) 90 teller.transfer(from, to, 50); 91 long end = System.currentTimeMillis(); 92 getLog().info("Average call time: "+((end - start) / (iter*6))); 93 94 getLog().debug("Show balance"); 95 AccountHome accountHome = (AccountHome)new InitialContext().lookup(AccountHome.JNDI_NAME); 96 Collection accts = accountHome.findAll(); 97 Iterator i = accts.iterator(); 98 while(i.hasNext()) 99 { 100 Account acct = (Account)i.next(); 101 AccountData data = acct.getData(); 102 getLog().debug(data.getId()+"("+data.getOwner().getName()+"):"+data.getBalance()); 103 acct.withdraw(data.getBalance()); } 105 106 teller.remove(); 107 } 108 109 public void testBank() 110 throws Exception 111 { 112 getLog().debug("Get code"); 113 BankHome bankHome = (BankHome)new InitialContext().lookup(BankHome.JNDI_NAME); 114 115 Bank bank = bankHome.create(); 116 117 getLog().debug("Bank id="+bank.getId()); 118 bank.remove(); 119 } 120 121 170 171 public void testMultiThread() 172 throws Exception 173 { 174 TellerHome home = (TellerHome)new InitialContext().lookup(TellerHome.JNDI_NAME); 175 final Teller teller = home.create(); 176 177 getLog().debug("Acquire customers"); 178 Customer marc = teller.getCustomer("Marc"); 179 Customer rickard = teller.getCustomer("Rickard"); 180 181 getLog().debug("Acquire accounts"); 182 final Account from = teller.getAccount(marc, 50); 183 final Account to = teller.getAccount(rickard, 0); 184 185 final Object lock = new Object (); 186 187 188 iter = getThreadCount(); 189 final int iterationCount = getIterationCount(); 190 getLog().info("Start test. "+getThreadCount()+ " threads, "+getIterationCount()+" iterations"); 191 long start = System.currentTimeMillis(); 192 193 for (int i = 0; i < getThreadCount(); i++) 194 { 195 Thread.sleep(50); 196 new Thread (new Runnable () 197 { 198 public void run() 199 { 200 Category log = Category.getInstance(getClass().getName()); 201 202 try 203 { 204 205 for (int j = 0; j < iterationCount; j++) 206 { 207 if (exc != null) break; 208 209 teller.transfer(from,to,50); 210 teller.transfer(from,to,-50); 211 } 214 } catch (Exception e) 215 { 216 exc = e; 217 } 218 219 synchronized(lock) 220 { 221 iter--; 222 log.info("Only "+iter+" left"); 223 lock.notifyAll(); 224 } 225 } 226 }).start(); 227 } 228 229 synchronized(lock) 230 { 231 while(iter>0) 232 { 233 lock.wait(); 234 } 235 } 236 237 if (exc != null) throw exc; 238 239 long end = System.currentTimeMillis(); 240 241 getLog().info("Show balance"); 242 getLog().info(from.getPrimaryKey()+":"+from.getBalance()); 243 getLog().info(to.getPrimaryKey()+":"+to.getBalance()); 244 getLog().info("Time:"+(end-start)); 245 getLog().info("Avg. time/call(ms):"+((end-start)/(getThreadCount()*getIterationCount()*6))); 246 } 247 248 public void testMultiThread2() 249 throws Exception 250 { 251 TellerHome home = (TellerHome)new InitialContext().lookup(TellerHome.JNDI_NAME); 252 final Teller teller = home.create(); 253 254 getLog().debug("Acquire customers"); 255 256 final Customer marc = teller.getCustomer("Marc"); 257 final Customer rickard = teller.getCustomer("Rickard"); 258 259 final Object lock = new Object (); 260 261 262 iter = getThreadCount(); 263 final int iterationCount = getIterationCount(); 264 getLog().info("Start test. "+getThreadCount()+ " threads, "+getIterationCount()+" iterations"); 265 long start = System.currentTimeMillis(); 266 267 for (int i = 0; i < getThreadCount(); i++) 268 { 269 Thread.sleep(500); new Thread (new Runnable () 271 { 272 Category log = Category.getInstance(getClass().getName()); 273 274 public void run() 275 { 276 try 277 { 278 279 Account from = teller.createAccount(marc, 50); 280 Account to = teller.createAccount(rickard, 0); 281 282 for (int j = 0; j < iterationCount; j++) 283 { 284 if (exc != null) break; 285 286 teller.transfer(from,to,50); 287 teller.transfer(from,to,-50); 288 } 291 } catch (Exception e) 292 { 293 exc = e; 294 } 295 296 synchronized(lock) 297 { 298 iter--; 299 log.info("Only "+iter+" left"); 300 lock.notifyAll(); 301 } 302 } 303 }).start(); 304 } 305 306 synchronized(lock) 307 { 308 while(iter>0) 309 { 310 lock.wait(); 311 } 312 } 313 314 if (exc != null) throw exc; 315 316 long end = System.currentTimeMillis(); 317 318 getLog().info("Time:"+(end-start)); 319 getLog().info("Avg. time/call(ms):"+((end-start)/(getThreadCount()*getIterationCount()*6))); 320 } 321 322 public void testTransaction() 323 throws Exception 324 { 325 TellerHome home = (TellerHome)new InitialContext().lookup(TellerHome.JNDI_NAME); 326 Teller teller = home.create(); 327 328 getLog().debug("Acquire customers"); 329 Customer marc = teller.getCustomer("Marc"); 330 getLog().debug("Marc acquired"); 331 Customer rickard = teller.getCustomer("Rickard"); 332 getLog().debug("Rickard acquired"); 333 334 getLog().debug("Acquire accounts"); 335 Account from = teller.getAccount(marc, 50); 336 Account to = teller.getAccount(rickard, 0); 337 338 getLog().debug("Show balance"); 339 getLog().debug(from.getPrimaryKey()+":"+from.getBalance()); 340 getLog().debug(to.getPrimaryKey()+":"+to.getBalance()); 341 342 getLog().debug("Transfer money"); 343 teller.transfer(from, to, 50); 344 getLog().debug("Transfer done"); 345 346 getLog().debug("Show balance"); 347 getLog().debug(from.getPrimaryKey()+"("+from.getOwner().getName()+"):"+from.getBalance()); 348 getLog().debug(to.getPrimaryKey()+"("+to.getOwner().getName()+"):"+to.getBalance()); 349 350 teller.remove(); 351 352 } 353 354 public void testTransfer() 355 throws Exception 356 { 357 358 TellerHome home = (TellerHome)new InitialContext().lookup(TellerHome.JNDI_NAME); 359 Teller teller = home.create(); 360 361 getLog().debug("Acquire customers"); 362 Customer marc = teller.getCustomer("Marc"); 363 getLog().debug("Marc acquired"); 364 Customer rickard = teller.getCustomer("Rickard"); 365 getLog().debug("Rickard acquired"); 366 367 getLog().debug("Acquire accounts"); 368 Account from = teller.getAccount(marc, 50*getIterationCount()); 369 Account to = teller.getAccount(rickard, 0); 370 371 getLog().debug("Show balance"); 372 getLog().debug(from.getPrimaryKey()+":"+from.getBalance()); 373 getLog().debug(to.getPrimaryKey()+":"+to.getBalance()); 374 375 getLog().info("Transfer money"); 376 long start = System.currentTimeMillis(); 377 teller.transferTest(from, to, 50, getIterationCount()); 378 long end = System.currentTimeMillis(); 379 getLog().info("Transfer done"); 380 getLog().info("Total time(ms):"+(end-start)); 381 getLog().info("Avg. time/call(ms):"+((end-start)/(getIterationCount()*2))); 382 383 getLog().debug("Show balance"); 384 getLog().debug(from.getPrimaryKey()+":"+from.getBalance()); 385 getLog().debug(to.getPrimaryKey()+":"+to.getBalance()); 386 387 teller.remove(); 388 } 389 390 public void testReadOnly() 391 throws Exception 392 { 393 394 TellerHome home = (TellerHome)new InitialContext().lookup(TellerHome.JNDI_NAME); 395 Teller teller = home.create(); 396 397 getLog().debug("Acquire customers"); 398 Customer marc = teller.getCustomer("Marc"); 399 getLog().debug("Marc acquired"); 400 Customer rickard = teller.getCustomer("Rickard"); 401 getLog().debug("Rickard acquired"); 402 403 getLog().debug("Acquire accounts"); 404 Account from = teller.getAccount(marc, 50*getIterationCount()); 405 Account to = teller.getAccount(rickard, 0); 406 407 getLog().info("Do read calls"); 408 long start = System.currentTimeMillis(); 409 for (int i = 0; i < getIterationCount(); i++) 410 { 411 marc.getName(); 412 from.getBalance(); 413 rickard.getName(); 414 to.getBalance(); 415 } 416 long end = System.currentTimeMillis(); 417 418 getLog().info("Calls done"); 419 getLog().info("Total time(ms):"+(end-start)); 420 getLog().info("Avg. time/call(ms):"+((end-start)/(getIterationCount()*4))); 421 422 teller.remove(); 423 } 424 425 public void testPassivation() 426 throws Exception 427 { 428 430 CustomerHome home = (CustomerHome)new InitialContext().lookup(CustomerHome.JNDI_NAME); 431 432 getLog().info("Create customers"); 433 434 for (int i = 0; i < getIterationCount(); i++) 435 home.create(i+"", "Smith_"+i); 436 getLog().debug("Customers created"); 437 438 } 439 440 public void testFinder() 441 throws Exception 442 { 443 testPassivation(); 445 AccountHome home = (AccountHome)new InitialContext().lookup(AccountHome.JNDI_NAME); 446 447 getLog().info("Get large accounts"); 448 Iterator i = home.findLargeAccounts(-1).iterator(); 449 while (i.hasNext()) 450 { 451 Account acct = (Account)i.next(); 452 getLog().debug(acct.getOwner().getName()+":"+acct.getBalance()); 453 } 454 } 455 456 protected void setUp() 457 throws Exception 458 { 459 super.setUp(); 460 getLog().info("Remove accounts"); 461 { 462 AccountHome home = (AccountHome)new InitialContext().lookup(AccountHome.JNDI_NAME); 463 Collection accounts = home.findAll(); 464 Iterator i = accounts.iterator(); 465 while(i.hasNext()) 466 { 467 Account acct = (Account)i.next(); 468 getLog().debug("Removing "+acct.getPrimaryKey()); 469 acct.remove(); 470 } 471 } 472 getLog().info("Remove customers"); 473 { 474 CustomerHome home = (CustomerHome)new InitialContext().lookup(CustomerHome.JNDI_NAME); 475 Collection customers = home.findAll(); 476 Iterator i = customers.iterator(); 477 while(i.hasNext()) 478 { 479 Customer cust = (Customer)i.next(); 480 getLog().debug("Removing "+cust.getPrimaryKey()); 481 cust.remove(); 482 } 483 } 484 } 485 486 487 public static Test suite() throws Exception 488 { 489 return getDeploySetup(BankStressTestCase.class, "bank.jar"); 490 } 491 492 493 494 } 495 | Popular Tags |