Exceptions and spacing
This commit is contained in:
@@ -5,14 +5,14 @@ class Chronometer
|
|||||||
|
|
||||||
[void]AddBreakpoint([string[]]$Path, [int[]]$LineNumber)
|
[void]AddBreakpoint([string[]]$Path, [int[]]$LineNumber)
|
||||||
{
|
{
|
||||||
if(-not [string]::IsNullOrEmpty($Path))
|
if (-not [string]::IsNullOrEmpty($Path))
|
||||||
{
|
{
|
||||||
foreach($file in (Resolve-Path $Path -ea 0))
|
foreach ($file in (Resolve-Path $Path -ea 0))
|
||||||
{
|
{
|
||||||
$script = [MonitoredScript]@{Path=$file.Path}
|
$script = [MonitoredScript]@{Path = $file.Path}
|
||||||
$lines = $script.SetScript($file)
|
$lines = $script.SetScript($file)
|
||||||
|
|
||||||
if($null -ne $LineNumber)
|
if ( $null -ne $LineNumber )
|
||||||
{
|
{
|
||||||
$bpLine = $LineNumber
|
$bpLine = $LineNumber
|
||||||
}
|
}
|
||||||
@@ -26,7 +26,7 @@ class Chronometer
|
|||||||
$breakpointParam = @{
|
$breakpointParam = @{
|
||||||
Script = $file
|
Script = $file
|
||||||
Line = $bpLine
|
Line = $bpLine
|
||||||
Action = {[ScriptProfiler]::RecordExecution( $_) }
|
Action = { [ScriptProfiler]::RecordExecution( $_) }
|
||||||
}
|
}
|
||||||
$this.breakPoint += Set-PSBreakpoint @breakpointParam
|
$this.breakPoint += Set-PSBreakpoint @breakpointParam
|
||||||
}
|
}
|
||||||
@@ -35,7 +35,7 @@ class Chronometer
|
|||||||
|
|
||||||
[void]ClearBreakpoint()
|
[void]ClearBreakpoint()
|
||||||
{
|
{
|
||||||
if($null -ne $this.Breakpoint -and $this.Breakpoint.count -gt 0)
|
if ( $null -ne $this.Breakpoint -and $this.Breakpoint.count -gt 0 )
|
||||||
{
|
{
|
||||||
Remove-PSBreakpoint -Breakpoint $this.Breakpoint
|
Remove-PSBreakpoint -Breakpoint $this.Breakpoint
|
||||||
}
|
}
|
||||||
@@ -45,7 +45,7 @@ class Chronometer
|
|||||||
[void] AddExecution([hashtable]$Execution)
|
[void] AddExecution([hashtable]$Execution)
|
||||||
{
|
{
|
||||||
$script = $Execution.Breakpoint.Script
|
$script = $Execution.Breakpoint.Script
|
||||||
if($this.FileMap.ContainsKey($script))
|
if ( $this.FileMap.ContainsKey($script) )
|
||||||
{
|
{
|
||||||
# Each script tracks it's own execution times
|
# Each script tracks it's own execution times
|
||||||
$this.FileMap[$script].AddExecution($Execution)
|
$this.FileMap[$script].AddExecution($Execution)
|
||||||
@@ -54,7 +54,7 @@ class Chronometer
|
|||||||
|
|
||||||
[MonitoredScript[]] GetResults()
|
[MonitoredScript[]] GetResults()
|
||||||
{
|
{
|
||||||
foreach($node in $this.FileMap.Values)
|
foreach ( $node in $this.FileMap.Values )
|
||||||
{
|
{
|
||||||
$node.PostProcessing()
|
$node.PostProcessing()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ class MonitoredScript
|
|||||||
[int] SetScript([string]$Path)
|
[int] SetScript([string]$Path)
|
||||||
{
|
{
|
||||||
$lineNumber = 0
|
$lineNumber = 0
|
||||||
foreach($command in ( Get-Content -Path $Path ))
|
foreach ( $command in ( Get-Content -Path $Path ) )
|
||||||
{
|
{
|
||||||
$this.Line.Add( [ScriptLine]::New($command, $path, $lineNumber) )
|
$this.Line.Add( [ScriptLine]::New($command, $path, $lineNumber) )
|
||||||
$lineNumber++
|
$lineNumber++
|
||||||
@@ -35,7 +35,7 @@ class MonitoredScript
|
|||||||
$record.AddExecution($node)
|
$record.AddExecution($node)
|
||||||
|
|
||||||
# Calclate the delta in time
|
# Calclate the delta in time
|
||||||
if($this.lastNode)
|
if ( $this.lastNode )
|
||||||
{
|
{
|
||||||
$duration = $node.Elapsed - $this.lastNode.Elapsed
|
$duration = $node.Elapsed - $this.lastNode.Elapsed
|
||||||
}
|
}
|
||||||
@@ -45,7 +45,7 @@ class MonitoredScript
|
|||||||
}
|
}
|
||||||
|
|
||||||
# The delta is how long the last command ran
|
# The delta is how long the last command ran
|
||||||
if($this.lastRecord)
|
if ( $this.lastRecord )
|
||||||
{
|
{
|
||||||
$this.lastRecord.AddExecutionTime($duration)
|
$this.lastRecord.AddExecutionTime($duration)
|
||||||
$this.ExecutionTime += $duration
|
$this.ExecutionTime += $duration
|
||||||
@@ -59,15 +59,15 @@ class MonitoredScript
|
|||||||
{
|
{
|
||||||
$this.lastNode = $null
|
$this.lastNode = $null
|
||||||
$this.ExecutionTime = [TimeSpan]::Zero
|
$this.ExecutionTime = [TimeSpan]::Zero
|
||||||
foreach($node in $this.line)
|
foreach ( $node in $this.line )
|
||||||
{
|
{
|
||||||
$command = $node.text -replace '\s',''
|
$command = $node.text -replace '\s', ''
|
||||||
|
|
||||||
switch -Regex ($command)
|
switch -Regex ( $command )
|
||||||
{
|
{
|
||||||
'^}$|^}#|^$'
|
'^}$|^}#|^$'
|
||||||
{
|
{
|
||||||
if($node.HitCount -eq 0)
|
if ( $node.HitCount -eq 0 )
|
||||||
{
|
{
|
||||||
$node.HitCount = $this.lastNode.HitCount
|
$node.HitCount = $this.lastNode.HitCount
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,12 +33,12 @@ class ScriptLine
|
|||||||
$this.HitCount += 1
|
$this.HitCount += 1
|
||||||
$this.Average = $this.Duration.TotalMilliseconds / $this.HitCount
|
$this.Average = $this.Duration.TotalMilliseconds / $this.HitCount
|
||||||
|
|
||||||
if($Duration -lt $this.Min)
|
if ( $Duration -lt $this.Min )
|
||||||
{
|
{
|
||||||
$this.Min = $Duration
|
$this.Min = $Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
if($Duration -gt $this.Max)
|
if ( $Duration -gt $this.Max )
|
||||||
{
|
{
|
||||||
$this.Max = $Duration
|
$this.Max = $Duration
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
|
|
||||||
function Write-ScriptLine
|
function Write-ScriptLine
|
||||||
{
|
{
|
||||||
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost","")]
|
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost", "")]
|
||||||
[cmdletbinding()]
|
[cmdletbinding()]
|
||||||
param(
|
param(
|
||||||
[scriptline]
|
[scriptline]
|
||||||
@@ -10,18 +9,18 @@ function Write-ScriptLine
|
|||||||
$ErrorAt = [int]::MaxValue
|
$ErrorAt = [int]::MaxValue
|
||||||
)
|
)
|
||||||
|
|
||||||
if($line)
|
if ( $line )
|
||||||
{
|
{
|
||||||
$Color = 'Green'
|
$Color = 'Green'
|
||||||
if($line.HitCount -eq 0)
|
if ( $line.HitCount -eq 0 )
|
||||||
{
|
{
|
||||||
$Color = 'Gray'
|
$Color = 'Gray'
|
||||||
}
|
}
|
||||||
elseif($line.Average -ge $ErrorAt)
|
elseif ( $line.Average -ge $ErrorAt )
|
||||||
{
|
{
|
||||||
$Color = 'Red'
|
$Color = 'Red'
|
||||||
}
|
}
|
||||||
elseif($line.Average -ge $WarningAt)
|
elseif ( $line.Average -ge $WarningAt )
|
||||||
{
|
{
|
||||||
$Color = 'Yellow'
|
$Color = 'Yellow'
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
|
|
||||||
|
|
||||||
function Format-Chronometer
|
function Format-Chronometer
|
||||||
{
|
{
|
||||||
<#
|
<#
|
||||||
@@ -11,21 +9,21 @@ function Format-Chronometer
|
|||||||
$resultes = Get-Chronometer -Path $script.fullname -ScriptBlock {Invoke-Pester C:\workspace\PSGraph}
|
$resultes = Get-Chronometer -Path $script.fullname -ScriptBlock {Invoke-Pester C:\workspace\PSGraph}
|
||||||
$results | Format-Chronometer -WarnAt 20 -ErrorAt 200
|
$results | Format-Chronometer -WarnAt 20 -ErrorAt 200
|
||||||
#>
|
#>
|
||||||
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost","")]
|
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost", "")]
|
||||||
[cmdletbinding(DefaultParameterSetName='Script')]
|
[cmdletbinding(DefaultParameterSetName = 'Script')]
|
||||||
param(
|
param(
|
||||||
# This is a MonitoredScript object from Get-Chronometer
|
# This is a MonitoredScript object from Get-Chronometer
|
||||||
[Parameter(
|
[Parameter(
|
||||||
ValueFromPipeline=$true,
|
ValueFromPipeline = $true,
|
||||||
ParameterSetName='Script'
|
ParameterSetName = 'Script'
|
||||||
)]
|
)]
|
||||||
[MonitoredScript[]]
|
[MonitoredScript[]]
|
||||||
$InputObject,
|
$InputObject,
|
||||||
|
|
||||||
# This is a ScriptLine object from a MonitoredScript object
|
# This is a ScriptLine object from a MonitoredScript object
|
||||||
[Parameter(
|
[Parameter(
|
||||||
ValueFromPipeline=$true,
|
ValueFromPipeline = $true,
|
||||||
ParameterSetName='Line'
|
ParameterSetName = 'Line'
|
||||||
)]
|
)]
|
||||||
[ScriptLine[]]
|
[ScriptLine[]]
|
||||||
$Line,
|
$Line,
|
||||||
@@ -45,21 +43,28 @@ function Format-Chronometer
|
|||||||
|
|
||||||
process
|
process
|
||||||
{
|
{
|
||||||
foreach($script in $InputObject)
|
try
|
||||||
{
|
|
||||||
if($script.ExecutionTime -ne [TimeSpan]::Zero -or $ShowAll)
|
|
||||||
{
|
|
||||||
Write-Host ''
|
|
||||||
Write-Host "Script: $($script.Path)" -ForegroundColor Green
|
|
||||||
Write-Host "Execution Time: $($script.ExecutionTime)" -ForegroundColor Green
|
|
||||||
|
|
||||||
$script.line | Format-Chronometer -WarningAt $WarningAt -ErrorAt $ErrorAt
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach($command in $Line)
|
|
||||||
{
|
{
|
||||||
Write-ScriptLine $command -WarningAt $WarningAt -ErrorAt $ErrorAt
|
foreach ( $script in $InputObject )
|
||||||
|
{
|
||||||
|
if ( $script.ExecutionTime -ne [TimeSpan]::Zero -or $ShowAll )
|
||||||
|
{
|
||||||
|
Write-Host ''
|
||||||
|
Write-Host "Script: $($script.Path)" -ForegroundColor Green
|
||||||
|
Write-Host "Execution Time: $($script.ExecutionTime)" -ForegroundColor Green
|
||||||
|
|
||||||
|
$script.line | Format-Chronometer -WarningAt $WarningAt -ErrorAt $ErrorAt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ( $command in $Line )
|
||||||
|
{
|
||||||
|
Write-ScriptLine $command -WarningAt $WarningAt -ErrorAt $ErrorAt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
$PSCmdlet.ThrowTerminatingError($PSItem)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,47 +23,56 @@ function Get-Chronometer
|
|||||||
$LineNumber = $null,
|
$LineNumber = $null,
|
||||||
|
|
||||||
# The script to start the scrupt or execute other commands
|
# The script to start the scrupt or execute other commands
|
||||||
[Parameter(Position=0)]
|
[Parameter(Position = 0)]
|
||||||
[alias('Script','CommandScript')]
|
[alias('Script', 'CommandScript')]
|
||||||
[scriptblock]
|
[scriptblock]
|
||||||
$ScriptBlock
|
$ScriptBlock
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if( $null -eq $Path )
|
process
|
||||||
{
|
{
|
||||||
$Path = Get-ChildItem -Recurse -Include *.psm1,*.ps1 -File
|
try
|
||||||
}
|
|
||||||
|
|
||||||
if($Path.FullName)
|
|
||||||
{
|
|
||||||
$Path = $Path.FullName
|
|
||||||
}
|
|
||||||
|
|
||||||
$Chronometer = [Chronometer]::New()
|
|
||||||
|
|
||||||
Write-Verbose "Setting breapoints"
|
|
||||||
$Chronometer.AddBreakpoint($Path,$LineNumber)
|
|
||||||
|
|
||||||
if($null -ne $Chronometer.breakPoint -and $null -ne $ScriptBlock)
|
|
||||||
{
|
|
||||||
Write-Verbose "Executing Script"
|
|
||||||
[ScriptProfiler]::Start()
|
|
||||||
[void] $ScriptBlock.Invoke($Path)
|
|
||||||
|
|
||||||
Write-Verbose "Clearing Breapoints"
|
|
||||||
$Chronometer.ClearBreakpoint()
|
|
||||||
|
|
||||||
Write-Verbose "Processing data"
|
|
||||||
foreach($node in [ScriptProfiler]::Queue.GetEnumerator())
|
|
||||||
{
|
{
|
||||||
$Chronometer.AddExecution($node)
|
if ( $null -eq $Path )
|
||||||
}
|
{
|
||||||
|
$Path = Get-ChildItem -Recurse -Include *.psm1, *.ps1 -File
|
||||||
|
}
|
||||||
|
|
||||||
Write-Output $Chronometer.GetResults()
|
if ( $Path.FullName )
|
||||||
}
|
{
|
||||||
else
|
$Path = $Path.FullName
|
||||||
{
|
}
|
||||||
Write-Warning "Parsing files did not result in any breakpoints"
|
|
||||||
|
$Chronometer = [Chronometer]::New()
|
||||||
|
|
||||||
|
Write-Verbose "Setting breapoints"
|
||||||
|
$Chronometer.AddBreakpoint($Path, $LineNumber)
|
||||||
|
|
||||||
|
if ( $null -ne $Chronometer.breakPoint -and $null -ne $ScriptBlock )
|
||||||
|
{
|
||||||
|
Write-Verbose "Executing Script"
|
||||||
|
[ScriptProfiler]::Start()
|
||||||
|
[void] $ScriptBlock.Invoke($Path)
|
||||||
|
|
||||||
|
Write-Verbose "Clearing Breapoints"
|
||||||
|
$Chronometer.ClearBreakpoint()
|
||||||
|
|
||||||
|
Write-Verbose "Processing data"
|
||||||
|
foreach ( $node in [ScriptProfiler]::Queue.GetEnumerator() )
|
||||||
|
{
|
||||||
|
$Chronometer.AddExecution($node)
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Output $Chronometer.GetResults()
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Write-Warning "Parsing files did not result in any breakpoints"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
$PSCmdlet.ThrowTerminatingError($PSItem)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user