KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > pooled > test > BeanStressTestCase


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.pooled.test;
23
24 import java.rmi.*;
25
26
27 import javax.naming.Context JavaDoc;
28 import javax.naming.InitialContext JavaDoc;
29 import javax.ejb.DuplicateKeyException JavaDoc;
30 import javax.ejb.Handle JavaDoc;
31 import javax.ejb.EJBMetaData JavaDoc;
32 import javax.ejb.EJBHome JavaDoc;
33 import javax.ejb.HomeHandle JavaDoc;
34 import javax.ejb.ObjectNotFoundException JavaDoc;
35
36 import java.util.Date JavaDoc;
37 import java.util.Properties JavaDoc;
38 import java.util.Collection JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.Enumeration JavaDoc;
41
42 import junit.framework.Test;
43 import junit.framework.TestCase;
44 import junit.framework.TestSuite;
45
46 import org.jboss.test.pooled.interfaces.StatelessSessionHome;
47 import org.jboss.test.pooled.interfaces.StatelessSession;
48 import org.jboss.test.JBossTestCase;
49 import org.jboss.invocation.pooled.interfaces.PooledInvokerProxy;
50
51 /**
52 * Sample client for the jboss container.
53 *
54 * @author <a HREF="mailto:bill@burkecentral.com">Bill Burke</a>
55 * @version $Id: BeanStressTestCase.java 58115 2006-11-04 08:42:14Z scott.stark@jboss.org $
56 */

57 public class BeanStressTestCase
58    extends JBossTestCase
59 {
60    org.jboss.logging.Logger log = getLog();
61    
62    static boolean deployed = false;
63    static int test = 0;
64    static Date JavaDoc startDate = new Date JavaDoc();
65    public int NUM_THREADS = getThreadCount();
66    public int iterations = getIterationCount();
67    
68    protected final String JavaDoc namingFactory =
69    System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
70    
71    protected final String JavaDoc providerURL =
72    System.getProperty(Context.PROVIDER_URL);
73
74    public BeanStressTestCase(String JavaDoc name) {
75       super(name);
76    }
77    
78
79    boolean failed = false;
80
81    InitialContext JavaDoc ctx;
82
83    private StatelessSession getSession() throws Exception JavaDoc
84    {
85       
86       StatelessSessionHome home = (StatelessSessionHome)new InitialContext JavaDoc().lookup("nextgen.StatelessSession");
87       return home.create();
88    }
89
90
91    private class NewProxy implements Runnable JavaDoc
92    {
93       StatelessSessionHome home;
94       public NewProxy(StatelessSessionHome home)
95       {
96          this.home = home;
97       }
98       public void run()
99       {
100          for (int i = 0; i < iterations; i++)
101          {
102             try
103             {
104                StatelessSession obj = home.create();
105                obj.noop();
106                
107             }
108             catch(Throwable JavaDoc t)
109             {
110                //t.printStackTrace();
111
incFailures();
112             }
113          }
114       }
115    }
116
117
118    private class OldProxy implements Runnable JavaDoc
119    {
120       StatelessSession proxy;
121       public OldProxy(StatelessSession proxy)
122       {
123          this.proxy = proxy;
124       }
125       public void run()
126       {
127          for (int i = 0; i < iterations; i++)
128          {
129             try
130             {
131                proxy.noop();
132                
133             }
134             catch(Throwable JavaDoc t)
135             {
136                //t.printStackTrace();
137
incFailures();
138             }
139          }
140       }
141    }
142
143
144    protected int failures = 0;
145
146    public void testNewProxy() throws Exception JavaDoc
147    {
148       ctx = new InitialContext JavaDoc();
149       for (int i = 0; i < 2; i++)
150       {
151          runNewProxy("PooledStatelessSession");
152          System.out.println("usedPooled: " + PooledInvokerProxy.usedPooled);
153          PooledInvokerProxy.usedPooled = 0;
154          runNewProxy("StatelessSession");
155       }
156    }
157    /**
158     * Creates proxy at every invocation.
159     */

160    protected void runNewProxy(String JavaDoc ejbname) throws Exception JavaDoc
161    {
162       failures = 0;
163       System.out.println("------------------------");
164       System.out.println("**** NewProxy" + ejbname + " ****");
165       
166       StatelessSessionHome home = (StatelessSessionHome)ctx.lookup(ejbname);
167       Thread JavaDoc[] threads = new Thread JavaDoc[NUM_THREADS];
168       for (int i = 0; i < NUM_THREADS; i++)
169       {
170          threads[i] = new Thread JavaDoc(new NewProxy(home));
171       }
172       long start = System.currentTimeMillis();
173       for (int i = 0; i < NUM_THREADS; i++)
174       {
175          threads[i].start();
176       }
177       for (int i = 0; i < NUM_THREADS; i++)
178       {
179          threads[i].join();
180       }
181       System.out.println("time: " + (System.currentTimeMillis() - start));
182       System.out.println("failures: " + failures);
183    }
184
185    public void testOldProxy() throws Exception JavaDoc
186    {
187       ctx = new InitialContext JavaDoc();
188       for (int i = 0; i < 2; i++)
189       {
190          runOldProxy("PooledStatelessSession");
191          System.out.println("usedPooled: " + PooledInvokerProxy.usedPooled);
192          PooledInvokerProxy.usedPooled = 0;
193          runOldProxy("StatelessSession");
194       }
195    }
196    /**
197     * Creates bean proxy before timing and shares it between threads
198     */

199    protected void runOldProxy(String JavaDoc ejbname) throws Exception JavaDoc
200    {
201       failures = 0;
202       System.out.println("------------------------");
203       System.out.println("**** OldProxy " + ejbname + " ****");
204       
205       StatelessSessionHome home = (StatelessSessionHome)ctx.lookup(ejbname);
206       StatelessSession proxy = home.create();
207       Thread JavaDoc[] threads = new Thread JavaDoc[NUM_THREADS];
208       for (int i = 0; i < NUM_THREADS; i++)
209       {
210          threads[i] = new Thread JavaDoc(new OldProxy(proxy));
211       }
212       long start = System.currentTimeMillis();
213       for (int i = 0; i < NUM_THREADS; i++)
214       {
215          threads[i].start();
216       }
217       for (int i = 0; i < NUM_THREADS; i++)
218       {
219          threads[i].join();
220       }
221       System.out.println("time: " + (System.currentTimeMillis() - start));
222       System.out.println("failures: " + failures);
223    }
224
225    protected synchronized void incFailures()
226    {
227       failures++;
228    }
229
230    public static Test suite() throws Exception JavaDoc
231    {
232       return getDeploySetup(BeanStressTestCase.class, "pooled.jar");
233    }
234
235 }
236
Popular Tags