Performance

How to speed-up the execution of the first PowerCLI cmdlet

Some users may experience delays on the first run of a PowerCLI cmdlet for each new PowerShell session. This delay is more noticeable when running on a 64-bit OS. You can see this with a simple script that measures the run time:

Connect-VIServer …

Measure-Command { Get-VMHost } | fl TotalSeconds

Measure-Command { Get-VMHost } | fl TotalSeconds

The first run of a Get-VMHost will be slower than the second one. This will only happen once in a PowerShell process.

Here are some results:

On a 64-bit OS:

#> Measure-Command { Get-VMHost } | fl TotalSeconds

TotalSeconds : 23.4781061

#> Measure-Command { Get-VMHost } | fl TotalSeconds

TotalSeconds : 1.0336202

On a 32-bit OS:

#> Measure-Command { Get-VMHost } | fl TotalSeconds

TotalSeconds : 8.578462

#> Measure-Command { Get-VMHost } | fl TotalSeconds

TotalSeconds : 1.1123916

This issue is due to the fact that the .Net framework compiles the underlying code on first use.

To improve this, by precompiling, you can run the following commands at your command prompt (you need to run the prompt as administrator):

C:\Windows\Microsoft.NET\Framework\v2.0.50727\ngen.exe install "VimService41.XmlSerializers, Version=4.1.0.0, Culture=neutral, PublicKeyToken=10980b081e887e9f"

C:\Windows\Microsoft.NET\Framework\v2.0.50727\ngen.exe install "VimService40.XmlSerializers, Version=4.0.0.0, Culture=neutral, PublicKeyToken=10980b081e887e9f"

C:\Windows\Microsoft.NET\Framework\v2.0.50727\ngen.exe install "VimService25.XmlSerializers, Version=2.5.0.0, Culture=neutral, PublicKeyToken=10980b081e887e9f"

If you are running on 64-bit OS, you need to run the following as well:

C:\Windows\Microsoft.NET\Framework64\v2.0.50727\ngen.exe install "VimService41.XmlSerializers, Version=4.1.0.0, Culture=neutral, PublicKeyToken=10980b081e887e9f"

C:\Windows\Microsoft.NET\Framework64\v2.0.50727\ngen.exe install "VimService40.XmlSerializers, Version=4.0.0.0, Culture=neutral, PublicKeyToken=10980b081e887e9f"

C:\Windows\Microsoft.NET\Framework64\v2.0.50727\ngen.exe install "VimService25.XmlSerializers, Version=2.5.0.0, Culture=neutral, PublicKeyToken=10980b081e887e9f"

Note that you need to do this only once for a PowerCLI installation. It is persisted even across OS restarts. The precompiled assemblies are stored in GAC, that is, all users and applications that use them will be affected by this change.

Here is a summary of the results after the execution of the above commands:

On a 64-bit OS:

Before:

TotalSeconds : 23.4781061

TotalSeconds : 1.0336202

After:

TotalSeconds : 3.437742

TotalSeconds : 1.0284757

On a 32-bit OS:

Before:

TotalSeconds : 8.578462

TotalSeconds : 1.1123916

After:

TotalSeconds : 3.592183

TotalSeconds : 1.2297652

You can check if there is any speed-up for you, by using the PowerCLI script at the beginning to measure your timings.

If there is no significant change in the timings for you, you should undo these settings by replacing the install options of the ngen commands with the uninstall option.

The tool that is used above is ngen.exe. It is a tool that is installed with .Net Framework. You can read more about it here.