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.net.URL;
025: import java.util.ArrayList;
026:
027: import javax.servlet.http.HttpServletRequest;
028:
029: public class FullTester {
030: org.apache.log4j.Category log = org.apache.log4j.Category
031: .getInstance(getClass());
032: int maxClients;
033:
034: HttpServletRequest req;
035:
036: // only the "depth" first items of this array will be used
037: public static final int nbClients[] = { 1, 10, 50, 100, 200, 500 };
038: public int depth;
039: public int nbTests = 0;
040: int nbCalls;
041:
042: ArrayList testNames = new ArrayList();
043: ArrayList testResults = new ArrayList();
044:
045: public FullTester(HttpServletRequest req) {
046:
047: maxClients = Integer.parseInt(req.getParameter("maxClients"));
048: nbCalls = Integer.parseInt(req.getParameter("nbCalls"));
049:
050: this .req = req;
051:
052: depth = nbClients.length;
053: for (int i = 0; i < nbClients.length; i++)
054: if (nbClients[i] > maxClients) {
055: depth = i;
056: break;
057: }
058: }
059:
060: public String getTestName(int i) {
061: return (String) testNames.get(i);
062: }
063:
064: public float getTestResult(int i, int j) {
065: return ((float[]) testResults.get(i))[j];
066: }
067:
068: public void test() {
069: try {
070: if (req.getParameter("servlet") != null) {
071: float[] result = testURL("http://"
072: + System.getProperty("jbosstest.server.host",
073: "localhost")
074: + ":8080/bench/servlet/SimpleServlet?dest=none");
075: testNames.add("Servlet alone");
076: testResults.add(result);
077: nbTests++;
078:
079: }
080: if (req.getParameter("servlet2SL") != null) {
081: float[] result = testURL("http://"
082: + System.getProperty("jbosstest.server.host",
083: "localhost")
084: + ":8080/bench/servlet/SimpleServlet?dest=SL");
085: testNames.add("Servlet calling stateless session");
086: testResults.add(result);
087: nbTests++;
088:
089: }
090: if (req.getParameter("servlet2Entity") != null) {
091: float[] result = testURL("http://"
092: + System.getProperty("jbosstest.server.host",
093: "localhost")
094: + ":8080/bench/servlet/SimpleServlet?dest=Entity");
095: testNames.add("Servlet calling entity");
096: testResults.add(result);
097: nbTests++;
098: }
099: } catch (Exception e) {
100: log.debug("failed", e);
101: }
102: }
103:
104: public float[] testURL(String url) throws Exception {
105:
106: Thread[] threads = new Thread[maxClients];
107: float[] result = new float[depth];
108:
109: class Worker extends Thread {
110: String url;
111: int loops;
112:
113: public Worker(int loops, String url) {
114: this .loops = loops;
115: this .url = url;
116: }
117:
118: public void run() {
119: for (int i = 0; i < loops; i++) {
120: try {
121: URL theUrl = new URL(url);
122: Object obj = theUrl.getContent();
123:
124: } catch (Exception e) {
125: }
126: }
127: }
128: }
129:
130: for (int i = 0; i < depth; i++) {
131:
132: log.debug("Calling url " + url + " with " + nbClients[i]
133: + " clients");
134:
135: int loops = nbCalls / nbClients[i];
136:
137: for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
138: Worker worker = new Worker(loops, url);
139: threads[threadNumber] = worker;
140: }
141:
142: long start = System.currentTimeMillis();
143:
144: for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
145: threads[threadNumber].start();
146: }
147:
148: for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
149: try {
150: threads[threadNumber].join();
151: } catch (InterruptedException e) {
152: // ignore
153: }
154: }
155:
156: long stop = System.currentTimeMillis();
157:
158: result[i] = ((float) (stop - start))
159: / (loops * nbClients[i]);
160:
161: }
162:
163: return result;
164: }
165:
166: }
|