001: /*
002: * This file is part of the QuickServer library
003: * Copyright (C) 2003-2005 QuickServer.org
004: *
005: * Use, modification, copying and distribution of this software is subject to
006: * the terms and conditions of the GNU Lesser General Public License.
007: * You should have received a copy of the GNU LGP License along with this
008: * library; if not, you can download a copy from <http://www.quickserver.org/>.
009: *
010: * For questions, suggestions, bug-reports, enhancement-requests etc.
011: * visit http://www.quickserver.org
012: *
013: */
014:
015: package xmladder;
016:
017: import java.io.*;
018: import java.net.*;
019: import java.util.*;
020:
021: public class LoadTest {
022: private Agent agents[] = null;
023: private ThreadGroup agentGroup = null;
024: private List times = null;
025:
026: public void init(int count, String host, int port) {
027: System.out.print("Starting init..");
028: agentGroup = new ThreadGroup("AgentGroup");
029: times = new ArrayList(count);
030: agents = new Agent[count];
031: for (int i = 0; i < count; i++) {
032: agents[i] = new Agent(agentGroup, times, host, port);
033: agents[i].start();
034: }
035: System.out.println(" " + agentGroup.activeCount() + " ready.");
036: }
037:
038: public void test() throws InterruptedException {
039: int count = agents.length;
040: int half = count / 2;
041:
042: System.out.print("Starting test..");
043: long stime = System.currentTimeMillis();
044: long htime = -1;
045: long hcount = -1;
046:
047: /*
048: for(int i=0;i<count;i++)
049: agents[i].start();
050: */
051: synchronized (agentGroup) {
052: agentGroup.notifyAll();
053: }
054:
055: int ac = times.size();
056: while (ac < count) {
057: if (htime < 0 && half >= ac) {
058: htime = System.currentTimeMillis();
059: hcount = count - ac;
060: }
061: Thread.sleep(5);
062: ac = times.size();
063: }
064:
065: long etime = System.currentTimeMillis();
066: System.out.println("Done\n");
067:
068: long time = etime - stime;
069: System.out.println("Total Time : " + time + "ms");
070:
071: time = time / count;
072: System.out.println("Avg. Time : " + time + "ms\n");
073:
074: /*
075: if(htime!=-1) {
076: time = htime - stime;
077: System.out.println("Half Time ("+hcount+"): "+time+"ms");
078: time = time/hcount;
079: System.out.println("Half Avg Time: "+time+"ms");
080: }
081: */
082:
083: /*
084: System.out.println("\nEach Client time..\n");
085: for(int i=0;i<count;) {
086: System.out.print(times.get(i)+",");
087: if(++i%10==0) System.out.println("");
088: }
089: */
090: }
091:
092: public static void main(String args[]) throws Exception {
093: LoadTest lt = new LoadTest();
094:
095: String host = "127.0.0.1";
096: int port = 2222;
097: int count = 50;
098:
099: if (args.length > 0)
100: count = Integer.parseInt(args[0]);
101: if (args.length > 1)
102: host = args[1];
103: if (args.length > 2)
104: port = Integer.parseInt(args[2]);
105:
106: lt.init(count, host, port);
107:
108: Thread.sleep(200); //let all thread get ready
109: lt.test();
110: }
111: }
112:
113: class Agent extends Thread {
114: private static int count = 0;
115:
116: XmlAdderClient client = null;
117: List list = null;
118:
119: public Agent(ThreadGroup threadGroup, List list, String host,
120: int port) {
121: super (threadGroup, null, "Agent:" + ++count);
122: client = new XmlAdderClient(host, port);
123: this .list = list;
124: }
125:
126: public void run() {
127: synchronized (getThreadGroup()) {
128: try {
129: getThreadGroup().wait();
130: } catch (Exception e) {
131: System.err.println("Error is wait! " + e);
132: }
133: }
134: client.test();
135: list.add("" + client.getTime());
136: }
137: }
|