KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > load > test > Client


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.load.test;
23
24 import java.util.Properties JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26 import java.util.Hashtable JavaDoc;
27
28 import org.jboss.test.JBossTestCase;
29
30 /**
31 * Test client. <br>
32 * Deployes the testbean.jar and then starts as many workers as on the
33 * command line given.
34 * After all workers are done, it prints a success or failure message.
35 * The return value is the number of failed workers (0=sucess, 1=one worker failed, ...)
36 *
37 * parsed parameter:
38 * <dl> <li> verbose - more output </li>
39 * <li> nodeploy - the testbean.jar dont becomes deployed </li>
40 * <li> loops - iterations each thread has to do </li>
41 * <li> beans - number of beans each thread has to deal with </li>
42 * <li> threads - number of threads getting started </li>
43 * </dl>
44 *
45 * @author <a HREF="mailto:daniel.schulze@telkel.com">Daniel Schulze</a>
46 * @version $Id: Client.java 37406 2005-10-29 23:41:24Z starksm $
47 */

48 public class Client extends JBossTestCase
49 {
50    Properties JavaDoc param = new Properties JavaDoc ();;
51    int exitCode = 0;
52
53    public Client(String JavaDoc name)
54    {
55       super(name);
56    }
57
58    public Client (String JavaDoc[] _args) throws Exception JavaDoc
59    {
60       super("main");
61       // scanning parameters
62
int i = 0;
63       while (i < _args.length)
64       {
65          StringTokenizer JavaDoc st = new StringTokenizer JavaDoc (_args[i++], "=");
66          param.put (st.nextToken (),
67                     st.hasMoreTokens () ? st.nextToken () : "");
68       }
69
70       System.out.println("_____________________________________________");
71        // sed kicks ass
72
System.out.println("jBoss, the EJB Open Source Server");
73       System.out.println("Copyright (C), The jBoss Organization, 2000");
74       System.out.println("_____________________________________________");
75        // sed kicks ass
76
System.out.println("Welcome to the Load Test v0.1");
77       System.out.println("_____________________________________________");
78        // sed kicks ass
79
System.out.println("the following optional parameters are supported:");
80       System.out.println(" loops=<number> number of test loops per thread");
81       System.out.println(" beans=<number> number of beans used per thread");
82       System.out.println(" threads=<number> number of threads getting started");
83       System.out.println(" delay=<millisec> delay between the thread start");
84       System.out.println(" verbose - gives infos about bean creation/removing");
85       System.out.println(" noremove - keeps the created beans after finish");
86       System.out.println(" nodeploy - does not deploy anything");
87       System.out.println(" (by default the testbeans.jar from this testsuite");
88       System.out.println(" is deployed (the test needs the nextgen.EnterpriseEntity)");
89       System.out.println(" name=<string> name of the threads");
90       System.out.println("for batch use: in case of any failure it returns != 0");
91       System.out.println ();
92       
93       if (param.get ("nodeploy") == null)
94       {
95          System.out.print("Deploying test beans...");
96          System.out.flush();
97          deploy("../deploy/testbean.jar");
98          System.out.println("done!");
99       }
100
101       exitCode = test1 ();
102
103       //System.out.print("Undeploying test beans...");
104
//deployer.undeploy("../deploy/testbean.jar");
105
//System.out.println("done!");
106

107        // sed kicks ass
108
System.out.println("Test completed.");
109       System.out.println("Please take the time to report us your results to the " +
110                          "jboss-dev@working-dogs.com or jboss-user@working-dogs.com mailing list");
111       System.out.println(" ");
112       System.out.println(" jBoss version : ");
113       System.out.println(" jBoss configuration : ");
114       System.out.println(" (conf/jboss.jcml) ");
115       System.out.println(" your OS : ");
116       System.out.println(" JDK (vm vendor/version) : ");
117       System.out.println(" DB (product/version) : ");
118       System.out.println(" Database driver (version): ");
119       System.out.println(" ");
120       System.out.println("Thanks in advance!");
121        // sed kicks ass
122
System.out.println("note: I guess the testbeans are still deployed.");
123
124       System.exit (exitCode);
125    }
126
127    private int test1 ()
128    {
129       Hashtable JavaDoc config = new Hashtable JavaDoc ();
130       
131
132       config.put ("verbose", new Boolean JavaDoc (param.getProperty ("verbose") != null));
133       config.put ("noremove", new Boolean JavaDoc (param.getProperty ("noremove") != null));
134
135       int beans = 5;
136       try {
137       beans = Integer.parseInt (param.getProperty ("beans"));
138       } catch (Exception JavaDoc _e) {
139          System.out.println("no (or wrong) number of beans (per thread) specified. using default: " + beans);
140       }
141       config.put ("beans", new Integer JavaDoc (beans));
142
143       int loops = 100;
144       try {
145       loops = Integer.parseInt (param.getProperty ("loops"));
146       } catch (Exception JavaDoc _e){
147          System.out.println("no (or wrong) number of loops specified. using default: " + loops);
148       }
149       config.put ("loops", new Integer JavaDoc (loops));
150
151       config.put ("name", param.getProperty ("name", "daniel") );
152
153       int threads = 50;
154       try {
155       threads = Integer.parseInt (param.getProperty ("threads"));
156       } catch (Exception JavaDoc _e){
157          System.out.println("no (or wrong) thread number specified. using default: " + threads);
158       }
159       int delay = 1000;
160       try {
161       delay = Integer.parseInt (param.getProperty ("delay"));
162       } catch (Exception JavaDoc _e){
163          System.out.println("no (or wrong) delay (millisec between each thread start) specified. using default: " + delay);
164       }
165       System.out.println ("start test1 with "+threads+" threads, "+beans+" beans per thread in "+loops+" loops.");
166       System.out.println("------------------------------------------------------");
167
168
169       ThreadGroup JavaDoc threadGroup = new ThreadGroup JavaDoc ("workers");
170       Worker[] workers = new Worker[threads];
171
172       long start = System.currentTimeMillis();
173       // create and start threads...
174
for (int i = 0; i < threads; ++i)
175       {
176          Hashtable JavaDoc cfg = (Hashtable JavaDoc)config.clone ();
177          cfg.put ("number", new Integer JavaDoc (i));
178          workers[i] = new Worker (threadGroup, cfg);
179          workers[i].start ();
180          // because of problems with Windows 2000...
181
// (on W2000 the server couldnt serve this many connection
182
// requests (jndi) in such quick sequence)
183
// .. this hack:
184
try
185          {
186             Thread.currentThread ().sleep (delay);
187          } catch (InterruptedException JavaDoc _ie)
188          { // shoudnt happen...
189
}
190       }
191       
192       // wait for all threads to finish... (is this the most elegant way?!)
193
try
194       {
195          Thread JavaDoc me = Thread.currentThread ();
196          while (threadGroup.activeCount () > 0)
197             me.sleep (5000L);
198       }
199       catch (InterruptedException JavaDoc _ie){
200          System.out.print ("Main thread interrupted?!");
201       }
202       long timeSum = (System.currentTimeMillis() - start)/1000L;
203       
204       // make statistic...
205
int failed = 0;
206       int tx = 0;
207       for (int i = 0; i < threads; ++i)
208       {
209          Hashtable JavaDoc cfg = workers[i].getConfig ();
210          if (((Boolean JavaDoc)cfg.get("failed")).booleanValue ())
211             ++failed;
212
213          tx += ((Integer JavaDoc)cfg.get("transactions")).intValue ();
214          //timeAcc += ((Long)cfg.get("time")).longValue ();
215
}
216       long txps = tx/timeSum;
217       
218
219       System.out.println("------------------------------------------------------");
220       if (failed > 0)
221       {
222          System.out.println("The Test didnt succeed completly :-(");
223          System.out.println("in " + failed + " threads occurred an error.");
224       }
225       else
226       {
227          System.out.println("Congratulations the test succeeded! :-)");
228          System.out.println("All threads finished clean.");
229       }
230       
231       
232       long h = timeSum/3600L;
233       long m = (timeSum-h*3600L)/60L;
234       long s = timeSum-h*3600L-m*60L;
235       System.out.println("");
236       System.out.println("statistic: "+tx+" transactions in "+h+" h "+m+" min "+s+" s.");
237       System.out.println(" transactions per second (average): "+txps);
238       return failed;
239    }
240
241
242
243    public static void main (String JavaDoc[] _args) throws Exception JavaDoc
244    {
245       new Client (_args);
246    }
247
248
249 }
250
Popular Tags