import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UdpSpeedTest {
public static void main(String[] args) throws Exception {
if (args.length < 2) {
System.out.println("Usage: UdpSpeedTest address length");
System.exit(0);
}
byte[] data = new byte[Integer.valueOf(args[1])];
DatagramSocket socket = new DatagramSocket();
DatagramPacket packet = new DatagramPacket(data, data.length,
InetAddress.getByName(args[0]), 20000);
long t0, t1;
t0 = System.nanoTime();
for (long i = 0; i < 10000L; i++) {
socket.send(packet);
}
t1 = System.nanoTime();
System.out.println("UDP packet length: " + data.length + ", speed: "
+ (int) (data.length * 1E13 * 8 / ((t1 - t0)) / 1024 / 1024)
+ " MBps");
socket.close();
}
}