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.bench.servlet;
023:
024: import java.util.ArrayList;
025:
026: import javax.servlet.http.HttpServletRequest;
027:
028: import javax.naming.Context;
029: import javax.naming.InitialContext;
030:
031: import org.jboss.test.bench.interfaces.MySession;
032: import org.jboss.test.bench.interfaces.MySessionHome;
033: import org.jboss.test.bench.interfaces.SimpleEntity;
034: import org.jboss.test.bench.interfaces.SimpleEntityHome;
035: import org.jboss.test.bench.interfaces.ComplexEntity;
036: import org.jboss.test.bench.interfaces.ComplexEntityHome;
037: import org.jboss.test.bench.interfaces.AComplexPK;
038:
039: public class EJBTester {
040: org.apache.log4j.Category log = org.apache.log4j.Category
041: .getInstance(getClass());
042:
043: int maxClients;
044: Context ctx;
045:
046: HttpServletRequest req;
047:
048: // only the "depth" first items of this array will be used
049: public static final int nbClients[] = { 1, 10, 50, 100, 200, 500 };
050: public int depth;
051: public int nbTests = 0;
052: int nbCalls;
053: int dataSize = 1024;
054:
055: ArrayList testNames = new ArrayList();
056: ArrayList testResults = new ArrayList();
057:
058: public EJBTester(HttpServletRequest req) {
059:
060: maxClients = Integer.parseInt(req.getParameter("maxClients"));
061: nbCalls = Integer.parseInt(req.getParameter("nbCalls"));
062:
063: this .req = req;
064:
065: depth = nbClients.length;
066: for (int i = 0; i < nbClients.length; i++)
067: if (nbClients[i] > maxClients) {
068: depth = i;
069: break;
070: }
071:
072: try {
073:
074: System.setProperty("java.naming.factory.initial",
075: "org.jnp.interfaces.NamingContextFactory");
076: System.setProperty("java.naming.provider.url", System
077: .getProperty("jbosstest.server.host", "localhost"));
078: System.setProperty("java.naming.factory.url.pkgs",
079: "org.jboss.naming;");
080:
081: ctx = new InitialContext();
082:
083: } catch (Exception e) {
084: log.debug("failed", e);
085: }
086: }
087:
088: public String getTestName(int i) {
089: return (String) testNames.get(i);
090: }
091:
092: public float getTestResult(int i, int j) {
093: return ((float[]) testResults.get(i))[j];
094: }
095:
096: public void test() {
097: try {
098: if (req.getParameter("createSimpleEntity") != null) {
099: SimpleEntityHome home;
100: float[] result;
101:
102: home = (SimpleEntityHome) ctx.lookup("SimpleEntity");
103: result = testSimpleCreateEntity(home);
104: testNames
105: .add("Simple Entity Bean creation (optimized)");
106: testResults.add(result);
107: nbTests++;
108:
109: home = (SimpleEntityHome) ctx
110: .lookup("NonOptSimpleEntity");
111: result = testSimpleCreateEntity(home);
112: testNames
113: .add("Simple Entity Bean creation (serialized)");
114: testResults.add(result);
115: nbTests++;
116: }
117: if (req.getParameter("createComplexEntity") != null) {
118: ComplexEntityHome home;
119: float[] result;
120:
121: home = (ComplexEntityHome) ctx.lookup("ComplexEntity");
122: result = testComplexCreateEntity(home);
123: testNames
124: .add("Complex Entity Bean creation (optimized)");
125: testResults.add(result);
126: nbTests++;
127:
128: home = (ComplexEntityHome) ctx
129: .lookup("NonOptComplexEntity");
130: result = testComplexCreateEntity(home);
131: testNames
132: .add("Complex Entity Bean creation (serialized)");
133: testResults.add(result);
134: nbTests++;
135: }
136: if (req.getParameter("readEntity") != null) {
137: SimpleEntityHome home;
138: float[] result;
139:
140: home = (SimpleEntityHome) ctx.lookup("SimpleEntity");
141: result = readEntity(home);
142: testNames
143: .add("Read-only call on an entity bean (optimized)");
144: testResults.add(result);
145: nbTests++;
146:
147: home = (SimpleEntityHome) ctx
148: .lookup("NonOptSimpleEntity");
149: result = readEntity(home);
150: testNames
151: .add("Read-only call on an entity bean (serialized)");
152: testResults.add(result);
153: nbTests++;
154: }
155: if (req.getParameter("writeEntity") != null) {
156: ComplexEntityHome home;
157: float[] result;
158:
159: home = (ComplexEntityHome) ctx.lookup("ComplexEntity");
160: result = writeEntity(home);
161: testNames.add("Write call to entity (optimized)");
162: testResults.add(result);
163: nbTests++;
164:
165: home = (ComplexEntityHome) ctx
166: .lookup("NonOptComplexEntity");
167: result = writeEntity(home);
168: testNames.add("Write call to entity (serialized)");
169: testResults.add(result);
170: nbTests++;
171: }
172: if (req.getParameter("callSF") != null) {
173: MySessionHome home;
174: float[] result;
175:
176: home = (MySessionHome) ctx.lookup("StatefulSession");
177: result = callSession(home);
178: testNames.add("Call to stateful session (optimized)");
179: testResults.add(result);
180: nbTests++;
181: }
182: if (req.getParameter("callSL") != null) {
183: MySessionHome home;
184: float[] result;
185:
186: home = (MySessionHome) ctx.lookup("StatelessSession");
187: result = callSession(home);
188: testNames.add("Call to stateless session (optimized)");
189: testResults.add(result);
190: nbTests++;
191: }
192:
193: } catch (Exception e) {
194: log.debug("failed", e);
195: }
196: }
197:
198: public float[] testSimpleCreateEntity(SimpleEntityHome home)
199: throws Exception {
200:
201: Thread[] threads = new Thread[maxClients];
202: float[] result = new float[depth];
203:
204: class Worker extends Thread {
205: int startId = 0;
206: int numBeans = 0;
207: SimpleEntityHome home;
208:
209: public Worker(int startId, int numBeans,
210: SimpleEntityHome home) {
211: this .startId = startId;
212: this .numBeans = numBeans;
213: this .home = home;
214: }
215:
216: public void run() {
217: for (int i = 0; i < numBeans; i++) {
218: try {
219: SimpleEntity bean = home.create(startId + i);
220: } catch (Exception e) {
221: }
222: }
223: }
224: }
225:
226: for (int i = 0; i < depth; i++) {
227:
228: log.debug("Testing simple bean creation with "
229: + nbClients[i] + " clients");
230:
231: int numBeans = nbCalls / nbClients[i];
232:
233: for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
234: Worker worker = new Worker(i * nbCalls + threadNumber
235: * numBeans, numBeans, home);
236: threads[threadNumber] = worker;
237: }
238:
239: long start = System.currentTimeMillis();
240:
241: for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
242: threads[threadNumber].start();
243: }
244:
245: for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
246: try {
247: threads[threadNumber].join();
248: } catch (InterruptedException e) {
249: // ignore
250: }
251: }
252:
253: long stop = System.currentTimeMillis();
254:
255: result[i] = ((float) (stop - start))
256: / (numBeans * nbClients[i]);
257:
258: }
259:
260: return result;
261: }
262:
263: public float[] testComplexCreateEntity(ComplexEntityHome home)
264: throws Exception {
265:
266: Thread[] threads = new Thread[maxClients];
267: float[] result = new float[depth];
268:
269: class Worker extends Thread {
270: long aLong;
271: double aDouble;
272: int numBeans = 0;
273: ComplexEntityHome home;
274: String aString = new String(new char[dataSize]);
275:
276: public Worker(long aLong, double aDouble, int numBeans,
277: ComplexEntityHome home) {
278: this .aLong = aLong;
279: this .aDouble = aDouble;
280: this .numBeans = numBeans;
281: this .home = home;
282: }
283:
284: public void run() {
285: for (int i = 0; i < numBeans; i++) {
286: try {
287: ComplexEntity bean = home.create(true, i,
288: aLong, aDouble, aString);
289: } catch (Exception e) {
290: }
291: }
292: }
293: }
294:
295: for (int i = 0; i < depth; i++) {
296:
297: log.debug("Testing complex bean creation with "
298: + nbClients[i] + " clients");
299:
300: int numBeans = nbCalls / nbClients[i];
301:
302: for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
303:
304: Worker worker = new Worker((long) i,
305: (double) threadNumber, numBeans, home);
306: threads[threadNumber] = worker;
307: }
308:
309: long start = System.currentTimeMillis();
310:
311: for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
312: threads[threadNumber].start();
313: }
314:
315: for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
316: try {
317: threads[threadNumber].join();
318: } catch (InterruptedException e) {
319: // ignore
320: }
321: }
322:
323: long stop = System.currentTimeMillis();
324:
325: result[i] = ((float) (stop - start))
326: / (numBeans * nbClients[i]);
327:
328: }
329:
330: return result;
331: }
332:
333: public float[] readEntity(SimpleEntityHome home) throws Exception {
334: Thread[] threads = new Thread[maxClients];
335: float[] result = new float[depth];
336:
337: class Worker extends Thread {
338: int loops;
339: SimpleEntity bean;
340:
341: public Worker(int beanId, SimpleEntityHome wHome, int loops)
342: throws Exception {
343: this .loops = loops;
344:
345: try {
346: bean = wHome.findByPrimaryKey(new Integer(beanId));
347: } catch (Exception e) {
348: bean = wHome.create(beanId);
349: }
350: }
351:
352: public void run() {
353: for (int i = 0; i < loops; i++) {
354: try {
355: int field = bean.getField();
356: } catch (Exception e) {
357: }
358: }
359: }
360: }
361:
362: for (int i = 0; i < depth; i++) {
363:
364: log.debug("Testing read-only call on simple entity with "
365: + nbClients[i] + " clients");
366:
367: int loops = nbCalls / nbClients[i];
368:
369: for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
370:
371: Worker worker = new Worker(threadNumber, home, loops);
372: threads[threadNumber] = worker;
373: }
374:
375: long start = System.currentTimeMillis();
376:
377: for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
378: threads[threadNumber].start();
379: }
380:
381: for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
382: try {
383: threads[threadNumber].join();
384: } catch (InterruptedException e) {
385: // ignore
386: }
387: }
388:
389: long stop = System.currentTimeMillis();
390:
391: result[i] = ((float) (stop - start))
392: / (loops * nbClients[i]);
393:
394: }
395:
396: return result;
397: }
398:
399: public float[] writeEntity(ComplexEntityHome home) throws Exception {
400: Thread[] threads = new Thread[maxClients];
401: float[] result = new float[depth];
402:
403: class Worker extends Thread {
404: int loops;
405: String otherField = new String(new char[dataSize]);
406: ComplexEntity bean;
407:
408: public Worker(int beanId, ComplexEntityHome wHome, int loops)
409: throws Exception {
410: this .loops = loops;
411:
412: try {
413: bean = wHome.findByPrimaryKey(new AComplexPK(true,
414: beanId, (long) 0, (double) 0, "empty"));
415: } catch (Exception e) {
416: bean = wHome.create(true, beanId, (long) 0,
417: (double) 0, "empty");
418: }
419: }
420:
421: public void run() {
422: for (int i = 0; i < loops; i++) {
423: try {
424: bean.setOtherField(otherField + i);
425: } catch (Exception e) {
426: }
427: }
428: }
429: }
430:
431: for (int i = 0; i < depth; i++) {
432:
433: log
434: .debug("Testing call with db write on complex entity with "
435: + nbClients[i] + " clients");
436:
437: int loops = nbCalls / nbClients[i];
438:
439: for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
440:
441: Worker worker = new Worker(i * maxClients
442: + threadNumber, home, loops);
443: threads[threadNumber] = worker;
444: }
445:
446: long start = System.currentTimeMillis();
447:
448: for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
449: threads[threadNumber].start();
450: }
451:
452: for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
453: try {
454: threads[threadNumber].join();
455: } catch (InterruptedException e) {
456: // ignore
457: }
458: }
459:
460: long stop = System.currentTimeMillis();
461:
462: result[i] = ((float) (stop - start))
463: / (loops * nbClients[i]);
464:
465: }
466:
467: return result;
468: }
469:
470: public float[] callSession(MySessionHome home) throws Exception {
471: Thread[] threads = new Thread[maxClients];
472: float[] result = new float[depth];
473:
474: class Worker extends Thread {
475: int loops;
476: MySession bean;
477:
478: public Worker(MySessionHome wHome, int loops)
479: throws Exception {
480: this .loops = loops;
481:
482: bean = wHome.create();
483: }
484:
485: public void run() {
486: for (int i = 0; i < loops; i++) {
487: try {
488: int res = bean.getInt();
489: } catch (Exception e) {
490: log.debug("failed", e);
491: }
492: }
493: }
494: }
495:
496: for (int i = 0; i < depth; i++) {
497:
498: log.debug("Testing call to session bean " + nbClients[i]
499: + " clients");
500:
501: int loops = nbCalls / nbClients[i];
502:
503: for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
504:
505: Worker worker = new Worker(home, loops);
506: threads[threadNumber] = worker;
507: }
508:
509: long start = System.currentTimeMillis();
510:
511: for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
512: threads[threadNumber].start();
513: }
514:
515: for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
516: try {
517: threads[threadNumber].join();
518: } catch (InterruptedException e) {
519: // ignore
520: }
521: }
522:
523: long stop = System.currentTimeMillis();
524:
525: result[i] = ((float) (stop - start))
526: / (loops * nbClients[i]);
527:
528: }
529:
530: return result;
531: }
532:
533: }
|