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

Speedup cold runs of PSSA by using a runspace pool and parallelizing the slowest rule (AvoidAlias) #1178

Conversation

bergmeister
Copy link
Collaborator

@bergmeister bergmeister commented Mar 17, 2019

PR Summary

We can decide to defer this performance improvement PR after the 1.18.0 release (or maybe merge only the runspace improvement?)

In total this can cut the time in half of analyzing a big script like e.g. build.psm1 the first time in a new shell.

  • Cold runs (where the command info cache is not initialized) are sped up by 20% by re-using a runspace pool, thanks @daxian-dbw for the original idea. Creation and opening of it incurs a one-off initialization cost of around 50ms, which is negligible in most cases where the script is of a decent length (10-100 lines plus). A runspace pool of 6 is required for that due to 5 rules being able to make queries to it in parallel.
  • The weakest link in the chain of PSSA runs is the slowest rule, I profiled rule execution and the AvoidAlias rule is the slowest rule. The slowness comes from a lot of calls to the command info cache, therefore this part is being parallelized and the runspace pool count is increased to 10 for that. This leads to a 30% improvement. The rule execution times before in this paraellization improvement changed the longest running rule from 7884 ms to 5164 (they are run in parallel, therefore the slowest rule is the weakest link):
Avoid Using Positional Parameters - Before: 3485ms - After: 4292ms
Should Process- Before: 4391ms - After: 5164ms
Use Cmdlet Correctly- Before: 4980ms - After: 4261 ms
Avoid Using Cmdlet Aliases or omitting the 'Get-' prefix. - Before: 7884ms - After: 3138ms

I did measure of course multiple times, the numbers are just an example of an average/normal run

PR Checklist

…ng PowerShell for CommandInfo.

There are only 4 rules that could call the CommandInfoCache, therefore sizing the pool to 5. The pool creation is synchronous and adds 15ms of initialization overhead, which we can neglect I think.
…nsive command lookup in AvoidAlias, which is the most expensive rule. To accomodate the higher parallel demand, increase runspace pool.
Rules/AvoidAlias.cs Outdated Show resolved Hide resolved
Rules/AvoidAlias.cs Outdated Show resolved Hide resolved
Rules/AvoidAlias.cs Outdated Show resolved Hide resolved
Rules/AvoidAlias.cs Outdated Show resolved Hide resolved
Engine/Helper.cs Outdated Show resolved Hide resolved
@bergmeister bergmeister requested a review from JamesWTruher May 29, 2019 08:44
…nalyzer into Performance_RunspacePool_AndSpeedupAvoidAlias

Resolved by taking upstream changes
# Conflicts:
#	Rules/AvoidAlias.cs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants