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