Refactoring
This commit is contained in:
@@ -1,10 +1,76 @@
|
||||
class Chronometer
|
||||
{
|
||||
[hashtable]$FileMap = @{}
|
||||
$Breakpoint = @()
|
||||
|
||||
[void]AddFile([string[]]$Path)
|
||||
[void]AddBreakpoint([string[]]$Path)
|
||||
{
|
||||
foreach($file in (Resolve-Path $Path -ea 0))
|
||||
{
|
||||
$script = [MonitoredScript]@{Path=$file.Path}
|
||||
$lines = $script.SetScript($file)
|
||||
|
||||
$this.fileMap[$file.Path] = $script
|
||||
|
||||
$breakpointParam = @{
|
||||
Script = $file
|
||||
Line = (1..$lines)
|
||||
Action = {[ScriptProfiler]::RecordExecution( $_) }
|
||||
}
|
||||
$this.breakPoint += Set-PSBreakpoint @breakpointParam
|
||||
}
|
||||
}
|
||||
|
||||
[void]ClearBreakpoint()
|
||||
{
|
||||
Remove-PSBreakpoint $this.Breakpointbreakpoint
|
||||
}
|
||||
|
||||
[void] AddExecution([hashtable]$Execution)
|
||||
{
|
||||
$this.FileMap[$Execution.Breakpoint.Script].AddExecution($Execution)
|
||||
}
|
||||
}
|
||||
|
||||
class MonitoredScript
|
||||
{
|
||||
[string]$Path
|
||||
[System.Collections.ArrayList]$Line
|
||||
|
||||
$lastNode = $null
|
||||
$lastRecord = $null
|
||||
|
||||
MonitoredScript()
|
||||
{
|
||||
$this.Line = [System.Collections.ArrayList]::New()
|
||||
}
|
||||
|
||||
[int] SetScript([string]$Path)
|
||||
{
|
||||
Get-Content -Path $Path | %{ $this.Line.Add( [ScriptLine]@{text=$_})}
|
||||
return $this.Line.Count()
|
||||
}
|
||||
|
||||
[void] AddExecution([hashtable]$node)
|
||||
{
|
||||
$record = $this.Line[$node.Breakpoint.Line-1]
|
||||
$record.LineNumber = $node.Breakpoint.Line - 1
|
||||
|
||||
if($this.lastNode)
|
||||
{
|
||||
$duration = $node.ElapsedMilliseconds - $this.lastNode.ElapsedMilliseconds
|
||||
}
|
||||
else
|
||||
{
|
||||
$duration = $node.ElapsedMilliseconds
|
||||
}
|
||||
|
||||
if($this.lastRecord)
|
||||
{
|
||||
$this.lastRecord.AddExecutionTime($duration)
|
||||
}
|
||||
|
||||
$this.lastRecord = $record
|
||||
$this.lastNode = $node
|
||||
}
|
||||
}
|
||||
@@ -21,47 +21,17 @@ function Get-Chronometer
|
||||
$ScriptBlock
|
||||
)
|
||||
|
||||
$breakPoint = @()
|
||||
$fileMap = @{}
|
||||
|
||||
foreach($file in (Resolve-Path $Path -ea 0))
|
||||
{
|
||||
$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( $_) }
|
||||
}
|
||||
$Chronometer = [Chronometer]::New()
|
||||
$breakPoint = $Chronometer.AddBreakpoint($Path)
|
||||
|
||||
[ScriptProfiler]::Start()
|
||||
[void] $ScriptBlock.Invoke()
|
||||
|
||||
Remove-PSBreakpoint $breakpoint
|
||||
|
||||
#$fileMap | ConvertTo-Json
|
||||
|
||||
$Chronometer.ClearBreakpoint()
|
||||
|
||||
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
|
||||
$Chronometer.AddExecution($node)
|
||||
}
|
||||
|
||||
foreach($script in $fileMap.Keys)
|
||||
|
||||
Reference in New Issue
Block a user