001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.bank.test;
023:
024: import java.util.*;
025: import java.lang.reflect.*;
026: import javax.ejb.*;
027: import javax.naming.*;
028: import javax.management.*;
029: import org.jboss.test.bank.interfaces.*;
030:
031: import junit.framework.Test;
032: import junit.framework.TestCase;
033: import junit.framework.TestSuite;
034:
035: import org.jboss.test.JBossTestCase;
036:
037: import org.apache.log4j.Category;
038:
039: /**
040: *
041: * @author Rickard Oberg
042: * @author Author: d_jencks among many others
043: * @version $Revision: 57211 $
044: */
045: public class BankStressTestCase extends JBossTestCase {
046: // Constants -----------------------------------------------------
047:
048: // Attributes ----------------------------------------------------
049: int idx = 1;
050: int iter;
051: Exception exc;
052:
053: // Static --------------------------------------------------------
054:
055: // Constructors --------------------------------------------------
056: public BankStressTestCase(String name) {
057: super (name);
058: }
059:
060: // Public --------------------------------------------------------
061: public void testTeller() throws Exception {
062: TellerHome home = (TellerHome) new InitialContext()
063: .lookup(TellerHome.JNDI_NAME);
064: Teller teller = home.create();
065:
066: BankHome bankHome = (BankHome) new InitialContext()
067: .lookup(BankHome.JNDI_NAME);
068: Bank bank = bankHome.create();
069:
070: getLog().debug("Acquire customers");
071: Customer marc = teller.getCustomer("Marc");
072: Customer rickard = teller.getCustomer("Rickard");
073:
074: getLog().debug("Acquire accounts");
075: Account from = teller.getAccount(marc, 200);
076: Account to = teller.getAccount(rickard, 200);
077:
078: getLog().debug("Show balance");
079: getLog().debug(from.getPrimaryKey() + ":" + from.getBalance());
080: getLog().debug(to.getPrimaryKey() + ":" + to.getBalance());
081:
082: getLog().debug("Transfer money");
083:
084: long start = System.currentTimeMillis();
085: int iter = 10;
086: for (int i = 0; i < iter; i++)
087: teller.transfer(from, to, 50);
088: long end = System.currentTimeMillis();
089: getLog().info(
090: "Average call time: " + ((end - start) / (iter * 6)));
091:
092: getLog().debug("Show balance");
093: AccountHome accountHome = (AccountHome) new InitialContext()
094: .lookup(AccountHome.JNDI_NAME);
095: Collection accts = accountHome.findAll();
096: Iterator i = accts.iterator();
097: while (i.hasNext()) {
098: Account acct = (Account) i.next();
099: AccountData data = acct.getData();
100: getLog().debug(
101: data.getId() + "(" + data.getOwner().getName()
102: + "):" + data.getBalance());
103: acct.withdraw(data.getBalance()); // Clear
104: }
105:
106: teller.remove();
107: }
108:
109: public void testBank() throws Exception {
110: getLog().debug("Get code");
111: BankHome bankHome = (BankHome) new InitialContext()
112: .lookup(BankHome.JNDI_NAME);
113:
114: Bank bank = bankHome.create();
115:
116: getLog().debug("Bank id=" + bank.getId());
117: bank.remove();
118: }
119:
120: /* public void testCustomer()
121: throws Exception
122: {
123: getLog().debug("Customer test----------------------------------");
124:
125: getLog().debug("Create Customer");
126: CustomerHome customerHome = (CustomerHome)new InitialContext().lookup("Customer");
127: Account from, to;
128: try
129: {
130: from = accountHome.findByPrimaryKey("Marc");
131: from.deposit(200);
132: } catch (FinderException e)
133: {
134: from = accountHome.create("Marc", 200);
135: }
136:
137: try
138: {
139: to = accountHome.findByPrimaryKey("Rickard");
140: } catch (FinderException e)
141: {
142: to = accountHome.create("Rickard", 0);
143: }
144:
145: getLog().debug("Show balance");
146: getLog().debug(from.getPrimaryKey()+":"+from.getBalance());
147: getLog().debug(to.getPrimaryKey()+":"+to.getBalance());
148:
149: getLog().debug("Transfer money");
150: TellerHome home = (TellerHome)new InitialContext().lookup("Teller");
151: Teller teller = home.create();
152:
153: long start = System.currentTimeMillis();
154: for (int i = 0; i < 100; i++)
155: teller.transfer(from, to, 50);
156: teller.remove();
157:
158: getLog().debug("Show balance");
159: Iterator enum = accountHome.findAll();
160: while(enum.hasNext())
161: {
162: Account acct = (Account)enum.next();
163: getLog().debug(acct.getPrimaryKey()+":"+acct.getBalance());
164: acct.withdraw(acct.getBalance()); // Clear
165: }
166: getLog().debug("Teller test done----------------------------------");
167: }
168: */
169:
170: public void testMultiThread() throws Exception {
171: TellerHome home = (TellerHome) new InitialContext()
172: .lookup(TellerHome.JNDI_NAME);
173: final Teller teller = home.create();
174:
175: getLog().debug("Acquire customers");
176: Customer marc = teller.getCustomer("Marc");
177: Customer rickard = teller.getCustomer("Rickard");
178:
179: getLog().debug("Acquire accounts");
180: final Account from = teller.getAccount(marc, 50);
181: final Account to = teller.getAccount(rickard, 0);
182:
183: final Object lock = new Object();
184:
185: iter = getThreadCount();
186: final int iterationCount = getIterationCount();
187: getLog().info(
188: "Start test. " + getThreadCount() + " threads, "
189: + getIterationCount() + " iterations");
190: long start = System.currentTimeMillis();
191:
192: for (int i = 0; i < getThreadCount(); i++) {
193: Thread.sleep(50);
194: new Thread(new Runnable() {
195: public void run() {
196: Category log = Category.getInstance(getClass()
197: .getName());
198:
199: try {
200:
201: for (int j = 0; j < iterationCount; j++) {
202: if (exc != null)
203: break;
204:
205: teller.transfer(from, to, 50);
206: teller.transfer(from, to, -50);
207: // Thread.currentThread().yield();
208: // logdebug(idx++);
209: }
210: } catch (Exception e) {
211: exc = e;
212: }
213:
214: synchronized (lock) {
215: iter--;
216: log.info("Only " + iter + " left");
217: lock.notifyAll();
218: }
219: }
220: }).start();
221: }
222:
223: synchronized (lock) {
224: while (iter > 0) {
225: lock.wait();
226: }
227: }
228:
229: if (exc != null)
230: throw exc;
231:
232: long end = System.currentTimeMillis();
233:
234: getLog().info("Show balance");
235: getLog().info(from.getPrimaryKey() + ":" + from.getBalance());
236: getLog().info(to.getPrimaryKey() + ":" + to.getBalance());
237: getLog().info("Time:" + (end - start));
238: getLog().info(
239: "Avg. time/call(ms):"
240: + ((end - start) / (getThreadCount()
241: * getIterationCount() * 6)));
242: }
243:
244: public void testMultiThread2() throws Exception {
245: TellerHome home = (TellerHome) new InitialContext()
246: .lookup(TellerHome.JNDI_NAME);
247: final Teller teller = home.create();
248:
249: getLog().debug("Acquire customers");
250:
251: final Customer marc = teller.getCustomer("Marc");
252: final Customer rickard = teller.getCustomer("Rickard");
253:
254: final Object lock = new Object();
255:
256: iter = getThreadCount();
257: final int iterationCount = getIterationCount();
258: getLog().info(
259: "Start test. " + getThreadCount() + " threads, "
260: + getIterationCount() + " iterations");
261: long start = System.currentTimeMillis();
262:
263: for (int i = 0; i < getThreadCount(); i++) {
264: Thread.sleep(500); // Wait between each client
265: new Thread(new Runnable() {
266: Category log = Category.getInstance(getClass()
267: .getName());
268:
269: public void run() {
270: try {
271:
272: Account from = teller.createAccount(marc, 50);
273: Account to = teller.createAccount(rickard, 0);
274:
275: for (int j = 0; j < iterationCount; j++) {
276: if (exc != null)
277: break;
278:
279: teller.transfer(from, to, 50);
280: teller.transfer(from, to, -50);
281: // Thread.currentThread().yield();
282: // log.debug(idx++);
283: }
284: } catch (Exception e) {
285: exc = e;
286: }
287:
288: synchronized (lock) {
289: iter--;
290: log.info("Only " + iter + " left");
291: lock.notifyAll();
292: }
293: }
294: }).start();
295: }
296:
297: synchronized (lock) {
298: while (iter > 0) {
299: lock.wait();
300: }
301: }
302:
303: if (exc != null)
304: throw exc;
305:
306: long end = System.currentTimeMillis();
307:
308: getLog().info("Time:" + (end - start));
309: getLog().info(
310: "Avg. time/call(ms):"
311: + ((end - start) / (getThreadCount()
312: * getIterationCount() * 6)));
313: }
314:
315: public void testTransaction() throws Exception {
316: TellerHome home = (TellerHome) new InitialContext()
317: .lookup(TellerHome.JNDI_NAME);
318: Teller teller = home.create();
319:
320: getLog().debug("Acquire customers");
321: Customer marc = teller.getCustomer("Marc");
322: getLog().debug("Marc acquired");
323: Customer rickard = teller.getCustomer("Rickard");
324: getLog().debug("Rickard acquired");
325:
326: getLog().debug("Acquire accounts");
327: Account from = teller.getAccount(marc, 50);
328: Account to = teller.getAccount(rickard, 0);
329:
330: getLog().debug("Show balance");
331: getLog().debug(from.getPrimaryKey() + ":" + from.getBalance());
332: getLog().debug(to.getPrimaryKey() + ":" + to.getBalance());
333:
334: getLog().debug("Transfer money");
335: teller.transfer(from, to, 50);
336: getLog().debug("Transfer done");
337:
338: getLog().debug("Show balance");
339: getLog().debug(
340: from.getPrimaryKey() + "(" + from.getOwner().getName()
341: + "):" + from.getBalance());
342: getLog().debug(
343: to.getPrimaryKey() + "(" + to.getOwner().getName()
344: + "):" + to.getBalance());
345:
346: teller.remove();
347:
348: }
349:
350: public void testTransfer() throws Exception {
351:
352: TellerHome home = (TellerHome) new InitialContext()
353: .lookup(TellerHome.JNDI_NAME);
354: Teller teller = home.create();
355:
356: getLog().debug("Acquire customers");
357: Customer marc = teller.getCustomer("Marc");
358: getLog().debug("Marc acquired");
359: Customer rickard = teller.getCustomer("Rickard");
360: getLog().debug("Rickard acquired");
361:
362: getLog().debug("Acquire accounts");
363: Account from = teller
364: .getAccount(marc, 50 * getIterationCount());
365: Account to = teller.getAccount(rickard, 0);
366:
367: getLog().debug("Show balance");
368: getLog().debug(from.getPrimaryKey() + ":" + from.getBalance());
369: getLog().debug(to.getPrimaryKey() + ":" + to.getBalance());
370:
371: getLog().info("Transfer money");
372: long start = System.currentTimeMillis();
373: teller.transferTest(from, to, 50, getIterationCount());
374: long end = System.currentTimeMillis();
375: getLog().info("Transfer done");
376: getLog().info("Total time(ms):" + (end - start));
377: getLog().info(
378: "Avg. time/call(ms):"
379: + ((end - start) / (getIterationCount() * 2)));
380:
381: getLog().debug("Show balance");
382: getLog().debug(from.getPrimaryKey() + ":" + from.getBalance());
383: getLog().debug(to.getPrimaryKey() + ":" + to.getBalance());
384:
385: teller.remove();
386: }
387:
388: public void testReadOnly() throws Exception {
389:
390: TellerHome home = (TellerHome) new InitialContext()
391: .lookup(TellerHome.JNDI_NAME);
392: Teller teller = home.create();
393:
394: getLog().debug("Acquire customers");
395: Customer marc = teller.getCustomer("Marc");
396: getLog().debug("Marc acquired");
397: Customer rickard = teller.getCustomer("Rickard");
398: getLog().debug("Rickard acquired");
399:
400: getLog().debug("Acquire accounts");
401: Account from = teller
402: .getAccount(marc, 50 * getIterationCount());
403: Account to = teller.getAccount(rickard, 0);
404:
405: getLog().info("Do read calls");
406: long start = System.currentTimeMillis();
407: for (int i = 0; i < getIterationCount(); i++) {
408: marc.getName();
409: from.getBalance();
410: rickard.getName();
411: to.getBalance();
412: }
413: long end = System.currentTimeMillis();
414:
415: getLog().info("Calls done");
416: getLog().info("Total time(ms):" + (end - start));
417: getLog().info(
418: "Avg. time/call(ms):"
419: + ((end - start) / (getIterationCount() * 4)));
420:
421: teller.remove();
422: }
423:
424: public void testPassivation() throws Exception {
425: // Create a bunch of customers, to test passivation
426:
427: CustomerHome home = (CustomerHome) new InitialContext()
428: .lookup(CustomerHome.JNDI_NAME);
429:
430: getLog().info("Create customers");
431:
432: for (int i = 0; i < getIterationCount(); i++)
433: home.create(i + "", "Smith_" + i);
434: getLog().debug("Customers created");
435:
436: }
437:
438: public void testFinder() throws Exception {
439: //create some accounts
440: testPassivation();
441: AccountHome home = (AccountHome) new InitialContext()
442: .lookup(AccountHome.JNDI_NAME);
443:
444: getLog().info("Get large accounts");
445: Iterator i = home.findLargeAccounts(-1).iterator();
446: while (i.hasNext()) {
447: Account acct = (Account) i.next();
448: getLog()
449: .debug(
450: acct.getOwner().getName() + ":"
451: + acct.getBalance());
452: }
453: }
454:
455: protected void setUp() throws Exception {
456: getLog().info("Remove accounts");
457: {
458: AccountHome home = (AccountHome) new InitialContext()
459: .lookup(AccountHome.JNDI_NAME);
460: Collection accounts = home.findAll();
461: Iterator i = accounts.iterator();
462: while (i.hasNext()) {
463: Account acct = (Account) i.next();
464: getLog().debug("Removing " + acct.getPrimaryKey());
465: acct.remove();
466: }
467: }
468: getLog().info("Remove customers");
469: {
470: CustomerHome home = (CustomerHome) new InitialContext()
471: .lookup(CustomerHome.JNDI_NAME);
472: Collection customers = home.findAll();
473: Iterator i = customers.iterator();
474: while (i.hasNext()) {
475: Customer cust = (Customer) i.next();
476: getLog().debug("Removing " + cust.getPrimaryKey());
477: cust.remove();
478: }
479: }
480: }
481:
482: public static Test suite() throws Exception {
483: return getDeploySetup(BankStressTestCase.class, "bank.jar");
484: }
485:
486: }
|