Added core functions and classes
This commit is contained in:
40
Chronometer/Classes/ScriptLine.ps1
Normal file
40
Chronometer/Classes/ScriptLine.ps1
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
|
||||||
|
class ScriptLine
|
||||||
|
{
|
||||||
|
[float] $Milliseconds = 0
|
||||||
|
[float] $HitCount = 0
|
||||||
|
[float] $Min = [float]::MaxValue
|
||||||
|
[float] $Max = [float]::MinValue
|
||||||
|
[float] $Average = 0
|
||||||
|
[int] $LineNumber
|
||||||
|
[string] $Path
|
||||||
|
[string] $Text
|
||||||
|
|
||||||
|
[void]AddExecutionTime([float]$Milliseconds)
|
||||||
|
{
|
||||||
|
$this.Milliseconds += $Milliseconds
|
||||||
|
$this.HitCount += 1
|
||||||
|
$this.Average = $this.Milliseconds / $this.HitCount
|
||||||
|
|
||||||
|
if($Milliseconds -lt $this.Min)
|
||||||
|
{
|
||||||
|
$this.Min = $Milliseconds
|
||||||
|
}
|
||||||
|
|
||||||
|
if($Milliseconds -gt $this.Max)
|
||||||
|
{
|
||||||
|
$this.Max = $Milliseconds
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[string] ToString()
|
||||||
|
{
|
||||||
|
$values = @(
|
||||||
|
$this.Milliseconds
|
||||||
|
$this.HitCount
|
||||||
|
$this.Average
|
||||||
|
$this.Text
|
||||||
|
)
|
||||||
|
return ("[{0:0000}ms,{1:0000},{2:0000}ms] {3}" -f $values)
|
||||||
|
}
|
||||||
|
}
|
||||||
19
Chronometer/Classes/ScriptProfiler.ps1
Normal file
19
Chronometer/Classes/ScriptProfiler.ps1
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
class ScriptProfiler {
|
||||||
|
|
||||||
|
static [System.Collections.Queue] $Queue
|
||||||
|
static [System.Diagnostics.Stopwatch] $Timer
|
||||||
|
|
||||||
|
static [void] Start()
|
||||||
|
{
|
||||||
|
[ScriptProfiler]::Queue = New-Object System.Collections.Queue
|
||||||
|
[ScriptProfiler]::Timer = [System.Diagnostics.Stopwatch]::StartNew()
|
||||||
|
}
|
||||||
|
|
||||||
|
static [void] RecordExecution ([System.Management.Automation.LineBreakpoint]$InputObject)
|
||||||
|
{
|
||||||
|
[ScriptProfiler]::Queue.Enqueue(@{
|
||||||
|
Breakpoint = $InputObject
|
||||||
|
ElapsedMilliseconds = [ScriptProfiler]::Timer.ElapsedMilliseconds
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
62
Chronometer/Public/Get-Chronometer.ps1
Normal file
62
Chronometer/Public/Get-Chronometer.ps1
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
function Get-Chronometer
|
||||||
|
{
|
||||||
|
[CmdletBinding()]
|
||||||
|
param(
|
||||||
|
[string[]]
|
||||||
|
$Path,
|
||||||
|
|
||||||
|
[scriptblock]
|
||||||
|
$CommandScript
|
||||||
|
)
|
||||||
|
|
||||||
|
$breakPoint = @()
|
||||||
|
$fileMap = @{}
|
||||||
|
|
||||||
|
foreach($file in (Resolve-Path $Path))
|
||||||
|
{
|
||||||
|
$fileMap[$file.Path] = @( Get-Content -Path $file | %{[ScriptLine]@{text=$_;path=$file.path}})
|
||||||
|
|
||||||
|
$lines = $fileMap[$file.Path].count
|
||||||
|
$breakPoint += Set-PSBreakpoint -Script $file -Line (1..$lines) -Action {[ScriptProfiler]::RecordExecution( $_) }
|
||||||
|
}
|
||||||
|
|
||||||
|
[ScriptProfiler]::Start()
|
||||||
|
[void] $CommandScript.Invoke()
|
||||||
|
|
||||||
|
Remove-PSBreakpoint $breakpoint
|
||||||
|
|
||||||
|
#$fileMap | ConvertTo-Json
|
||||||
|
|
||||||
|
|
||||||
|
foreach($node in [ScriptProfiler]::Queue.GetEnumerator())
|
||||||
|
{
|
||||||
|
$record = $fileMap[$node.Breakpoint.Script][$node.Breakpoint.Line-1]
|
||||||
|
$record.LineNumber = $node.Breakpoint.Line - 1
|
||||||
|
|
||||||
|
if($lastNode)
|
||||||
|
{
|
||||||
|
$duration = $node.ElapsedMilliseconds - $lastNode.ElapsedMilliseconds
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$duration = $node.ElapsedMilliseconds
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if($lastRecord)
|
||||||
|
{
|
||||||
|
$lastRecord.AddExecutionTime($duration)
|
||||||
|
}
|
||||||
|
|
||||||
|
$lastRecord = $record
|
||||||
|
$lastNode = $node
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach($script in $fileMap.Keys)
|
||||||
|
{
|
||||||
|
foreach($line in $fileMap[$script])
|
||||||
|
{
|
||||||
|
Write-Output $line
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user