-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathLoadTest.java
More file actions
117 lines (92 loc) · 2.69 KB
/
LoadTest.java
File metadata and controls
117 lines (92 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* This file is part of the QuickServer library
* Copyright (C) 2003-2005 QuickServer.org
*
* Use, modification, copying and distribution of this software is subject to
* the terms and conditions of the GNU Lesser General Public License.
* You should have received a copy of the GNU LGP License along with this
* library; if not, you can download a copy from <http://www.quickserver.org/>.
*
* For questions, suggestions, bug-reports, enhancement-requests etc.
* visit http://www.quickserver.org
*
*/
package xmladder;
import java.io.*;
import java.net.*;
import java.util.*;
public class LoadTest {
private Agent agents[] = null;
private final ThreadGroup agentGroup = new ThreadGroup("AgentGroup");
private List times = null;
public void init(int count, String host, int port) {
System.out.print("Starting init..");
times = Collections.synchronizedList(new ArrayList(count));
agents = new Agent[count];
for(int i=0;i<count;i++) {
agents[i] = new Agent(agentGroup, times, host, port);
agents[i].start();
}
System.out.println(" "+agentGroup.activeCount()+" ready.");
}
public void test() throws InterruptedException {
int count = agents.length;
System.out.print("Starting test..");
long stime = System.currentTimeMillis();
synchronized(agentGroup) {
agentGroup.notifyAll();
}
for(int i=0;i<count;i++) {
agents[i].join();
}
long etime = System.currentTimeMillis();
System.out.println("Done\n");
long time = etime - stime;
System.out.println("Total Time : "+time+"ms");
time = time/count;
System.out.println("Avg. Time : "+time+"ms\n");
/*
System.out.println("\nEach Client time..\n");
for(int i=0;i<count;) {
System.out.print(times.get(i)+",");
if(++i%10==0) System.out.println("");
}
*/
}
public static void main(String args[]) throws Exception {
LoadTest lt = new LoadTest();
String host = "127.0.0.1";
int port = 2222;
int count = 50;
if(args.length>0)
count = Integer.parseInt(args[0]);
if(args.length>1)
host = args[1];
if(args.length>2)
port = Integer.parseInt(args[2]);
lt.init(count, host, port);
Thread.sleep(200); //let all thread get ready
lt.test();
}
}
class Agent extends Thread {
private static int count = 0;
XmlAdderClient client = null;
List list = null;
public Agent(ThreadGroup threadGroup, List list, String host, int port) {
super(threadGroup, null, "Agent:"+ ++count);
client = new XmlAdderClient(host, port);
this.list = list;
}
public void run() {
synchronized(getThreadGroup()) {
try {
getThreadGroup().wait();
} catch(Exception e) {
System.err.println("Error is wait! "+e);
}
}
client.test();
list.add(""+client.getTime());
}
}