KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jgroups.tests;
2
3 import java.io.IOException JavaDoc;
4 import java.net.DatagramPacket JavaDoc;
5 import java.net.DatagramSocket JavaDoc;
6 import java.net.InetAddress JavaDoc;
7
8 /**
9  * Determine the fragmentation size of a system. Can be used offline, then set properties
10  * FRAG.frag_size and NAKACK.max_xmit_size
11  * @author Bela Ban Dec 11
12  * @author 2003
13  * @version $Id: DetermineFragSize.java,v 1.2 2003/12/12 18:34:48 belaban Exp $
14  */

15 public class DetermineFragSize {
16
17
18     static int senseMaxFragSize() {
19         int upper=4096;
20         int lower=0;
21         int highest_failed=-1;
22         DatagramSocket JavaDoc sock;
23         byte[] buf;
24         DatagramPacket JavaDoc packet;
25         InetAddress JavaDoc local_addr;
26         final int num_iterations=15;
27
28         try {
29             sock=new DatagramSocket JavaDoc();
30             local_addr=InetAddress.getLocalHost();
31         }
32         catch(Exception JavaDoc ex) {
33             System.err.println("failed creating DatagramSocket: " + ex);
34             return lower;
35         }
36
37         for(int i=0; i < num_iterations && lower < upper; i++) { // iterations to approximate frag_size
38
try {
39                 buf=new byte[upper];
40                 // System.out.println("** upper=" + upper + " (lower=" + lower + ")");
41
packet=new DatagramPacket JavaDoc(buf, buf.length, local_addr, 9);
42                 sock.send(packet);
43                 lower=Math.max(lower, upper);
44                 System.out.println("-- trying " + lower + " [OK]");
45                 upper=upper * 2;
46                 if(highest_failed > -1)
47                     upper=Math.min(highest_failed, upper);
48             }
49             catch(IOException JavaDoc io_ex) {
50                 if(highest_failed > -1)
51                     highest_failed=Math.min(highest_failed, upper); // never exceed max_upper
52
else
53                     highest_failed=upper;
54                 upper=(upper + lower) / 2;
55             }
56             catch(Throwable JavaDoc ex) {
57                 ex.printStackTrace();
58                 break;
59             }
60         }
61         return lower;
62     }
63
64
65     public static void main(String JavaDoc[] args) {
66         DatagramSocket JavaDoc sock;
67         DatagramPacket JavaDoc packet;
68         int size=0, frag_size=0;
69         byte[] buf;
70
71         try {
72             size=senseMaxFragSize();
73             System.out.println("-- fine tuning (starting at " + size + "):");
74             sock=new DatagramSocket JavaDoc();
75             for(; ;) {
76                 buf=new byte[size];
77                 packet=new DatagramPacket JavaDoc(buf, buf.length, InetAddress.getLocalHost(), 9);
78                 sock.send(packet);
79                 // System.out.print(size + " ");
80
// System.out.println(size + " [OK]");
81
frag_size=size;
82                 size++;
83             }
84         }
85         catch(Throwable JavaDoc t) {
86             // System.out.println(size + " [FAIL]");
87
// t.printStackTrace();
88
}
89         System.out.println("\n***** fragmentation size on your system is " + frag_size + " bytes *******\n");
90     }
91 }
92
Popular Tags