KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > bank > test > BankStressTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

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 /**
40  *
41  * @see <related>
42  * @author Author: d_jencks among many others
43  * @version $Revision: 58364 $
44  */

45 public class BankStressTestCase
46    extends JBossTestCase
47 {
48    // Constants -----------------------------------------------------
49

50    // Attributes ----------------------------------------------------
51
int idx = 1;
52    int iter;
53    Exception JavaDoc exc;
54    
55    // Static --------------------------------------------------------
56

57    // Constructors --------------------------------------------------
58
public BankStressTestCase(String JavaDoc name)
59     {
60         super(name);
61     }
62    
63    // Public --------------------------------------------------------
64
public void testTeller()
65       throws Exception JavaDoc
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()); // Clear
104
}
105       
106       teller.remove();
107    }
108    
109    public void testBank()
110       throws Exception JavaDoc
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 /* public void testCustomer()
122       throws Exception
123    {
124       getLog().debug("Customer test----------------------------------");
125       
126       getLog().debug("Create Customer");
127       CustomerHome customerHome = (CustomerHome)new InitialContext().lookup("Customer");
128       Account from, to;
129       try
130       {
131          from = accountHome.findByPrimaryKey("Marc");
132          from.deposit(200);
133       } catch (FinderException e)
134       {
135          from = accountHome.create("Marc", 200);
136       }
137       
138       try
139       {
140          to = accountHome.findByPrimaryKey("Rickard");
141       } catch (FinderException e)
142       {
143          to = accountHome.create("Rickard", 0);
144       }
145       
146       getLog().debug("Show balance");
147       getLog().debug(from.getPrimaryKey()+":"+from.getBalance());
148       getLog().debug(to.getPrimaryKey()+":"+to.getBalance());
149
150       getLog().debug("Transfer money");
151       TellerHome home = (TellerHome)new InitialContext().lookup("Teller");
152       Teller teller = home.create();
153       
154       long start = System.currentTimeMillis();
155       for (int i = 0; i < 100; i++)
156          teller.transfer(from, to, 50);
157       teller.remove();
158       
159       getLog().debug("Show balance");
160       Iterator enum = accountHome.findAll();
161       while(enum.hasNext())
162       {
163          Account acct = (Account)enum.next();
164          getLog().debug(acct.getPrimaryKey()+":"+acct.getBalance());
165          acct.withdraw(acct.getBalance()); // Clear
166       }
167       getLog().debug("Teller test done----------------------------------");
168    }
169 */

170
171    public void testMultiThread()
172       throws Exception JavaDoc
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 JavaDoc lock = new Object JavaDoc();
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 JavaDoc(new Runnable JavaDoc()
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 // Thread.currentThread().yield();
212
// logdebug(idx++);
213
}
214                } catch (Exception JavaDoc 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 JavaDoc
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 JavaDoc lock = new Object JavaDoc();
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); // Wait between each client
270
new Thread JavaDoc(new Runnable JavaDoc()
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 // Thread.currentThread().yield();
289
// log.debug(idx++);
290
}
291                } catch (Exception JavaDoc 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 JavaDoc
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 JavaDoc
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 JavaDoc
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 JavaDoc
427    {
428       // Create a bunch of customers, to test passivation
429

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 JavaDoc
442    {
443       //create some accounts
444
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 JavaDoc
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 JavaDoc
488    {
489       return getDeploySetup(BankStressTestCase.class, "bank.jar");
490    }
491
492
493
494 }
495
Popular Tags