KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > NetworkUtilization


1 // $Id: NetworkUtilization.java,v 1.4 2004/07/05 14:15:22 belaban Exp $
2

3 package org.jgroups.tests;
4
5 import org.jgroups.util.Util;
6
7 import java.net.*;
8
9 /**
10  * @author Bela Ban
11  */

12 public class NetworkUtilization {
13     boolean sender=false;
14     InetAddress mcast_addr;
15     int mcast_port=7500;
16     long start, stop;
17     long num_received=0;
18     int received_packet_size=0;
19     MulticastSocket mcast_sock=null;
20
21
22     class MyTimer extends Thread JavaDoc {
23
24         MyTimer() {
25         }
26
27         public void run() {
28
29             if(mcast_sock != null)
30                 mcast_sock.close();
31
32             long stop=System.currentTimeMillis();
33             long diff=stop-start;
34             double secs=diff/1000.0;
35             double num_kb_received=num_received * received_packet_size / 1000.0;
36
37             Util.sleep(200);
38             System.out.println("** took " + secs + " secs to receive " + num_received + " msgs (" +
39                     (num_received / secs) + " msgs/sec)\n" +
40                     "** throughput: " + num_kb_received / secs + " KB/sec");
41         }
42     }
43
44
45     byte[] createBuffer(int size) {
46         byte[] buf=new byte[size];
47         for(int i=0; i < buf.length; i++) {
48             buf[i]='.';
49         }
50         return buf;
51     }
52
53     public void start(boolean sender, int packet_size) throws Exception JavaDoc {
54         this.sender=sender;
55
56         mcast_addr=InetAddress.getByName("228.8.8.8");
57
58         if(sender) {
59             DatagramSocket sock=new DatagramSocket();
60             byte[] buf=createBuffer(packet_size);
61
62             System.out.println("-- starting to send packets");
63             while(true) {
64                 DatagramPacket p=new DatagramPacket(buf, buf.length, mcast_addr, mcast_port);
65                 sock.send(p);
66             }
67         }
68         else {
69             Runtime.getRuntime().addShutdownHook(new MyTimer());
70             byte[] buf=new byte[1000000];
71             boolean first=true;
72             DatagramPacket p=new DatagramPacket(buf, buf.length);
73             mcast_sock=new MulticastSocket(mcast_port);
74             // sock.setLoopbackMode(true); // disable reception of own mcasts
75
mcast_sock.joinGroup(mcast_addr);
76             System.out.println("-- joined group " + mcast_addr + ':' + mcast_port + ", waiting for packets\n" +
77                     "(press ctrl-c to kill)");
78             while(true) {
79                 p.setData(buf);
80                 try {
81                     mcast_sock.receive(p);
82                 }
83                 catch(SocketException ex) {
84                     break;
85                 }
86                 if(first) {
87                     first=false;
88                     start=System.currentTimeMillis();
89                     received_packet_size=p.getLength();
90                 }
91                 num_received++;
92                 //System.out.println("-- received " + p.getLength() + " bytes from " + p.getAddress() + ":" + p.getPort());
93
if(num_received % 1000 == 0)
94                     System.out.println(num_received);
95             }
96         }
97
98     }
99
100
101
102     public static void main(String JavaDoc[] args) {
103         boolean sender=false;
104         int packet_size=10;
105
106         for(int i=0; i < args.length; i++) {
107             if("-help".equals(args[i])) {
108                 help();
109                 return;
110             }
111             if("-sender".equals(args[i])) {
112                 sender=true;
113                 continue;
114             }
115             if("-size".equals(args[i])) {
116                 packet_size=Integer.parseInt(args[++i]);
117                 continue;
118             }
119             help();
120             return;
121         }
122
123         try {
124             new NetworkUtilization().start(sender, packet_size);
125         }
126         catch(Exception JavaDoc e) {
127             e.printStackTrace();
128         }
129     }
130
131     static void help() {
132         System.out.println("NetworkUtilization [-help] [-sender] [-size <packet size in bytes>]");
133     }
134
135
136 }
137
Popular Tags