001: // $Id: NetworkUtilization.java,v 1.4 2004/07/05 14:15:22 belaban Exp $
002:
003: package org.jgroups.tests;
004:
005: import org.jgroups.util.Util;
006:
007: import java.net.*;
008:
009: /**
010: * @author Bela Ban
011: */
012: public class NetworkUtilization {
013: boolean sender = false;
014: InetAddress mcast_addr;
015: int mcast_port = 7500;
016: long start, stop;
017: long num_received = 0;
018: int received_packet_size = 0;
019: MulticastSocket mcast_sock = null;
020:
021: class MyTimer extends Thread {
022:
023: MyTimer() {
024: }
025:
026: public void run() {
027:
028: if (mcast_sock != null)
029: mcast_sock.close();
030:
031: long stop = System.currentTimeMillis();
032: long diff = stop - start;
033: double secs = diff / 1000.0;
034: double num_kb_received = num_received
035: * received_packet_size / 1000.0;
036:
037: Util.sleep(200);
038: System.out.println("** took " + secs + " secs to receive "
039: + num_received + " msgs (" + (num_received / secs)
040: + " msgs/sec)\n" + "** throughput: "
041: + num_kb_received / secs + " KB/sec");
042: }
043: }
044:
045: byte[] createBuffer(int size) {
046: byte[] buf = new byte[size];
047: for (int i = 0; i < buf.length; i++) {
048: buf[i] = '.';
049: }
050: return buf;
051: }
052:
053: public void start(boolean sender, int packet_size) throws Exception {
054: this .sender = sender;
055:
056: mcast_addr = InetAddress.getByName("228.8.8.8");
057:
058: if (sender) {
059: DatagramSocket sock = new DatagramSocket();
060: byte[] buf = createBuffer(packet_size);
061:
062: System.out.println("-- starting to send packets");
063: while (true) {
064: DatagramPacket p = new DatagramPacket(buf, buf.length,
065: mcast_addr, mcast_port);
066: sock.send(p);
067: }
068: } else {
069: Runtime.getRuntime().addShutdownHook(new MyTimer());
070: byte[] buf = new byte[1000000];
071: boolean first = true;
072: DatagramPacket p = new DatagramPacket(buf, buf.length);
073: mcast_sock = new MulticastSocket(mcast_port);
074: // sock.setLoopbackMode(true); // disable reception of own mcasts
075: mcast_sock.joinGroup(mcast_addr);
076: System.out.println("-- joined group " + mcast_addr + ':'
077: + mcast_port + ", waiting for packets\n"
078: + "(press ctrl-c to kill)");
079: while (true) {
080: p.setData(buf);
081: try {
082: mcast_sock.receive(p);
083: } catch (SocketException ex) {
084: break;
085: }
086: if (first) {
087: first = false;
088: start = System.currentTimeMillis();
089: received_packet_size = p.getLength();
090: }
091: num_received++;
092: //System.out.println("-- received " + p.getLength() + " bytes from " + p.getAddress() + ":" + p.getPort());
093: if (num_received % 1000 == 0)
094: System.out.println(num_received);
095: }
096: }
097:
098: }
099:
100: public static void main(String[] args) {
101: boolean sender = false;
102: int packet_size = 10;
103:
104: for (int i = 0; i < args.length; i++) {
105: if ("-help".equals(args[i])) {
106: help();
107: return;
108: }
109: if ("-sender".equals(args[i])) {
110: sender = true;
111: continue;
112: }
113: if ("-size".equals(args[i])) {
114: packet_size = Integer.parseInt(args[++i]);
115: continue;
116: }
117: help();
118: return;
119: }
120:
121: try {
122: new NetworkUtilization().start(sender, packet_size);
123: } catch (Exception e) {
124: e.printStackTrace();
125: }
126: }
127:
128: static void help() {
129: System.out
130: .println("NetworkUtilization [-help] [-sender] [-size <packet size in bytes>]");
131: }
132:
133: }
|