07_PSProfiler2-in-powershell5-demo.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
34
35
$ErrorActionPreference = 'Stop'

# .\build.ps1

Get-Module Tracer, Profiler | Remove-Module

Import-Module $PSScriptRoot/Profiler.psm1
 
$trace2 = Trace-Script { & "$PSScriptRoot/scripts/hello.ps1" } -old

$trace = Trace-Script { & "$PSScriptRoot/scripts/hello.ps1" } -old

if (-not $trace) { 
    throw "Trace is null something is wrong."
}

Write-Host -ForegroundColor Blue "Trace is done. Processing the it via Get-Profile"
$profiles = Get-Profile -Trace $trace # -Path $hello
Write-Host -ForegroundColor Blue "Get-Profile is done."

Write-Host -ForegroundColor Magenta Trace events $trace.Count 
$profiles.Top10 | ft

break 

$profiles.Files | Select-Object -Property Path
# hello.ps1
$profiles.Files[2].Profile | Format-Table -Property Line, Duration, HitCount, Text


break 

# but how do we know what is slow? Well it's simple:
$profiles.Top10 |
    Format-Table -Property Percent, HitCount, Duration, Average, Line, Text, CommandHits