KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > bench > servlet > EJBTester


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.bench.servlet;
23
24 import java.util.ArrayList JavaDoc;
25
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27
28 import javax.naming.Context JavaDoc;
29 import javax.naming.InitialContext JavaDoc;
30
31 import org.jboss.test.bench.interfaces.MySession;
32 import org.jboss.test.bench.interfaces.MySessionHome;
33 import org.jboss.test.bench.interfaces.SimpleEntity;
34 import org.jboss.test.bench.interfaces.SimpleEntityHome;
35 import org.jboss.test.bench.interfaces.ComplexEntity;
36 import org.jboss.test.bench.interfaces.ComplexEntityHome;
37 import org.jboss.test.bench.interfaces.AComplexPK;
38
39
40
41 public class EJBTester {
42    org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(getClass());
43    
44     int maxClients;
45     Context JavaDoc ctx;
46     
47     HttpServletRequest JavaDoc req;
48
49     // only the "depth" first items of this array will be used
50
public static final int nbClients[] = { 1, 10, 50, 100, 200, 500 };
51     public int depth;
52     public int nbTests = 0;
53     int nbCalls;
54     int dataSize = 1024;
55
56     ArrayList JavaDoc testNames = new ArrayList JavaDoc();
57     ArrayList JavaDoc testResults = new ArrayList JavaDoc();
58
59     public EJBTester(HttpServletRequest JavaDoc req) {
60         
61         maxClients = Integer.parseInt(req.getParameter("maxClients"));
62         nbCalls = Integer.parseInt(req.getParameter("nbCalls"));
63         
64         this.req = req;
65
66         depth = nbClients.length;
67         for (int i = 0; i< nbClients.length; i++) if (nbClients[i] > maxClients) {
68             depth = i;
69             break;
70         }
71
72         try {
73             
74             System.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
75             System.setProperty("java.naming.provider.url","localhost");
76             System.setProperty("java.naming.factory.url.pkgs","org.jboss.naming;");
77             
78             ctx = new InitialContext JavaDoc();
79             
80         } catch (Exception JavaDoc e) {
81             log.debug("failed", e);
82         }
83     }
84
85     public String JavaDoc getTestName(int i) {
86         return (String JavaDoc)testNames.get(i);
87     }
88
89     public float getTestResult(int i, int j) {
90         return ((float[])testResults.get(i))[j];
91     }
92
93     public void test() {
94         try {
95             if (req.getParameter("createSimpleEntity") != null) {
96                 SimpleEntityHome home;
97                 float[] result;
98                 
99                 home = (SimpleEntityHome)ctx.lookup("SimpleEntity");
100                 result = testSimpleCreateEntity(home);
101                 testNames.add("Simple Entity Bean creation (optimized)");
102                 testResults.add(result);
103                 nbTests++;
104                 
105                 home = (SimpleEntityHome)ctx.lookup("NonOptSimpleEntity");
106                 result = testSimpleCreateEntity(home);
107                 testNames.add("Simple Entity Bean creation (serialized)");
108                 testResults.add(result);
109                 nbTests++;
110             }
111             if (req.getParameter("createComplexEntity") != null) {
112                 ComplexEntityHome home;
113                 float[] result;
114                 
115                 home = (ComplexEntityHome)ctx.lookup("ComplexEntity");
116                 result = testComplexCreateEntity(home);
117                 testNames.add("Complex Entity Bean creation (optimized)");
118                 testResults.add(result);
119                 nbTests++;
120                 
121                 home = (ComplexEntityHome)ctx.lookup("NonOptComplexEntity");
122                 result = testComplexCreateEntity(home);
123                 testNames.add("Complex Entity Bean creation (serialized)");
124                 testResults.add(result);
125                 nbTests++;
126             }
127             if (req.getParameter("readEntity") != null) {
128                 SimpleEntityHome home;
129                 float[] result;
130                 
131                 home = (SimpleEntityHome)ctx.lookup("SimpleEntity");
132                 result = readEntity(home);
133                 testNames.add("Read-only call on an entity bean (optimized)");
134                 testResults.add(result);
135                 nbTests++;
136                 
137                 home = (SimpleEntityHome)ctx.lookup("NonOptSimpleEntity");
138                 result = readEntity(home);
139                 testNames.add("Read-only call on an entity bean (serialized)");
140                 testResults.add(result);
141                 nbTests++;
142             }
143             if (req.getParameter("writeEntity") != null) {
144                 ComplexEntityHome home;
145                 float[] result;
146                 
147                 home = (ComplexEntityHome)ctx.lookup("ComplexEntity");
148                 result = writeEntity(home);
149                 testNames.add("Write call to entity (optimized)");
150                 testResults.add(result);
151                 nbTests++;
152                 
153                 home = (ComplexEntityHome)ctx.lookup("NonOptComplexEntity");
154                 result = writeEntity(home);
155                 testNames.add("Write call to entity (serialized)");
156                 testResults.add(result);
157                 nbTests++;
158             }
159             if (req.getParameter("callSF") != null) {
160                 MySessionHome home;
161                 float[] result;
162                 
163                 home = (MySessionHome)ctx.lookup("StatefulSession");
164                 result = callSession(home);
165                 testNames.add("Call to stateful session (optimized)");
166                 testResults.add(result);
167                 nbTests++;
168             }
169             if (req.getParameter("callSL") != null) {
170                 MySessionHome home;
171                 float[] result;
172                                 
173                 home = (MySessionHome)ctx.lookup("StatelessSession");
174                 result = callSession(home);
175                 testNames.add("Call to stateless session (optimized)");
176                 testResults.add(result);
177                 nbTests++;
178             }
179             
180         } catch (Exception JavaDoc e) {
181             log.debug("failed", e);
182         }
183     }
184
185     public float[] testSimpleCreateEntity(SimpleEntityHome home) throws Exception JavaDoc {
186
187         Thread JavaDoc[] threads = new Thread JavaDoc[maxClients];
188         float[] result = new float[depth];
189
190         class Worker extends Thread JavaDoc {
191             int startId = 0;
192             int numBeans = 0;
193             SimpleEntityHome home;
194             
195             public Worker(int startId, int numBeans, SimpleEntityHome home) {
196                 this.startId = startId;
197                 this.numBeans = numBeans;
198                 this.home = home;
199             }
200
201             public void run() {
202                 for (int i=0; i<numBeans; i++) {
203                     try {
204                         SimpleEntity bean = home.create(startId + i);
205                     } catch (Exception JavaDoc e) {
206                     }
207                 }
208             }
209         }
210
211         for (int i = 0; i < depth; i++) {
212             
213             log.debug("Testing simple bean creation with " + nbClients[i] + " clients");
214             
215             int numBeans = nbCalls / nbClients[i];
216
217             for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
218                 Worker worker = new Worker(i * nbCalls + threadNumber * numBeans, numBeans, home);
219                 threads[threadNumber] = worker;
220             }
221             
222             long start = System.currentTimeMillis();
223             
224             for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
225                 threads[threadNumber].start();
226             }
227
228             for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
229                 try {
230                     threads[threadNumber].join();
231                 } catch (InterruptedException JavaDoc e) {
232                     // ignore
233
}
234             }
235
236             long stop = System.currentTimeMillis();
237             
238             result[i] = ((float)(stop-start)) / (numBeans * nbClients[i]);
239             
240         }
241         
242         return result;
243     }
244
245     public float[] testComplexCreateEntity(ComplexEntityHome home) throws Exception JavaDoc {
246
247         Thread JavaDoc[] threads = new Thread JavaDoc[maxClients];
248         float[] result = new float[depth];
249
250         class Worker extends Thread JavaDoc {
251             long aLong;
252             double aDouble;
253             int numBeans = 0;
254             ComplexEntityHome home;
255             String JavaDoc aString = new String JavaDoc(new char[dataSize]);
256             
257             public Worker(long aLong, double aDouble, int numBeans, ComplexEntityHome home) {
258                 this.aLong = aLong;
259                 this.aDouble = aDouble;
260                 this.numBeans = numBeans;
261                 this.home = home;
262             }
263
264             public void run() {
265                 for (int i=0; i<numBeans; i++) {
266                     try {
267                         ComplexEntity bean = home.create(true, i, aLong, aDouble, aString);
268                     } catch (Exception JavaDoc e) {
269                     }
270                 }
271             }
272         }
273
274         for (int i = 0; i < depth; i++) {
275             
276             log.debug("Testing complex bean creation with " + nbClients[i] + " clients");
277             
278             int numBeans = nbCalls / nbClients[i];
279
280             for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
281
282                 Worker worker = new Worker((long)i, (double)threadNumber, numBeans, home);
283                 threads[threadNumber] = worker;
284             }
285             
286             long start = System.currentTimeMillis();
287             
288             for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
289                 threads[threadNumber].start();
290             }
291
292             for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
293                 try {
294                     threads[threadNumber].join();
295                 } catch (InterruptedException JavaDoc e) {
296                     // ignore
297
}
298             }
299
300             long stop = System.currentTimeMillis();
301             
302             result[i] = ((float)(stop-start)) / (numBeans * nbClients[i]);
303             
304         }
305         
306         return result;
307     }
308
309     public float[] readEntity(SimpleEntityHome home) throws Exception JavaDoc {
310         Thread JavaDoc[] threads = new Thread JavaDoc[maxClients];
311         float[] result = new float[depth];
312
313         class Worker extends Thread JavaDoc {
314             int loops;
315             SimpleEntity bean;
316             
317             public Worker(int beanId, SimpleEntityHome wHome, int loops) throws Exception JavaDoc {
318                 this.loops = loops;
319                 
320                 try {
321                     bean = wHome.findByPrimaryKey(new Integer JavaDoc(beanId));
322                 } catch (Exception JavaDoc e) {
323                     bean = wHome.create(beanId);
324                 }
325             }
326
327             public void run() {
328                 for (int i=0; i<loops; i++) {
329                     try {
330                         int field = bean.getField();
331                     } catch (Exception JavaDoc e) {
332                     }
333                 }
334             }
335         }
336
337         for (int i = 0; i < depth; i++) {
338             
339             log.debug("Testing read-only call on simple entity with " + nbClients[i] + " clients");
340             
341             int loops = nbCalls / nbClients[i];
342
343             for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
344
345                 Worker worker = new Worker(threadNumber, home, loops);
346                 threads[threadNumber] = worker;
347             }
348             
349             long start = System.currentTimeMillis();
350             
351             for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
352                 threads[threadNumber].start();
353             }
354
355             for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
356                 try {
357                     threads[threadNumber].join();
358                 } catch (InterruptedException JavaDoc e) {
359                     // ignore
360
}
361             }
362
363             long stop = System.currentTimeMillis();
364             
365             result[i] = ((float)(stop-start)) / (loops * nbClients[i]);
366             
367         }
368         
369         return result;
370     }
371
372     public float[] writeEntity(ComplexEntityHome home) throws Exception JavaDoc {
373         Thread JavaDoc[] threads = new Thread JavaDoc[maxClients];
374         float[] result = new float[depth];
375
376         class Worker extends Thread JavaDoc {
377             int loops;
378             String JavaDoc otherField = new String JavaDoc(new char[dataSize]);
379             ComplexEntity bean;
380             
381             public Worker(int beanId, ComplexEntityHome wHome, int loops) throws Exception JavaDoc {
382                 this.loops = loops;
383                 
384                 try {
385                     bean = wHome.findByPrimaryKey(new AComplexPK(true, beanId, (long)0, (double)0, "empty"));
386                 } catch (Exception JavaDoc e) {
387                     bean = wHome.create(true, beanId, (long)0, (double)0, "empty");
388                 }
389             }
390
391             public void run() {
392                 for (int i=0; i<loops; i++) {
393                     try {
394                         bean.setOtherField(otherField + i);
395                     } catch (Exception JavaDoc e) {
396                     }
397                 }
398             }
399         }
400
401         for (int i = 0; i < depth; i++) {
402             
403             log.debug("Testing call with db write on complex entity with " + nbClients[i] + " clients");
404             
405             int loops = nbCalls / nbClients[i];
406
407             for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
408
409                 Worker worker = new Worker(i * maxClients + threadNumber, home, loops);
410                 threads[threadNumber] = worker;
411             }
412             
413             long start = System.currentTimeMillis();
414             
415             for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
416                 threads[threadNumber].start();
417             }
418
419             for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
420                 try {
421                     threads[threadNumber].join();
422                 } catch (InterruptedException JavaDoc e) {
423                     // ignore
424
}
425             }
426
427             long stop = System.currentTimeMillis();
428             
429             result[i] = ((float)(stop-start)) / (loops * nbClients[i]);
430             
431         }
432         
433         return result;
434     }
435
436     public float[] callSession(MySessionHome home) throws Exception JavaDoc {
437         Thread JavaDoc[] threads = new Thread JavaDoc[maxClients];
438         float[] result = new float[depth];
439
440         class Worker extends Thread JavaDoc {
441             int loops;
442             MySession bean;
443             
444             public Worker(MySessionHome wHome, int loops) throws Exception JavaDoc {
445                 this.loops = loops;
446                 
447                 bean = wHome.create();
448             }
449
450             public void run() {
451                 for (int i=0; i<loops; i++) {
452                     try {
453                         int res = bean.getInt();
454                     } catch (Exception JavaDoc e) {
455                         log.debug("failed", e);
456                     }
457                 }
458             }
459         }
460
461         for (int i = 0; i < depth; i++) {
462             
463             log.debug("Testing call to session bean " + nbClients[i] + " clients");
464             
465             int loops = nbCalls / nbClients[i];
466
467             for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
468
469                 Worker worker = new Worker(home, loops);
470                 threads[threadNumber] = worker;
471             }
472             
473             long start = System.currentTimeMillis();
474             
475             for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
476                 threads[threadNumber].start();
477             }
478
479             for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
480                 try {
481                     threads[threadNumber].join();
482                 } catch (InterruptedException JavaDoc e) {
483                     // ignore
484
}
485             }
486
487             long stop = System.currentTimeMillis();
488             
489             result[i] = ((float)(stop-start)) / (loops * nbClients[i]);
490             
491         }
492         
493         return result;
494     }
495
496         
497
498 }
499
Popular Tags