Last active
September 5, 2022 21:27
-
-
Save TechDufus/9116187b246aef1b8087caea6a8e4730 to your computer and use it in GitHub Desktop.
Test PowerShell Profile Performance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Perform a benchtest of your PowerShell profile. | |
.DESCRIPTION | |
Load Powershell (or Preview) X number of times with NO profile, and with profile, and compare the average loading times. | |
.PARAMETER Count | |
Specify the number of consoles to load for testing. | |
.PARAMETER Preview | |
Specify whether to test again pwsh-preview or not. | |
With this present, the tests will use pwsh-preview. | |
.EXAMPLE | |
Test-PowerShellProfilePerformance -Count 50 | |
Description | |
----------- | |
Loop through testing your powershell profile 50 times. | |
This is 50 times PER console. With profile and without. | |
.NOTES | |
Author: matthewjdegarmo | |
Github: https://github.com/matthewjdegarmo | |
#> | |
Function Test-PowerShellProfilePerformance() { | |
[CmdletBinding()] | |
Param( | |
[Parameter()] | |
$Count = 100, | |
[Parameter()] | |
[Switch] $Preview | |
) | |
Begin { | |
If ($Preview.IsPresent) { | |
$Pwsh = 'pwsh-preview' | |
} Else { | |
$Pwsh = 'pwsh' | |
} | |
If (-Not(Get-Command $Pwsh -ErrorAction SilentlyContinue)) { | |
Throw "The command '$Pwsh' does not exist on this system." | |
} | |
} | |
Process { | |
$Result = @{} | |
$NoProfile = 0 | |
1..$Count | ForEach-Object { | |
$Percent = $($_ / $Count) * 100 | |
Write-Progress -Id 1 -Activity "$($Pwsh.ToUpper()) - No Profile" -PercentComplete $Percent | |
$NoProfile += (Measure-Command { | |
&$Pwsh -noprofile -command 1 | |
}).TotalMilliseconds | |
} | |
Write-Progress -id 1 -Activity "$($Pwsh.ToUpper()) - No Profile" -Completed | |
$Result['NoProfile_Average'] = "$($NoProfile/$Count)`ms" | |
$WithProfile = 0 | |
1..$Count | ForEach-Object { | |
$Percent = $($_ / $Count) * 100 | |
Write-Progress -Id 1 -Activity "$($Pwsh.ToUpper()) - With Profile" -PercentComplete $Percent | |
$WithProfile += (Measure-Command { | |
&$Pwsh -command 1 | |
}).TotalMilliseconds | |
} | |
Write-Progress -id 1 -activity "$($Pwsh.ToUpper()) - With Profile" -Completed | |
$Result['Profile_Average'] = "$($WithProfile/$Count)`ms" | |
Return $Result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment