001: package org.xsocket.stream;
002:
003: import java.io.IOException;
004: import java.io.InputStreamReader;
005: import java.io.LineNumberReader;
006: import java.io.OutputStreamWriter;
007: import java.io.PrintWriter;
008: import java.net.Socket;
009: import java.util.ArrayList;
010: import java.util.List;
011:
012: import org.xsocket.DataConverter;
013:
014: public class LoadClient {
015:
016: /*
017: * http://www.minq.se/products/pureload/tuning/connections.html
018: *
019: * win xp
020: * http://support.microsoft.com/kb/314053/de
021: * http://support.microsoft.com/kb/196271/de
022: */
023:
024: private String host = null;
025: private int port = 0;
026: private int countWorkers = 0;
027: private int countConnections = 0;
028: private int loops = 0;
029: private boolean openNew = false;
030: private int dataSize = 0;
031: private int waitBetweenCalls = 0;
032:
033: private Statistic statistic = null;
034:
035: public static void main(String... args) throws Exception {
036: if (args.length != 8) {
037: System.out
038: .println("usage org.xsocket.stream.LoadClient <host> <port> <workers> <cons> <openNew?> <loops> <dataSize> <waitTime>");
039: System.exit(-1);
040: }
041:
042: LoadClient loadClient = new LoadClient();
043: loadClient.host = args[0];
044: loadClient.port = Integer.parseInt(args[1]);
045: loadClient.countWorkers = Integer.parseInt(args[2]);
046: loadClient.countConnections = Integer.parseInt(args[3]);
047: loadClient.openNew = Boolean.parseBoolean(args[4]);
048: loadClient.loops = Integer.parseInt(args[5]);
049: loadClient.dataSize = Integer.parseInt(args[6]);
050: loadClient.waitBetweenCalls = Integer.parseInt(args[7]);
051:
052: loadClient.testLoad();
053: }
054:
055: public void testLoad() throws Exception {
056:
057: System.out.println("running test client with (server=" + host
058: + ":" + port + "; loops=" + loops + ")");
059:
060: System.out
061: .println("\r\n---------------------------------------");
062: System.out.println("Load-info:");
063: System.out.println("data packet size "
064: + dataSize + " bytes");
065: System.out.println("concurrent client threads "
066: + countWorkers);
067: System.out.println("concurrent connections "
068: + countConnections * countWorkers);
069: System.out.println("new connection for each request? "
070: + openNew);
071: System.out.println("wait time between client calls "
072: + DataConverter.toFormatedDuration(waitBetweenCalls));
073: System.out
074: .println("---------------------------------------\r\n");
075:
076: List<Worker> workers = new ArrayList<Worker>();
077:
078: System.out.println("preparing workers (init connctions)...");
079: for (int i = 0; i < countWorkers; i++) {
080: Worker worker = new Worker(host, port, countConnections,
081: openNew, loops, dataSize, waitBetweenCalls);
082: workers.add(worker);
083: worker.prepare();
084: }
085:
086: statistic = new Statistic();
087:
088: System.out.println("run workers");
089: for (Worker worker : workers) {
090: Thread t = new Thread(worker);
091: t.start();
092: }
093:
094: boolean allFinished;
095: do {
096: allFinished = true;
097: TestUtil.sleep(1000);
098:
099: for (Worker worker : workers) {
100: allFinished = allFinished & worker.isRunning();
101: }
102:
103: } while (!allFinished);
104:
105: }
106:
107: Socket createClientSocket(String host, int port) throws IOException {
108: return new Socket(host, port);
109: }
110:
111: private final class Worker implements Runnable {
112:
113: private boolean isRunning = true;
114:
115: private String host = null;
116: private int port = 0;
117: private int countConnections = 0;
118: private boolean openNew = false;
119: private int loops = 0;
120: private int dataSize = 0;
121: private int waitBetweenCalls = 0;
122:
123: private ArrayList<ClientConnection> connections = new ArrayList<ClientConnection>();
124:
125: Worker(String host, int port, int countConnections,
126: boolean openNew, int loops, int dataSize,
127: int waitBetweenCalls) throws IOException {
128: this .host = host;
129: this .port = port;
130: this .countConnections = countConnections;
131: this .openNew = openNew;
132: this .loops = loops;
133: this .dataSize = dataSize;
134: this .waitBetweenCalls = waitBetweenCalls;
135: }
136:
137: public void prepare() {
138: for (int i = 0; i < countConnections; i++) {
139: newConnection();
140: }
141: }
142:
143: @SuppressWarnings("unchecked")
144: public void run() {
145:
146: String data = new String(TestUtil
147: .generatedByteArray(dataSize));
148:
149: for (int i = 0; i < loops; i++) {
150: ArrayList<ClientConnection> copy = (ArrayList<ClientConnection>) connections
151: .clone();
152: for (ClientConnection connection : copy) {
153: try {
154: String request = data + i;
155:
156: String response = null;
157: long elapsed = 0;
158:
159: if (openNew) {
160: long start = System.currentTimeMillis();
161: ClientConnection con = new ClientConnection(
162: host, port);
163: response = con.send(request);
164: elapsed = System.currentTimeMillis()
165: - start;
166: con.close();
167:
168: } else {
169: long start = System.currentTimeMillis();
170: response = connection.send(request);
171: elapsed = System.currentTimeMillis()
172: - start;
173: }
174:
175: if (response == null) {
176: throw new Exception("null response");
177: }
178:
179: if (request.equals(response)) {
180: statistic.addValue((int) elapsed);
181: } else {
182: throw new Exception("wrong response: "
183: + response);
184: }
185:
186: } catch (Exception e) {
187: System.out.println("error " + e.toString()
188: + ";");
189: connection.close();
190: connections.remove(connection);
191: newConnection();
192: }
193:
194: if (waitBetweenCalls > 0) {
195: TestUtil.sleep(waitBetweenCalls);
196: }
197: }
198: }
199:
200: isRunning = false;
201: }
202:
203: public boolean isRunning() {
204: return isRunning;
205: }
206:
207: private ClientConnection newConnection() {
208: ClientConnection connection = null;
209: do {
210: try {
211: connection = new ClientConnection(host, port);
212: } catch (Exception ex) {
213: try {
214: Thread.sleep(100);
215: } catch (InterruptedException ignore) {
216: }
217: }
218: } while (connection == null);
219:
220: connections.add(connection);
221: return connection;
222: }
223: }
224:
225: private static final class ClientConnection {
226: private Socket socket = null;
227: private LineNumberReader lnr = null;
228: private PrintWriter pw = null;
229:
230: ClientConnection(String host, int port) throws IOException {
231: socket = new Socket(host, port);
232: socket.setReuseAddress(true);
233: lnr = new LineNumberReader(new InputStreamReader(socket
234: .getInputStream()));
235: pw = new PrintWriter(new OutputStreamWriter(socket
236: .getOutputStream()));
237: }
238:
239: public String send(String data) throws IOException {
240: pw.println(data);
241: pw.flush();
242: return lnr.readLine();
243: }
244:
245: public void close() {
246: try {
247: lnr.close();
248: pw.close();
249: socket.close();
250: } catch (Exception ignore) {
251: }
252: }
253: }
254: }
|