KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > bankiiop > 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.bankiiop.test;
23
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import javax.rmi.PortableRemoteObject JavaDoc;
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 /**
42  *
43  * @author Rickard Oberg
44  * @author Author: d_jencks among many others
45  * @version $Revision: 58115 $
46  */

47 public class BankStressTestCase
48    extends JBossIIOPTestCase
49 {
50    // Constants -----------------------------------------------------
51

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

59    // Constructors --------------------------------------------------
60
public BankStressTestCase(String JavaDoc name)
61    {
62       super(name);
63    }
64     
65    // Public --------------------------------------------------------
66
public void testTeller()
67       throws Exception JavaDoc
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 JavaDoc accts = accountHome.findAll();
105       Iterator JavaDoc 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()); // Clear
113
}
114       
115       teller.remove();
116    }
117    
118    public void testBank()
119       throws Exception JavaDoc
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 /* public void testCustomer()
133       throws Exception
134    {
135       getLog().debug("Customer test----------------------------------");
136       
137       getLog().debug("Create Customer");
138       CustomerHome customerHome = (CustomerHome)getInitialContext().lookup("Customer");
139       Account from, to;
140       try
141       {
142          from = accountHome.findByPrimaryKey("Marc");
143          from.deposit(200);
144       } catch (FinderException e)
145       {
146          from = accountHome.create("Marc", 200);
147       }
148       
149       try
150       {
151          to = accountHome.findByPrimaryKey("Rickard");
152       } catch (FinderException e)
153       {
154          to = accountHome.create("Rickard", 0);
155       }
156       
157       getLog().debug("Show balance");
158       getLog().debug(from.getPrimaryKey()+":"+from.getBalance());
159       getLog().debug(to.getPrimaryKey()+":"+to.getBalance());
160
161       getLog().debug("Transfer money");
162       TellerHome home = (TellerHome)getInitialContext().lookup("Teller");
163       Teller teller = home.create();
164       
165       long start = System.currentTimeMillis();
166       for (int i = 0; i < 100; i++)
167          teller.transfer(from, to, 50);
168       teller.remove();
169       
170       getLog().debug("Show balance");
171       Iterator enum = accountHome.findAll();
172       while(enum.hasNext())
173       {
174          Account acct = (Account)enum.next();
175          getLog().debug(acct.getPrimaryKey()+":"+acct.getBalance());
176          acct.withdraw(acct.getBalance()); // Clear
177       }
178       getLog().debug("Teller test done----------------------------------");
179    }
180 */

181
182    public void testMultiThread()
183       throws Exception JavaDoc
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 JavaDoc lock = new Object JavaDoc();
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 JavaDoc(new Runnable JavaDoc()
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 // Thread.currentThread().yield();
225
// logdebug(idx++);
226
}
227                } catch (Exception JavaDoc 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    /*--- commented out until I understant why this test fails with jdk 1.4 ----
262
263    public void testMultiThread2()
264       throws Exception
265    {
266       TellerHome home = (TellerHome)PortableRemoteObject.narrow(
267                              getInitialContext().lookup(TellerHome.JNDI_NAME),
268                              TellerHome.class);
269       final Teller teller = home.create();
270       
271       getLog().debug("Acquire customers");
272
273       final Customer marc = teller.getCustomer("Marc");
274       final Customer rickard = teller.getCustomer("Rickard");
275       
276       final Object lock = new Object();
277    
278       
279       iter = getThreadCount();
280       final int iterationCount = getIterationCount();
281       getLog().info("Start test. "+getThreadCount()+ " threads, "+getIterationCount()+" iterations");
282       long start = System.currentTimeMillis();
283
284       for (int i = 0; i < getThreadCount(); i++)
285       {
286          Thread.sleep(500); // Wait between each client
287          new Thread(new Runnable()
288          {
289             Logger log = Logger.getLogger(getClass().getName());
290
291             public void run()
292             {
293                try
294                {
295                   
296                   Account from = teller.createAccount(marc, 50);
297                   Account to = teller.createAccount(rickard, 0);
298                   
299                   for (int j = 0; j < iterationCount; j++)
300                   {
301                      if (exc != null) break;
302                      
303                      teller.transfer(from,to,50);
304                      teller.transfer(from,to,-50);
305 // Thread.currentThread().yield();
306 // log.debug(idx++);
307                   }
308                } catch (Exception e)
309                {
310                   exc = e;
311                }
312                
313                synchronized(lock)
314                {
315                   iter--;
316                   log.info("Only "+iter+" left");
317                   lock.notifyAll();
318                }
319             }
320          }).start();
321       }
322       
323       synchronized(lock)
324       {
325          while(iter>0)
326          {
327             lock.wait();
328          }
329       }
330       
331       if (exc != null) throw exc;
332       
333       long end = System.currentTimeMillis();
334       
335       getLog().info("Time:"+(end-start));
336       getLog().info("Avg. time/call(ms):"+((end-start)/(getThreadCount()*getIterationCount()*6)));
337    }
338    --------------------------------------------------------------------------*/

339    
340    public void testTransaction()
341       throws Exception JavaDoc
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 JavaDoc
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 JavaDoc
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 JavaDoc
451    {
452       // Create a bunch of customers, to test passivation
453

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 JavaDoc
468    {
469       //create some accounts
470
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 JavaDoc 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 JavaDoc
487    {
488       if (System.getSecurityManager() == null)
489          System.setSecurityManager(new java.rmi.RMISecurityManager JavaDoc());
490       getLog().info("Remove accounts");
491       {
492          AccountHome home = (AccountHome)PortableRemoteObject.narrow(
493                             getInitialContext().lookup(AccountHome.JNDI_NAME),
494                             AccountHome.class);
495          Collection JavaDoc accounts = home.findAll();
496          Iterator JavaDoc 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 JavaDoc customers = home.findAll();
511          Iterator JavaDoc 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 JavaDoc
525    {
526       return getDeploySetup(BankStressTestCase.class, "bankiiop.jar");
527    }
528
529
530
531 }
532
Popular Tags