Exceptions and spacing

This commit is contained in:
KevinMarquette
2017-05-02 23:50:22 -07:00
parent e4822cb810
commit 958ada59e4
6 changed files with 95 additions and 82 deletions

View File

@@ -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()
} }

View File

@@ -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
} }

View File

@@ -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
} }

View File

@@ -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'
} }

View File

@@ -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,9 +43,11 @@ function Format-Chronometer
process process
{ {
foreach($script in $InputObject) try
{ {
if($script.ExecutionTime -ne [TimeSpan]::Zero -or $ShowAll) foreach ( $script in $InputObject )
{
if ( $script.ExecutionTime -ne [TimeSpan]::Zero -or $ShowAll )
{ {
Write-Host '' Write-Host ''
Write-Host "Script: $($script.Path)" -ForegroundColor Green Write-Host "Script: $($script.Path)" -ForegroundColor Green
@@ -57,9 +57,14 @@ function Format-Chronometer
} }
} }
foreach($command in $Line) foreach ( $command in $Line )
{ {
Write-ScriptLine $command -WarningAt $WarningAt -ErrorAt $ErrorAt Write-ScriptLine $command -WarningAt $WarningAt -ErrorAt $ErrorAt
} }
} }
catch
{
$PSCmdlet.ThrowTerminatingError($PSItem)
}
}
} }

View File

@@ -23,19 +23,22 @@ 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 ( $null -eq $Path )
{
$Path = Get-ChildItem -Recurse -Include *.psm1, *.ps1 -File
} }
if($Path.FullName) if ( $Path.FullName )
{ {
$Path = $Path.FullName $Path = $Path.FullName
} }
@@ -43,9 +46,9 @@ function Get-Chronometer
$Chronometer = [Chronometer]::New() $Chronometer = [Chronometer]::New()
Write-Verbose "Setting breapoints" Write-Verbose "Setting breapoints"
$Chronometer.AddBreakpoint($Path,$LineNumber) $Chronometer.AddBreakpoint($Path, $LineNumber)
if($null -ne $Chronometer.breakPoint -and $null -ne $ScriptBlock) if ( $null -ne $Chronometer.breakPoint -and $null -ne $ScriptBlock )
{ {
Write-Verbose "Executing Script" Write-Verbose "Executing Script"
[ScriptProfiler]::Start() [ScriptProfiler]::Start()
@@ -55,7 +58,7 @@ function Get-Chronometer
$Chronometer.ClearBreakpoint() $Chronometer.ClearBreakpoint()
Write-Verbose "Processing data" Write-Verbose "Processing data"
foreach($node in [ScriptProfiler]::Queue.GetEnumerator()) foreach ( $node in [ScriptProfiler]::Queue.GetEnumerator() )
{ {
$Chronometer.AddExecution($node) $Chronometer.AddExecution($node)
} }
@@ -66,4 +69,10 @@ function Get-Chronometer
{ {
Write-Warning "Parsing files did not result in any breakpoints" Write-Warning "Parsing files did not result in any breakpoints"
} }
}
catch
{
$PSCmdlet.ThrowTerminatingError($PSItem)
}
}
} }