User: schulze
Date: 00/10/07 17:03:02
Added: src/main/org/jboss/test/load/test Client.java Worker.java
Log:
simple loadtest added.
Revision Changes Path
1.1 jbosstest/src/main/org/jboss/test/load/test/Client.java
Index: Client.java
===================================================================
package org.jboss.test.load.test;
import java.util.Properties;
import java.util.StringTokenizer;
import org.jboss.jmx.client.Deployer;
/**
* Test client. <br>
* Deployes the testbean.jar and then starts as many workers as on the
* command line given.
* After all workers are done, it prints a success or failure message.
* The return value is the number of failed workers (0=sucess, 1=one worker failed,
...)
*
* parsed parameter:
* <dl> <li> verbose - more output </li>
* <li> nodeploy - the testbean.jar dont becomes deployed </li>
* <li> loops - iterations each thread has to do </li>
* <li> beans - number of beans each thread has to deal with </li>
* <li> threads - number of threads getting started </li>
* </dl>
*
* @author <a href="mailto:[EMAIL PROTECTED]">Daniel Schulze</a>
* @version $Id: Client.java,v 1.1 2000/10/08 00:03:01 schulze Exp $
*/
public class Client
{
Properties param = new Properties ();;
int exitCode = 0;
public Client (String[] _args) throws Exception
{
// scanning parameters
int i = 0;
while (i < _args.length)
{
StringTokenizer st = new StringTokenizer (_args[i++], "=");
param.put (st.nextToken (),
st.hasMoreTokens () ? st.nextToken () : "");
}
System.out.println("_____________________________________________");
System.out.println();
System.out.println("jBoss, the EJB Open Source Server");
System.out.println("Copyright (C), The jBoss Organization, 2000");
System.out.println("_____________________________________________");
System.out.println();
System.out.println("Welcome to the Load Test v0.1");
System.out.println("_____________________________________________");
System.out.println();
if (param.get ("nodeploy") == null)
{
System.out.print("Deploying test beans...");
System.out.flush();
Deployer deployer = new Deployer ();
deployer.deploy("../deploy/testbean.jar");
System.out.println("done!");
}
exitCode = test1 ();
//System.out.print("Undeploying test beans...");
//deployer.undeploy("../deploy/testbean.jar");
//System.out.println("done!");
System.out.println();
System.out.println("Test completed.");
System.out.println("Please take the time to report us your results to the " +
"[EMAIL PROTECTED] or [EMAIL PROTECTED]
mailing list");
System.out.println(" ");
System.out.println(" jBoss version : ");
System.out.println(" jBoss configuration : ");
System.out.println(" (conf/jboss.jcml) ");
System.out.println(" your OS : ");
System.out.println(" JDK (vm vendor/version) : ");
System.out.println(" DB (product/version) : ");
System.out.println(" Database driver (version): ");
System.out.println(" ");
System.out.println("Thanks in advance!");
System.out.println();
System.out.println("note: I guess the testbeans are still deployed.");
System.exit (exitCode);
}
private int test1 ()
{
//System.out.println("Test (I):");
boolean verbose = false;
verbose = param.getProperty ("verbose") != null;
int threads = 50;
try {
threads = Integer.parseInt (param.getProperty ("threads"));
} catch (Exception _e){
System.out.println("no (or wrong) thread number specified. using default: "
+ threads);
}
int beans = 5;
try {
beans = Integer.parseInt (param.getProperty ("beans"));
} catch (Exception _e) {
System.out.println("no (or wrong) number of beans (per thread) specified.
using default: " + beans);
}
int loops = 100;
try {
loops = Integer.parseInt (param.getProperty ("loops"));
} catch (Exception _e){
System.out.println("no (or wrong) number of loops specified. using default:
" + loops);
}
String name = param.getProperty ("name", "daniel");
System.out.println ("start test1 with "+threads+" threads, "+beans+" beans per
thread in "+loops+" loops.");
System.out.println("------------------------------------------------------");
ThreadGroup threadGroup = new ThreadGroup ("workers");
Thread[] workers = new Thread[threads];
long start = System.currentTimeMillis ();
// create and start threads...
for (int i = 0; i < threads; ++i)
{
workers[i] = new Worker (threadGroup, i, name, verbose, beans, loops);
workers[i].start ();
}
// wait for all threads to finish... (is this the most elegant way?!)
try
{
Thread me = Thread.currentThread ();
while (threadGroup.activeCount () > 0)
me.sleep (5000L);
}
catch (InterruptedException _ie){
System.out.print ("Main thread interrupted?!");
}
long stop = System.currentTimeMillis ();
int time = (int)(stop - start)/1000;
// determine success...
int failed = 0;
for (int i = 0; i < threads; ++i)
if (((Worker)workers[i]).failed ())
++failed;
System.out.println("------------------------------------------------------");
if (failed > 0)
{
System.out.println("The Test didnt succeed completly :-(");
System.out.println("in " + failed + " threads occurred an error.");
}
else
{
System.out.println("Congratulations the test succeeded! :-)");
System.out.println("All threads finished clean.");
int transactions = threads*beans*loops*2;
int h = time/3600;
int m = (time-h*3600)/60;
System.out.println("statistic: "+transactions+" transactions in "+h+" h
"+m+" min.");
System.out.println(" transactions per second:
"+(transactions/time));
System.out.println(" (on short runs the bean creation/deletion
may influence the result!)");
}
return failed;
}
public static void main (String[] _args) throws Exception
{
new Client (_args);
}
}
1.1 jbosstest/src/main/org/jboss/test/load/test/Worker.java
Index: Worker.java
===================================================================
package org.jboss.test.load.test;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.ejb.*;
import org.jboss.test.testbean.interfaces.*;
/**
* Working thread for the Client class. <br>
* looks up the number of beans he shall use (creates them if not found)
* and reads a value and writes a value for every iteration on every bean.
* After completition the beans are removed on fail the beans are not removed.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Daniel Schulze</a>
* @version $Id: Worker.java,v 1.1 2000/10/08 00:03:02 schulze Exp $
*/
public class Worker
extends Thread
{
// beans per thread
int beans;
// loops per thread
int loops;
// for the log
String logTag;
// some more info...
boolean verbose;
// succcess or not?
boolean failed = false;
Worker (ThreadGroup _group, int _number, String _name, boolean _verbose, int
_beans, int _loops)
{
super (_group, _name + "_" + _number);
beans = _beans;
loops = _loops;
verbose = _verbose;
logTag = "["+getName ()+"] ";
}
/** for the client to ask for success */
public boolean failed ()
{
return failed;
}
public void run()
{
EnterpriseEntity[] entity = new EnterpriseEntity[beans];
try
{
// find/create the beans
EnterpriseEntityHome home = (EnterpriseEntityHome) new InitialContext
().lookup ("nextgen.EnterpriseEntity");
if (home == null)
{
System.out.println (logTag + "EJBHomeInterface lookup returned null?!");
System.out.println (logTag + "died.");
failed = true;
return;
}
for (int i = 0; i < beans; ++i)
{
String key = getName () + "_" + i;
try
{
// first try to find it...
entity[i] = home.findByPrimaryKey (key);
if (entity[i] == null)
{
System.out.println (logTag + "EJBHome.findByPrimaryKey () returned
null?!");
System.out.println (logTag + "died.");
failed = true;
return;
}
if (verbose) System.out.println (logTag + "reuse bean:
"+entity[i].getPrimaryKey ());
}
catch (FinderException _fe)
{
// so lets create it...
entity[i] = home.create (key);
if (entity[i] == null)
{
System.out.println (logTag + "EJBHome.create () returned null?!");
System.out.println (logTag + "died.");
failed = true;
return;
}
if (verbose) System.out.println (logTag + "create bean:
"+entity[i].getPrimaryKey ());
}
}
}
catch (Exception _e)
{
System.out.println (logTag+ "problem while finding/creating beans:
"+_e.toString ());
System.out.println (logTag+" died!");
failed = true;
return;
}
// prepare the beans for the test.....
for (int b = 0; b < beans; ++b)
{
try
{
entity[b].setOtherField (0);
}
catch (Exception _e)
{
System.out.println (logTag+ "cannot prepare bean #"+b+": "+_e.toString
());
System.out.println (logTag+" died!");
failed = true;
return;
}
}
// do the test...
for (int l = 0; l < loops; ++l)
{
for (int b = 0; b < beans; ++b)
{
int value = -1;
try
{
value = entity[b].getOtherField ();
}
catch (Exception _e)
{
System.out.println (logTag + "error in getOtherField () on bean
#"+b+" in loop "+l+ ": "+_e.toString ());
if (value != l)
System.out.println (logTag + "unexpected value in bean #"+b+" in
loop "+l);
}
try
{
entity[b].setOtherField (l + 1);
}
catch (Exception _e)
{
System.out.println (logTag + "error in setOtherField () on bean
#"+b+" in loop "+l+ ": "+_e.toString ());
}
}
}
// remove the beans...
for (int b = 0; b < beans; ++b)
{
try
{
if (verbose) System.out.println (logTag + "remove bean #"+b);
entity[b].remove ();
}
catch (Exception _e)
{
System.out.println (logTag + "error in removing bean #"+b+":
"+_e.toString ());
//_e.printStackTrace (System.out);
}
}
}
}