On 30 Jun 2011 there was a post on the official PowerCLI Blog about How to speed-up the execution of the first PowerCLI cmdlet. They talked about how the first PowerCLI cmdlet run in a PowerShell session is considerably slower than all other PowerCLI cmdlet calls in the same session, and how that was "due to the fact that the .NET framework compiles the underlying code on first use."
So, they listed out the commands for precompiling some assemblies, so that the first PowerCLI cmdlet call would not be extra slow due to just-in-time compilation. The commands add the compiled code to the "native image cache" on the local machine, and so only need performed once for the computer on which they are run. Note: that is once per computer per version, so when a new PowerCLI version comes out and you install the new version, you would compile the assembly for the new version.
I wanted to keep the speed alive when I upgraded to PowerCLI v5, but found no mention of doing this for the new release. So, I looked into it, and the commands for precompiling the XmlSerializers used by PowerCLI are roughly the same as those found at the PowerCLI Blog post, just with updating the version-specific items:
PS vN:\> C:\Windows\Microsoft.NET\Framework\v2.0.50727\ngen.exe install "VimService50.XmlSerializers, Version=5.0.0.0, Culture=neutral, PublicKeyToken= 10980b081e887e9f"
And, if running on a 64-bit OS, also run:
PS vN:\> C:\Windows\Microsoft.NET\Framework64\v2.0.50727\ngen. exe install "VimService50.XmlSerializers, Version=5.0.0.0, Culture=neutral, PublicKeyToken= 10980b081e887e9f"
These can both be run from PowerShell or a cmd prompt -- the Native Image Generator (ngen.exe) is doing the work. The VimService50.XmlSerializers info can be found in the assembly folder at C:\Windows\assembly\. There, one can get its Version and Public Key Token info used in the commands.
* Update - 02 Oct 2012:
With the release of PowerCLI v5.1 Release 1, there is a new XmlSerializers version to pre-compile to keep the speed streak alive. This new one is named VimService51.XmlSerializers, and the version is "5.1.0.0". So, here is another example of the speed difference of before and after using ngen.exe to precompile the XmlSerializers, along with the updated commandlines to run:
Speed results before:
Speed results after precompiling:
Further info abut using NGen can be found at the MSDN site http://msdn.microsoft.com/en-us/library/6t9t5wcf%28v=VS.100%29.aspx. There you can read all about:
The speed increases for the first PowerCLI cmdlet call in a PowerShell session after compiling were on par with those reported in the PowerCLI blog post for previous versions. Definitely worth running, to help keep away the slow-sies (SAS)!
* Update - 02 Oct 2012:
With the release of PowerCLI v5.1 Release 1, there is a new XmlSerializers version to pre-compile to keep the speed streak alive. This new one is named VimService51.XmlSerializers, and the version is "5.1.0.0". So, here is another example of the speed difference of before and after using ngen.exe to precompile the XmlSerializers, along with the updated commandlines to run:
Speed results before:
PS vN:\> Measure-Command {Connect-VIServer myVcenter.dom.com} ... TotalSeconds : 14.4464282 TotalMilliseconds : 14446.4282 PS vN:\> Measure-Command {Get-VMHost} ... TotalSeconds : 11.859625 TotalMilliseconds : 11859.625 PS vN:\> Measure-Command {Get-VMHost} ... TotalSeconds : 0.0677823 TotalMilliseconds : 67.7823The pre-compile commands for PowerCLI v5.1:
PS vN:\> C:\Windows\Microsoft.NET\Framework\v2.0.50727\ngen.exe install "VimService51.XmlSerializers, Version=5.1.0.0, Culture=neutral, PublicKeyToken=10980b081e887e9f"If running on a 64-bit OS, also run:
PS vN:\> C:\Windows\Microsoft.NET\Framework64\v2.0.50727\ngen.exe install "VimService51.XmlSerializers, Version=5.1.0.0, Culture=neutral, PublicKeyToken=10980b081e887e9f"
Speed results after precompiling:
PS vN:\> Measure-Command {Connect-VIServer myVcenter.dom.com} ... TotalSeconds : 2.6289308 TotalMilliseconds : 2628.9308 PS vN:\> Measure-Command {Get-VMHost} ... TotalSeconds : 0.4143238 TotalMilliseconds : 414.3238 PS vN:\> Measure-Command {Get-VMHost} ... TotalSeconds : 0.0890656 TotalMilliseconds : 89.0656End of update - 02 Oct 2012
Further info abut using NGen can be found at the MSDN site http://msdn.microsoft.com/en-us/library/6t9t5wcf%28v=VS.100%29.aspx. There you can read all about:
- how the precompilation helps with faster application startup
- loads faster, requires smaller initial working set
- displaying the Native Image Cache (seeing one/all compiled native images)
- uninstalling native images that have been compiled/installed
- a "Summary of Usage Considerations" -- pros and cons of using native images