Skip to content

Instantly share code, notes, and snippets.

@JustinGrote
Last active March 20, 2025 12:47
Show Gist options
  • Save JustinGrote/484fcb1324f58ec359b9291b4431c962 to your computer and use it in GitHub Desktop.
Save JustinGrote/484fcb1324f58ec359b9291b4431c962 to your computer and use it in GitHub Desktop.
Benchmark your profile using Profiler

Powershell Profile Benchmarking

Copy the above code into your profile, then run Enable-ProfileBenchmark and start a new pwsh process.

You will get the top 10 slowest commands in your profile: image

You can get a more detailed view by going to https://speedscope.app and uploading the json file as mentioned in the output. image

#region ProfileBenchmark
if ($ENV:PWSH_PROFILE_BENCHMARK -and -not $ENV:PWSH_PROFILE_BENCHMARK_RUN) {
Write-Host -Fore Magenta 'πŸ‘·β€β™‚οΈ BENCHMARKING PROFILE SETUP'
Invoke-WebRequest bit.ly/modulefast | Invoke-Expression
Install-ModuleFast profiler
# This will ensure we don't end up in a setup loop
Write-Host -Fore Magenta 'πŸ“ˆ BENCHMARKING PROFILE'
$profilePath = $MyInvocation.MyCommand.Source
$profileTrace = Trace-Script -ExportPath TEMP:/pwsh-profile -ScriptBlock {
$ENV:PWSH_PROFILE_BENCHMARK_RUN = $true
. $profilePath
}
Write-Host -Fore Magenta 'βœ… BENCHMARKING PROFILE COMPLETE. Results saved to $profileTrace'
Write-Host -Fore DarkGreen "⏱️ PROFILE LOAD TIME: $([int]($profileTrace.TotalDuration.TotalMilliseconds))ms"
Write-Host -Fore Magenta "=== πŸ“ˆ PROFILE BENCHMARK TOP 10 BY DURATION ==="
$profileTrace.Top50SelfDuration
| Where-Object text -NE '. $PROFILE.CurrentUserAllHosts'
| Select-Object -First 10
| Format-Table
| Out-String
| Write-Host -ForegroundColor Cyan
Write-Host -Fore Magenta "==============================================="
#Cleanup
$profilePath = $null
return
}
function Enable-ProfileBenchmark {
[Environment]::SetEnvironmentVariable('PWSH_PROFILE_BENCHMARK', 'true', 'User')
Write-Host -Fore Magenta 'πŸ“ˆ BENCHMARKING PROFILE ENABLED. START A NEW PWSH PROCESS.'
}
function Disable-ProfileBenchMark {
[Environment]::SetEnvironmentVariable('PWSH_PROFILE_BENCHMARK', '', 'User')
Write-Host -Fore Magenta 'πŸ“ˆ BENCHMARKING PROFILE DISABLED'
}
#endregion ProfileBenchmark
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment