001: // $Id: DisconnectTest.java 1156 2007-04-15 08:10:39Z grro $
002: /*
003: * Copyright (c) xsocket.org, 2006. All rights reserved.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
020: * The latest copy of this software may be found on http://www.xsocket.org/
021: */
022: package org.xsocket.connection;
023:
024: import java.io.IOException;
025: import java.io.InputStreamReader;
026: import java.io.LineNumberReader;
027: import java.io.OutputStreamWriter;
028: import java.io.PrintWriter;
029: import java.net.Socket;
030: import java.nio.BufferUnderflowException;
031:
032: import org.xsocket.MaxReadSizeExceededException;
033: import org.xsocket.connection.BlockingConnection;
034: import org.xsocket.connection.IBlockingConnection;
035:
036: /**
037: *
038: * @author grro@xsocket.org
039: */
040: public final class BenchmarkingClients {
041:
042: private static final String DELIMITER = "\r\n";
043:
044: public static final void main(String... args) throws Exception {
045: if (args.length != 2) {
046: System.out
047: .println("usage java org.xsocket.stream.BenchmarkClient <host> <port>");
048: System.exit(-1);
049: }
050:
051: new BenchmarkingClients().launch(args[0], Integer
052: .parseInt(args[1]));
053: }
054:
055: public void launch(String host, int port) throws IOException {
056: compare(host, port, 10, 100, 1);
057:
058: System.out
059: .println("\n\n\nloops, dataSize, packages, direct, blocking, nonblocking");
060: compare(host, port, 10, 1000, 1);
061: compare(host, port, 10, 1000, 10);
062: compare(host, port, 1000, 1000, 1);
063: compare(host, port, 1000, 1000, 10);
064: compare(host, port, 50000, 1000, 2);
065: compare(host, port, 1000000, 50, 1);
066: }
067:
068: private void compare(String host, int port, int size, int loops,
069: int packages) throws IOException {
070: long timeDirect = callDirect(host, port, size, loops, packages);
071: long timeBlocking = callBlockingConnection(host, port, size,
072: loops, packages);
073: long timeNonBlocking = callNonBlockingConnection(host, port,
074: size, loops, packages);
075: System.out.println(loops + ", " + size + ", " + packages
076: + ", " + timeDirect + ", " + timeBlocking + ", "
077: + timeNonBlocking);
078: }
079:
080: static long callDirect(String host, int port, int dataSize,
081: int loops, int packages) throws IOException {
082: String data = new String(QAUtil.generateByteArray(dataSize));
083:
084: long start = System.currentTimeMillis();
085:
086: for (int i = 0; i < loops; i++) {
087: Socket socket = new Socket(host, port);
088: PrintWriter pw = new PrintWriter(new OutputStreamWriter(
089: socket.getOutputStream()));
090: LineNumberReader lnr = new LineNumberReader(
091: new InputStreamReader(socket.getInputStream()));
092:
093: for (int j = 0; j < packages; j++) {
094: pw.println(data);
095: pw.flush();
096:
097: String result = lnr.readLine();
098:
099: if (!data.equals(result)) {
100: System.out.println("error!");
101: }
102: }
103:
104: pw.close();
105: lnr.close();
106: socket.close();
107: }
108:
109: long end = System.currentTimeMillis();
110: return (end - start);
111: }
112:
113: static long callBlockingConnection(String host, int port,
114: int dataSize, int loops, int packages) throws IOException {
115: String data = new String(QAUtil.generateByteArray(dataSize));
116:
117: long start = System.currentTimeMillis();
118:
119: for (int i = 0; i < loops; i++) {
120: IBlockingConnection connection = new BlockingConnection(
121: host, port);
122: connection.setAutoflush(true);
123:
124: for (int j = 0; j < packages; j++) {
125: connection.write(data + DELIMITER);
126: String result = connection
127: .readStringByDelimiter(DELIMITER);
128:
129: if (!data.equals(result)) {
130: System.out.println("error!");
131: }
132: }
133:
134: connection.close();
135: }
136:
137: long end = System.currentTimeMillis();
138: return (end - start);
139: }
140:
141: static long callNonBlockingConnection(String host, int port,
142: int dataSize, int loops, int packages) throws IOException {
143: String data = new String(QAUtil.generateByteArray(dataSize));
144:
145: long start = System.currentTimeMillis();
146:
147: for (int i = 0; i < loops; i++) {
148: ClientHandler hdl = new ClientHandler();
149: INonBlockingConnection connection = new NonBlockingConnection(
150: host, port, hdl);
151: connection.setAutoflush(true);
152:
153: for (int j = 0; j < packages; j++) {
154: synchronized (hdl) {
155: connection.write(data + DELIMITER);
156: try {
157: hdl.wait();
158: } catch (InterruptedException ignore) {
159: }
160: }
161:
162: if (!data.equals(hdl.result)) {
163: System.out.println("error!");
164: }
165: }
166:
167: connection.close();
168: }
169:
170: long end = System.currentTimeMillis();
171: return (end - start);
172: }
173:
174: private static final class ClientHandler implements IDataHandler {
175:
176: private String result = null;
177:
178: public boolean onData(INonBlockingConnection connection)
179: throws IOException, BufferUnderflowException,
180: MaxReadSizeExceededException {
181: result = connection.readStringByDelimiter(DELIMITER);
182:
183: synchronized (this ) {
184: this .notify();
185: }
186:
187: return true;
188: }
189: }
190: }
|