-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdemo2.ps1
33 lines (24 loc) · 1.37 KB
/
demo2.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
## Improving performance
Import-Module $PSScriptRoot/Profiler/Profiler.psd1 -Force
# This is the script to be profiled. Provide a path to script, and provide
# parameters if you need them. You can also profile any other code. Like
# importing a module and running a function from it.
$scriptBlock = { & "$PSScriptRoot/demo-scripts/MyScript.ps1" }
# When making changes to your code, use `if` to wrap the new
# improved code and keep the old code in `else`. Profiler defines
# global variables based on the hashtable below, and can automatically
# switch between the code before and after changes to compare it.
$flag = @{ _profiler = $true }
## Comparing performance
# Runs the script mulitiple times, switching between the code before
# and after changes:
Invoke-Script -ScriptBlock $scriptBlock -Preheat 0 -Repeat 3 -Flag $flag
# At this point you probably improved your performance a bit. Run the profiler again,
# this time providing the -Flag to enable running the new code.
# Runs the script 1 time, with 1 warm up run using the code After the changes.
# You can switch back to before changes by adding `-Before`.
$trace = Trace-Script -ScriptBlock $scriptBlock -Flag $flag
# Shows the top 50 lines that take the most percent of the run
$trace.Top50Duration | Format-Table
# Try it for yourself by running the commands from this demo, or by invoking this file as
# . ./demo2.ps1