Monday, February 04, 2008

IPAddress.Parse("127.0.0.1") vs. Dns.GetHostEntry("127.0.0.1")

Did you ever come to the point where you had to get several times an System.Net.IPEndPoint? The constructor takes an IPAddress and a specific Port which you select... Now guess what... if you handle with known IPAddresses within your Network you should not use Dns.GetHostEntry just because its very very very slow, as long you know the IP Address ;-). In almoast every online example I have seen so far online I always have seen usage of Dns.GetHostEntry.... people thats so wrong in my point of view and after my measurment - correct me if I'm wrong but I love to measure :-) !

Once you deal with other issues maybe there is no other way but since a while I grab the WWW around tcp and udp socket issues and one of this issues is to give a solution with object pooling while a unexpected load arrives on a web-server and it needs to open more then the expected amount of sockets to its server. So here we can have e.g. instead of the normal 20 open connections fast 120 - 150 additional sockets. This additional instances are there for a certain time amount and afterwards they get closed again but even then. If you need to be fast, then every millisecond counts since we are talking about scalability.

In general I gone compare about the following code lines:

IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 48888);

and

IPEndPoint ep = new IPEndPoint(Dns.GetHostEntry("127.0.0.1").AddressList[0], 48888);

I have done a small test to measure if this two options are really have such a hugh difference:

Run no. 1 with: 127.0.0.1

IP-Address Parse average: 6 milliseconds
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8

DNS GetHostEntry average: 125 milliseconds
56, 57, 66, 67, 68, 70, 70, 71, 71, 72, 78, 78, 79, 80, 81, 81, 82, 82, 83, 85, 86, 87, 88, 88, 91, 95, 96, 96, 97, 97, 98, 99, 99, 101, 102, 103, 104, 106, 110, 111, 113, 114, 116, 117, 118, 119, 122, 127, 128, 129, 130, 130, 131, 133, 143, 144, 144, 145, 145, 146, 146, 147, 148, 148, 149, 150, 155, 155, 156, 157, 158, 158, 159, 159, 160, 161, 161, 162, 162, 163, 164, 165, 166, 167, 169, 169, 170, 171, 172, 173, 173, 174, 175, 175, 176, 177, 177, 178, 178, 179,


Run no. 2 with: 192.168.xxx.21

IP-Address Parse average: 11 milliseconds
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12
DNS GetHostEntry average: 168 milliseconds
95, 99, 100, 101, 105, 106, 106, 109, 110, 112, 121, 121, 122, 123, 124, 125, 126, 132, 133, 134, 136, 137, 139, 141, 141, 143, 144, 146, 148, 153, 153, 155, 155, 156, 157, 157, 160, 163, 163, 164, 165, 166, 168, 168, 169, 170, 170, 173, 177, 177, 178, 179, 180, 180, 181, 181, 182, 182, 183, 184, 184, 185, 186, 186, 187, 187, 188, 191, 192, 193, 193, 194, 195, 195, 196, 197, 197, 198, 199, 200, 201, 202, 203, 203, 204, 205, 206, 206, 207, 207, 208, 208, 209, 210, 211, 211, 215, 217, 218, 218,

incredible how much the difference is.

The formatted code is available over here: http://www.ronischuetz.com/code/IpParseVsDnsGetHostEntry.html
and the c# version you can get from here:
http://www.ronischuetz.com/code/IpParseVsDnsGetHostEntry.cs.txt

1 comment:

NuBemet said...

Thank you very much!

Exactly what I for googling for!

Shared Cache - .Net Caching made easy

All information about Shared Cache is available here: http://www.sharedcache.com/. Its free and easy to use, we provide all sources at codeplex.

Facebook Badge