KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.URL JavaDoc;
25 import java.util.ArrayList JavaDoc;
26
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28
29 public class FullTester {
30    org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(getClass());
31     int maxClients;
32     
33     HttpServletRequest JavaDoc req;
34
35     // only the "depth" first items of this array will be used
36
public static final int nbClients[] = { 1, 10, 50, 100, 200, 500 };
37     public int depth;
38     public int nbTests = 0;
39     int nbCalls;
40
41     ArrayList JavaDoc testNames = new ArrayList JavaDoc();
42     ArrayList JavaDoc testResults = new ArrayList JavaDoc();
43
44     public FullTester(HttpServletRequest JavaDoc req) {
45         
46         maxClients = Integer.parseInt(req.getParameter("maxClients"));
47         nbCalls = Integer.parseInt(req.getParameter("nbCalls"));
48         
49         this.req = req;
50
51         depth = nbClients.length;
52         for (int i = 0; i< nbClients.length; i++) if (nbClients[i] > maxClients) {
53             depth = i;
54             break;
55         }
56     }
57
58     public String JavaDoc getTestName(int i) {
59         return (String JavaDoc)testNames.get(i);
60     }
61
62     public float getTestResult(int i, int j) {
63         return ((float[])testResults.get(i))[j];
64     }
65
66     public void test() {
67         try {
68             if (req.getParameter("servlet") != null) {
69                 float[] result = testURL("http://localhost:8080/bench/servlet/SimpleServlet?dest=none");
70                 testNames.add("Servlet alone");
71                 testResults.add(result);
72                 nbTests++;
73
74             }
75             if (req.getParameter("servlet2SL") != null) {
76                 float[] result = testURL("http://localhost:8080/bench/servlet/SimpleServlet?dest=SL");
77                 testNames.add("Servlet calling stateless session");
78                 testResults.add(result);
79                 nbTests++;
80
81             }
82             if (req.getParameter("servlet2Entity") != null) {
83                 float[] result = testURL("http://localhost:8080/bench/servlet/SimpleServlet?dest=Entity");
84                 testNames.add("Servlet calling entity");
85                 testResults.add(result);
86                 nbTests++;
87             }
88         } catch (Exception JavaDoc e) {
89             log.debug("failed", e);
90         }
91     }
92
93     public float[] testURL(String JavaDoc url) throws Exception JavaDoc {
94
95         Thread JavaDoc[] threads = new Thread JavaDoc[maxClients];
96         float[] result = new float[depth];
97
98
99         class Worker extends Thread JavaDoc {
100             String JavaDoc url;
101             int loops;
102
103             public Worker(int loops, String JavaDoc url) {
104                 this.loops = loops;
105                 this.url = url;
106             }
107
108             public void run() {
109                 for (int i=0; i<loops; i++) {
110                     try {
111                         URL JavaDoc theUrl = new URL JavaDoc(url);
112                         Object JavaDoc obj = theUrl.getContent();
113
114                     } catch (Exception JavaDoc e) {
115                     }
116                 }
117             }
118         }
119
120         for (int i = 0; i < depth; i++) {
121             
122             log.debug("Calling url " + url + " with " + nbClients[i] + " clients");
123             
124             int loops = nbCalls / nbClients[i];
125
126             for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
127                 Worker worker = new Worker(loops, url);
128                 threads[threadNumber] = worker;
129             }
130             
131             long start = System.currentTimeMillis();
132             
133             for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
134                 threads[threadNumber].start();
135             }
136
137             for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) {
138                 try {
139                     threads[threadNumber].join();
140                 } catch (InterruptedException JavaDoc e) {
141                     // ignore
142
}
143             }
144
145             long stop = System.currentTimeMillis();
146             
147             result[i] = ((float)(stop-start)) / (loops * nbClients[i]);
148             
149         }
150         
151         return result;
152     }
153
154 }
155
Popular Tags