User: d_jencks Date: 01/09/11 21:55:38 Added: src/main/org/jboss/test/bank/test BankStressTestCase.java Removed: src/main/org/jboss/test/bank/test Main.java Log: Changed naming scheme of tests to *UnitTestCase.java for short running tests and *StressTestCase.java for lengthy tests. Made tests-unit and tests-stress targets in build.xml Revision Changes Path 1.1 jbosstest/src/main/org/jboss/test/bank/test/BankStressTestCase.java Index: BankStressTestCase.java =================================================================== /* * Copyright 1999 by dreamBean Software, * All rights reserved. */ package org.jboss.test.bank.test; import java.util.*; import java.lang.reflect.*; import javax.ejb.*; import javax.naming.*; import javax.management.*; import org.jboss.test.bank.interfaces.*; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; import org.jboss.test.util.Deploy; /** * * @see <related> * @author $Author: d_jencks $ * @version $Revision: 1.1 $ */ public class BankStressTestCase extends TestCase { // Constants ----------------------------------------------------- // Attributes ---------------------------------------------------- int idx = 1; int iter; Exception exc; // Static -------------------------------------------------------- static boolean deployed = false; // Constructors -------------------------------------------------- public BankStressTestCase(String name) { super(name); } // Public -------------------------------------------------------- public void testTeller() throws Exception { TellerHome home = (TellerHome)new InitialContext().lookup(TellerHome.JNDI_NAME); Teller teller = home.create(); BankHome bankHome = (BankHome)new InitialContext().lookup(BankHome.JNDI_NAME); Bank bank = bankHome.create(); System.out.println("Acquire customers"); Customer marc = teller.getCustomer("Marc"); Customer rickard = teller.getCustomer("Rickard"); System.out.println("Acquire accounts"); Account from = teller.getAccount(marc, 200); Account to = teller.getAccount(rickard, 200); System.out.println("Show balance"); System.out.println(from.getPrimaryKey()+":"+from.getBalance()); System.out.println(to.getPrimaryKey()+":"+to.getBalance()); System.out.println("Transfer money"); long start = System.currentTimeMillis(); int iter = 10; for (int i = 0; i < iter; i++) teller.transfer(from, to, 50); long end = System.currentTimeMillis(); System.out.println("Average call time: "+((end - start) / (iter*6))); System.out.println("Show balance"); AccountHome accountHome = (AccountHome)new InitialContext().lookup(AccountHome.JNDI_NAME); Collection accts = accountHome.findAll(); Iterator enum = accts.iterator(); while(enum.hasNext()) { Account acct = (Account)enum.next(); AccountData data = acct.getData(); System.out.println(data.getId()+"("+data.getOwner().getName()+"):"+data.getBalance()); acct.withdraw(data.getBalance()); // Clear } teller.remove(); } public void testBank() throws Exception { System.out.println("Get code"); BankHome bankHome = (BankHome)new InitialContext().lookup(BankHome.JNDI_NAME); Bank bank = bankHome.create(); System.out.println("Bank id="+bank.getId()); bank.remove(); } /* public void testCustomer() throws Exception { System.out.println("Customer test----------------------------------"); System.out.println("Create Customer"); CustomerHome customerHome = (CustomerHome)new InitialContext().lookup("Customer"); Account from, to; try { from = accountHome.findByPrimaryKey("Marc"); from.deposit(200); } catch (FinderException e) { from = accountHome.create("Marc", 200); } try { to = accountHome.findByPrimaryKey("Rickard"); } catch (FinderException e) { to = accountHome.create("Rickard", 0); } System.out.println("Show balance"); System.out.println(from.getPrimaryKey()+":"+from.getBalance()); System.out.println(to.getPrimaryKey()+":"+to.getBalance()); System.out.println("Transfer money"); TellerHome home = (TellerHome)new InitialContext().lookup("Teller"); Teller teller = home.create(); long start = System.currentTimeMillis(); for (int i = 0; i < 100; i++) teller.transfer(from, to, 50); teller.remove(); System.out.println("Show balance"); Iterator enum = accountHome.findAll(); while(enum.hasNext()) { Account acct = (Account)enum.next(); System.out.println(acct.getPrimaryKey()+":"+acct.getBalance()); acct.withdraw(acct.getBalance()); // Clear } System.out.println("Teller test done----------------------------------"); } */ public void testMultiThread() throws Exception { TellerHome home = (TellerHome)new InitialContext().lookup(TellerHome.JNDI_NAME); final Teller teller = home.create(); System.out.println("Acquire customers"); Customer marc = teller.getCustomer("Marc"); Customer rickard = teller.getCustomer("Rickard"); System.out.println("Acquire accounts"); final Account from = teller.getAccount(marc, 50); final Account to = teller.getAccount(rickard, 0); final Object lock = new Object(); final int threadCount = 10; final int threadIterations = 10; iter = threadCount; System.out.println("Start test. "+threadCount+ " threads, "+threadIterations+" iterations"); long start = System.currentTimeMillis(); for (int i = 0; i < threadCount; i++) { Thread.sleep(50); new Thread(new Runnable() { public void run() { try { for (int j = 0; j < threadIterations; j++) { if (exc != null) break; teller.transfer(from,to,50); teller.transfer(from,to,-50); // Thread.currentThread().yield(); // System.out.println(idx++); } } catch (Exception e) { exc = e; } synchronized(lock) { iter--; System.out.println("Only "+iter+" left"); lock.notifyAll(); } } }).start(); } synchronized(lock) { while(iter>0) { lock.wait(); } } if (exc != null) throw exc; long end = System.currentTimeMillis(); System.out.println("Show balance"); System.out.println(from.getPrimaryKey()+":"+from.getBalance()); System.out.println(to.getPrimaryKey()+":"+to.getBalance()); System.out.println("Time:"+(end-start)); System.out.println("Avg. time/call(ms):"+((end-start)/(threadCount*threadIterations*6))); } public void testMultiThread2() throws Exception { TellerHome home = (TellerHome)new InitialContext().lookup(TellerHome.JNDI_NAME); final Teller teller = home.create(); System.out.println("Acquire customers"); final Customer marc = teller.getCustomer("Marc"); final Customer rickard = teller.getCustomer("Rickard"); final Object lock = new Object(); final int threadCount = 50; final int threadIterations = 100; iter = threadCount; System.out.println("Start test. "+threadCount+ " threads, "+threadIterations+" iterations"); long start = System.currentTimeMillis(); for (int i = 0; i < threadCount; i++) { Thread.sleep(500); // Wait between each client new Thread(new Runnable() { public void run() { try { Account from = teller.createAccount(marc, 50); Account to = teller.createAccount(rickard, 0); for (int j = 0; j < threadIterations; j++) { if (exc != null) break; teller.transfer(from,to,50); teller.transfer(from,to,-50); // Thread.currentThread().yield(); // System.out.println(idx++); } } catch (Exception e) { exc = e; } synchronized(lock) { iter--; System.out.println("Only "+iter+" left"); lock.notifyAll(); } } }).start(); } synchronized(lock) { while(iter>0) { lock.wait(); } } if (exc != null) throw exc; long end = System.currentTimeMillis(); System.out.println("Time:"+(end-start)); System.out.println("Avg. time/call(ms):"+((end-start)/(threadCount*threadIterations*6))); } public void testTransaction() throws Exception { TellerHome home = (TellerHome)new InitialContext().lookup(TellerHome.JNDI_NAME); Teller teller = home.create(); System.out.println("Acquire customers"); Customer marc = teller.getCustomer("Marc"); System.out.println("Marc acquired"); Customer rickard = teller.getCustomer("Rickard"); System.out.println("Rickard acquired"); System.out.println("Acquire accounts"); Account from = teller.getAccount(marc, 50); Account to = teller.getAccount(rickard, 0); System.out.println("Show balance"); System.out.println(from.getPrimaryKey()+":"+from.getBalance()); System.out.println(to.getPrimaryKey()+":"+to.getBalance()); System.out.println("Transfer money"); teller.transfer(from, to, 50); System.out.println("Transfer done"); System.out.println("Show balance"); System.out.println(from.getPrimaryKey()+"("+from.getOwner().getName()+"):"+from.getBalance()); System.out.println(to.getPrimaryKey()+"("+to.getOwner().getName()+"):"+to.getBalance()); teller.remove(); } public void testTransfer() throws Exception { // Nr of transfers iter = 100; TellerHome home = (TellerHome)new InitialContext().lookup(TellerHome.JNDI_NAME); Teller teller = home.create(); System.out.println("Acquire customers"); Customer marc = teller.getCustomer("Marc"); System.out.println("Marc acquired"); Customer rickard = teller.getCustomer("Rickard"); System.out.println("Rickard acquired"); System.out.println("Acquire accounts"); Account from = teller.getAccount(marc, 50*iter); Account to = teller.getAccount(rickard, 0); System.out.println("Show balance"); System.out.println(from.getPrimaryKey()+":"+from.getBalance()); System.out.println(to.getPrimaryKey()+":"+to.getBalance()); System.out.println("Transfer money"); long start = System.currentTimeMillis(); teller.transferTest(from, to, 50, iter); long end = System.currentTimeMillis(); System.out.println("Transfer done"); System.out.println("Total time(ms):"+(end-start)); System.out.println("Avg. time/call(ms):"+((end-start)/(iter*2))); System.out.println("Show balance"); System.out.println(from.getPrimaryKey()+":"+from.getBalance()); System.out.println(to.getPrimaryKey()+":"+to.getBalance()); teller.remove(); } public void testReadOnly() throws Exception { // Nr of transfers iter = 100; TellerHome home = (TellerHome)new InitialContext().lookup(TellerHome.JNDI_NAME); Teller teller = home.create(); System.out.println("Acquire customers"); Customer marc = teller.getCustomer("Marc"); System.out.println("Marc acquired"); Customer rickard = teller.getCustomer("Rickard"); System.out.println("Rickard acquired"); System.out.println("Acquire accounts"); Account from = teller.getAccount(marc, 50*iter); Account to = teller.getAccount(rickard, 0); System.out.println("Do read calls"); long start = System.currentTimeMillis(); for (int i = 0; i < iter; i++) { marc.getName(); from.getBalance(); rickard.getName(); to.getBalance(); } long end = System.currentTimeMillis(); System.out.println("Calls done"); System.out.println("Total time(ms):"+(end-start)); System.out.println("Avg. time/call(ms):"+((end-start)/(iter*4))); teller.remove(); } public void testPassivation() throws Exception { // Create a bunch of customers, to test passivation iter = 200; CustomerHome home = (CustomerHome)new InitialContext().lookup(CustomerHome.JNDI_NAME); System.out.println("Create customers"); for (int i = 0; i < iter; i++) home.create(i+"", "Smith_"+i); System.out.println("Customers created"); } public void testFinder() throws Exception { AccountHome home = (AccountHome)new InitialContext().lookup(AccountHome.JNDI_NAME); System.out.println("Get large accounts"); Iterator enum = home.findLargeAccounts(-1).iterator(); while (enum.hasNext()) { Account acct = (Account)enum.next(); System.out.println(acct.getOwner().getName()+":"+acct.getBalance()); } } protected void setUp() throws Exception { if (deployed) return; // System.out.println("Deploying"); // new org.jboss.jmx.client.Deployer().deploy("../deploy/bank.jar"); deployed = true; System.out.println("Remove accounts"); { AccountHome home = (AccountHome)new InitialContext().lookup(AccountHome.JNDI_NAME); Collection accounts = home.findAll(); Iterator enum = accounts.iterator(); while(enum.hasNext()) { Account acct = (Account)enum.next(); System.out.println("Removing "+acct.getPrimaryKey()); acct.remove(); } } System.out.println("Remove customers"); { CustomerHome home = (CustomerHome)new InitialContext().lookup(CustomerHome.JNDI_NAME); Collection customers = home.findAll(); Iterator enum = customers.iterator(); while(enum.hasNext()) { Customer cust = (Customer)enum.next(); System.out.println("Removing "+cust.getPrimaryKey()); cust.remove(); } } } /** * Setup the test suite. */ public static Test suite() { TestSuite suite = new TestSuite(); // add a test case to deploy our support applications String filename = "bank.jar"; suite.addTest(new Deploy.Deployer(filename)); suite.addTest(new TestSuite(BankStressTestCase.class)); // add a test case to undeploy our support applications suite.addTest(new Deploy.Undeployer(filename)); return suite; } } _______________________________________________ Jboss-development mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/jboss-development