Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize the startup of powershell sessions #174

Closed
wants to merge 3 commits into from

Conversation

petemounce
Copy link

@petemounce petemounce commented Jun 19, 2019

Before doing this, a powershell session takes around 6s to begin on an n1-standard-4, over winrm. Afterwards, on the order of 200ms.

For us, using winrm as the packer/ansible communicator, this reduced provisioning time from around 25m to around 8m.

Not sure if this is the place to call it, compared to it happening inside an on-boot scheduled task; happy to move it, obviously.

All credit to @ca-johnson for actually finding this optimisation after an extended session of profiling!

More detail:

Before doing this, a powershell session takes around 6s to begin on an n1-standard-4, over winrm

Afterwards, on the order of 200ms

For us, using winrm as the packer/ansible communicator, this reduced provisioning time from around 25m to around 8m

Not sure if this is the place to call it, compared to it happening inside an on-boot scheduled task; happy to move it, obviously.
if (! $_.location) {continue}
$Name = Split-Path $_.location -leaf
if ($Name.startswith("Microsoft.PowerShell.")) {
Write-Progress -Activity "Native Image Installation" -Status "$name"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Write-Progress can cause issues when there is no console attached (as is the case here), if you want this stuff printed out you can use the Write-Log function

@@ -287,6 +305,7 @@ else {
Write-Log 'Running startup scripts from metadata server.'
Create-GCEStartup
Invoke-ExternalCommand schtasks /run /tn GCEStartup
Optimize-PowershellAssemblies
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whats' the time for this to run, since this isn't required for instance setup it would make more sense to add before Wait-Job (if its <2s) or after if greater. you could also start it in the background if that makes more sense.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On an n1-standard-4 I've seen this take around 30s. I've moved this down to just after Wait-Job - is that what you meant?

@@ -245,6 +245,24 @@ function Create-GCEStartup {
$folder.RegisterTaskDefinition('GCEStartup',$task,6,'System',$null,5) | Out-Null
}

function Optimize-PowershellAssemblies {
# NGEN powershell assembly, improves startup time of powershell by 10x
$old_path = $env:path
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty aggressive, do you have to overwrite the path? This only really matters if you run this function in the background.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that was just simplest. I've adjusted.

don't brutalise the PATH; move the call later in the sequence
@adjackura
Copy link
Contributor

I'm holding off on this for now as I am looking into other ways to handle this. If nothing pans out I'll come back to this PR.

@adjackura
Copy link
Contributor

Please test out the latest images, this change should be in the base image build now and initial startup time should have actually gone down as a result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants