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
Conversation
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.
sysprep/instance_setup.ps1
Outdated
if (! $_.location) {continue} | ||
$Name = Split-Path $_.location -leaf | ||
if ($Name.startswith("Microsoft.PowerShell.")) { | ||
Write-Progress -Activity "Native Image Installation" -Status "$name" |
There was a problem hiding this comment.
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
sysprep/instance_setup.ps1
Outdated
@@ -287,6 +305,7 @@ else { | |||
Write-Log 'Running startup scripts from metadata server.' | |||
Create-GCEStartup | |||
Invoke-ExternalCommand schtasks /run /tn GCEStartup | |||
Optimize-PowershellAssemblies |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
sysprep/instance_setup.ps1
Outdated
@@ -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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
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. |
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. |
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: