001: package org.jgroups.tests.perf;
002:
003: import java.io.*;
004: import java.net.InetAddress;
005: import java.net.ServerSocket;
006: import java.net.Socket;
007: import java.text.NumberFormat;
008:
009: /**
010: * Tool to measure TCP throughput, similar to iperf
011: * @author Bela Ban
012: * @version $Id: JPerf.java,v 1.2 2006/01/24 16:26:40 belaban Exp $
013: */
014: public class JPerf {
015: boolean sender;
016: int num;
017: InetAddress local_addr;
018: int local_port;
019: int remote_port;
020: InetAddress remote_addr;
021: int size = 1000;
022: int incr = 1000;
023:
024: static NumberFormat f;
025:
026: static {
027: f = NumberFormat.getNumberInstance();
028: f.setGroupingUsed(false);
029: f.setMaximumFractionDigits(2);
030: }
031:
032: private void start(boolean sender, int num, int size,
033: String local_addr, int local_port, String remote_addr,
034: int remote_port, int receivebuf, int sendbuf)
035: throws IOException {
036: this .sender = sender;
037: this .num = num;
038: this .size = size;
039: this .local_addr = InetAddress.getByName(local_addr);
040: this .local_port = local_port;
041: this .remote_addr = InetAddress.getByName(remote_addr);
042: this .remote_port = remote_port;
043:
044: incr = num / 10;
045:
046: if (sender) {
047: System.out.println("-- creating socket to "
048: + this .remote_addr + ":" + this .remote_port);
049: Socket sock = new Socket(this .remote_addr, remote_port,
050: this .local_addr, 0);
051: sock.setReceiveBufferSize(receivebuf);
052: sock.setSendBufferSize(sendbuf);
053: DataOutputStream out = new DataOutputStream(
054: new BufferedOutputStream(sock.getOutputStream()));
055: byte[] buf = new byte[size];
056: int cnt = 1;
057: System.out.println("-- sending " + num + " messages");
058: for (int i = 0; i < num; i++) {
059: out.write(buf, 0, buf.length);
060: out.flush();
061: if (cnt % incr == 0)
062: System.out.println("-- sent " + cnt + " messages");
063: cnt++;
064: }
065: } else {
066: ServerSocket srv_sock = new ServerSocket(remote_port, 10,
067: this .local_addr);
068: System.out.println("-- waiting for " + num
069: + " messages on "
070: + srv_sock.getLocalSocketAddress());
071: Socket client_sock = srv_sock.accept();
072: client_sock.setReceiveBufferSize(receivebuf);
073: client_sock.setSendBufferSize(sendbuf);
074: System.out.println("-- accepted connection from "
075: + client_sock.getRemoteSocketAddress());
076: DataInputStream in = new DataInputStream(
077: new BufferedInputStream(client_sock
078: .getInputStream()));
079: byte[] buf = new byte[size];
080: int counter = 0;
081: long start = 0, stop;
082: while (counter < num) {
083: in.readFully(buf, 0, buf.length);
084: if (start == 0)
085: start = System.currentTimeMillis();
086: counter++;
087: if (counter % incr == 0)
088: System.out.println("-- received " + counter);
089: }
090: stop = System.currentTimeMillis();
091: long diff = stop - start;
092: double msgs_sec = (counter / (diff / 1000.0));
093: System.out.println("\nreceived " + counter
094: + " messages in " + diff + "ms ("
095: + f.format(msgs_sec) + " msgs/sec)");
096: double throughput = num * size / (diff / 1000.0) / 1000.0;
097: if (throughput < 1000)
098: System.out.println("throughput: "
099: + f.format(throughput) + "KB/sec");
100: else {
101: throughput /= 1000.0;
102: System.out.println("throughput: "
103: + f.format(throughput) + "MB/sec");
104: }
105: }
106: }
107:
108: static void help() {
109: System.out
110: .println("JPerf [-help] [-sender] [-num <number of msgs] [-size <bytes>] [-local_addr <interface>] [-local_port <port]"
111: + "[-remote_addr <IP addr>] [-remote_port <port>] [-receivebuf <bytes>] [-sendbuf <bytes>]");
112: }
113:
114: public static void main(String[] args) {
115: boolean sender = false;
116: int num = 100000;
117: String local_addr = "127.0.0.1";
118: int local_port = 5000;
119: int size = 1000;
120: int remote_port = 10000;
121: int receivebuf = 200000, sendbuf = 200000;
122: String remote_addr = "127.0.0.1";
123:
124: for (int i = 0; i < args.length; i++) {
125: if (args[i].equals("-sender")) {
126: sender = true;
127: continue;
128: }
129: if (args[i].equals("-num")) {
130: num = Integer.parseInt(args[++i]);
131: continue;
132: }
133: if (args[i].equals("-size")) {
134: size = Integer.parseInt(args[++i]);
135: continue;
136: }
137: if (args[i].equals("-local_addr")) {
138: local_addr = args[++i];
139: continue;
140: }
141: if (args[i].equals("-remote_addr")) {
142: remote_addr = args[++i];
143: continue;
144: }
145: if (args[i].equals("-local_port")) {
146: local_port = Integer.parseInt(args[++i]);
147: continue;
148: }
149: if (args[i].equals("-remote_port")) {
150: remote_port = Integer.parseInt(args[++i]);
151: continue;
152: }
153: if (args[i].equals("-receivebuf")) {
154: receivebuf = Integer.parseInt(args[++i]);
155: continue;
156: }
157: if (args[i].equals("-sendbuf")) {
158: sendbuf = Integer.parseInt(args[++i]);
159: continue;
160: }
161: help();
162: return;
163: }
164: try {
165: new JPerf().start(sender, num, size, local_addr,
166: local_port, remote_addr, remote_port, receivebuf,
167: sendbuf);
168: } catch (IOException e) {
169: e.printStackTrace();
170: }
171: }
172:
173: }
|