Skip to content

Instantly share code, notes, and snippets.

@gcorrao
Created June 9, 2011 19:25
Show Gist options
  • Save gcorrao/1017510 to your computer and use it in GitHub Desktop.
Save gcorrao/1017510 to your computer and use it in GitHub Desktop.
Java - test INetAddress speed against regular ping
import java.net.InetAddress;
import java.util.Arrays;
public class Reachable {
public static void main(String[] args) {
try {
long startTime;
boolean result;
System.out.println("InetAddress.getByName(args[0]).isReachable(1000);");
startTime=System.currentTimeMillis();
result= InetAddress.getByName(args[0]).isReachable(1000);
System.out.println("took: "+(System.currentTimeMillis()-startTime));
System.out.println("Result: "+result);
System.out.println();
System.out.println();
System.out.println("ping "+args[0]+" -c 1 -W 1");
startTime=System.currentTimeMillis();
Process p= Runtime.getRuntime().exec("ping "+args[0]+" -c 1 -W 1");
result= (p.waitFor()==0);
System.out.println("took: "+(System.currentTimeMillis()-startTime));
System.out.println("Result: "+result);
System.out.println();
System.out.println();
System.out.println("InetAddress.getByAddress(address).isReachable(1000);");
startTime=System.currentTimeMillis();
String[] tmp= args[0].split("\\.");
System.out.println(Arrays.toString(tmp));
byte[] address= new byte[]{Byte.parseByte(tmp[0]),Byte.parseByte(tmp[1]),Byte.parseByte(tmp[2]),Byte.parseByte(tmp[3])};
result= InetAddress.getByAddress(address).isReachable(1000);
System.out.println("took: "+(System.currentTimeMillis()-startTime));
System.out.println("Result: "+result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment