Comparing Win32_PingStatus vs [System.Net.NetworkInformation.Ping]
Back again with another comparison between .NET and WMI performance. While WMI can be very useful and often the only way to accomplish some remote oriented tasks, you will rarely hear anyone that has worked with WMI classes for long brag about its performance. Whenever possible I try to discover alternatives to WMI and I’ve found what I think is another candidate.
The most popular method of pinging in powershell so far seems to be the Win32_PingStatus WMI class since it’s easily available in powershell and doesn’t involve the use of an external command (ping.exe). For the test, I’m just going to create an array of 50 items all containing the same computername (which for this test is pointing to the local machine). Ping responses should be as nearly instantaneously as possible. I’ll grant there are a lot of factors that could affect such a simple test, but the results are promising enough.
The Array:
$g = @("testmachine")*50
So we now have an array of 50 identical computer names. Let’s pipe it through the most common ping function that I’ve seen which I found at http://blog.sapien.com/current/2006/12/8/more-pinging.html.
Function Ping-Name {
PROCESS {
$wmi = get-wmiobject -query "SELECT * FROM Win32_PingStatus WHERE Address = '$_'"
if ($wmi.StatusCode -eq 0) { $_ }
}
}
And now for a trip through Measure-Command:
23# measure-command { $g | Ping-Name }
Days : 0
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 542
Ticks : 5427557
TotalDays : 6.28189467592593E-06
TotalHours : 0.000150765472222222
TotalMinutes : 0.00904592833333333
TotalSeconds : 0.5427557
TotalMilliseconds : 542.7557
Not bad. That didn’t take very long. Now let’s look at a ping function that uses the [System.Net.NetworkInformation.Ping] .NET class.
Function Ping-Net {
Begin { $ping = new-object System.Net.NetworkInformation.Ping }
Process {
if ($ping.Send($_)) {
$_
}
}
}
And again, let’s test the performance using Measure-Command:
41# measure-command { $g | Ping-Net }
Days : 0
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 38
Ticks : 385107
TotalDays : 4.45725694444444E-07
TotalHours : 1.06974166666667E-05
TotalMinutes : 0.000641845
TotalSeconds : 0.0385107
TotalMilliseconds : 38.5107
Even faster. I don’t know about you, but I like faster even if it is barely noticeable J