Merge pull request #7 from KevinMarquette/develop !deploy

Fully reworked the execution time tracking to use timespan objects
This commit is contained in:
Kevin Marquette
2017-02-06 23:39:56 -08:00
committed by GitHub
4 changed files with 70 additions and 28 deletions

View File

@@ -6,37 +6,45 @@ class MonitoredScript
hidden $lastNode = $null hidden $lastNode = $null
hidden $lastRecord = $null hidden $lastRecord = $null
[float]$ExecutionTime = 0 [timespan]$ExecutionTime = [timespan]::Zero
[int]$LinesOfCode = 0 [int]$LinesOfCode = 0
MonitoredScript() MonitoredScript()
{ {
$this.Line =New-Object 'System.Collections.Generic.List[ScriptLine]' $this.Line = New-Object 'System.Collections.Generic.List[ScriptLine]'
} }
[int] SetScript([string]$Path) [int] SetScript([string]$Path)
{ {
Get-Content -Path $Path | %{ $this.Line.Add( [ScriptLine]@{text=$_;path=$path})} $lineNumber = 0
foreach($command in ( Get-Content -Path $Path ))
{
$this.Line.Add( [ScriptLine]::New($command, $path, $lineNumber) )
$lineNumber++
}
$this.LinesOfCode = $this.Line.Count $this.LinesOfCode = $this.Line.Count
return $this.LinesOfCode return $this.LinesOfCode
} }
[void] AddExecution([hashtable]$node) [void] AddExecution( [hashtable]$node )
{ {
# Line numbers start at 1 but the array starts at 0 # Line numbers start at 1 but the array starts at 0
$lineNumber = $node.Breakpoint.Line - 1 $lineNumber = $node.Breakpoint.Line - 1
$record = $this.Line[$lineNumber] $record = $this.Line[$lineNumber]
$record.LineNumber = $lineNumber
$record.AddExecution($node)
# Calclate the delta in time
if($this.lastNode) if($this.lastNode)
{ {
$duration = $node.ElapsedMilliseconds - $this.lastNode.ElapsedMilliseconds $duration = $node.Elapsed - $this.lastNode.Elapsed
} }
else else
{ {
$duration = $node.ElapsedMilliseconds $duration = $node.Elapsed
} }
# The delta is how long the last command ran
if($this.lastRecord) if($this.lastRecord)
{ {
$this.lastRecord.AddExecutionTime($duration) $this.lastRecord.AddExecutionTime($duration)
@@ -50,32 +58,35 @@ class MonitoredScript
[void] PostProcessing() [void] PostProcessing()
{ {
$this.lastNode = $null $this.lastNode = $null
$this.ExecutionTime = 0 $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
} }
$node.Milliseconds = 0 $node.Duration = [TimeSpan]::Zero
$node.Average = 0 $node.Average = 0
$this.lastNode = $node $this.lastNode = $node
} }
'^{$|^{#}' { '^{$|^{#}'
$node.Milliseconds = 0 {
$node.Duration = [TimeSpan]::Zero
$node.Average = 0 $node.Average = 0
$this.lastNode = $node $this.lastNode = $node
} }
default { default
{
$this.lastNode = $node $this.lastNode = $node
} }
} }
$this.ExecutionTime += $node.Milliseconds $this.ExecutionTime += $node.Duration
} }
} }
} }

View File

@@ -1,37 +1,68 @@
class ScriptLine class ScriptLine
{ {
[float] $Milliseconds = 0 [TimeSpan] $Duration = 0
[float] $HitCount = 0 [float] $HitCount = 0
[float] $Min = [float]::MaxValue [TimeSpan] $Min = [TimeSpan]::MaxValue
[float] $Max = [float]::MinValue [TimeSpan] $Max = [TimeSpan]::MinValue
[float] $Average = 0 [float] $Average = 0
[int] $LineNumber [int] $LineNumber
[string] $Path [string] $Path
[string] $Text [string] $Text
[System.Collections.ArrayList]$Executions
hidden [hashtable]$LastNode = @{}
ScriptLine()
[void]AddExecutionTime([float]$Milliseconds)
{ {
$this.Milliseconds += $Milliseconds $this.Executions = New-Object 'System.Collections.ArrayList'
}
ScriptLine($Command, $Path, $LineNumber)
{
$this.Executions = New-Object 'System.Collections.ArrayList'
$this.Text = $Command
$this.Path = $Path
$this.LineNumber = $LineNumber
}
[void]AddExecutionTime([timespan]$Duration)
{
$this.LastNode.Duration = $Duration
$this.Duration += $Duration
$this.HitCount += 1 $this.HitCount += 1
$this.Average = $this.Milliseconds / $this.HitCount $this.Average = $this.Duration.Milliseconds / $this.HitCount
if($Milliseconds -lt $this.Min) if($Duration -lt $this.Min)
{ {
$this.Min = $Milliseconds $this.Min = $Duration
} }
if($Milliseconds -gt $this.Max) if($Duration -gt $this.Max)
{ {
$this.Max = $Milliseconds $this.Max = $Duration
} }
} }
[void] AddExecution([hashtable]$node)
{
$this.Executions.Add($node)
$this.LastNode = $node
}
[void] Clear()
{
$this.Duration = [timespan]::Zero
$this.HitCount = 0
$this.Average = 0
$this.LastNode = $null
$this.Executions.Clear()
}
[string] ToString() [string] ToString()
{ {
$values = @( $values = @(
$this.Milliseconds $this.Duration.Milliseconds
$this.HitCount $this.HitCount
$this.Average $this.Average
$this.LineNumber $this.LineNumber

View File

@@ -13,7 +13,7 @@ class ScriptProfiler {
{ {
[ScriptProfiler]::Queue.Enqueue(@{ [ScriptProfiler]::Queue.Enqueue(@{
Breakpoint = $InputObject Breakpoint = $InputObject
ElapsedMilliseconds = [ScriptProfiler]::Timer.ElapsedMilliseconds Elapsed = [ScriptProfiler]::Timer.Elapsed
}) })
} }
} }

View File

@@ -8,7 +8,7 @@
RootModule = 'chronometer.psm1' RootModule = 'chronometer.psm1'
# Version number of this module. # Version number of this module.
ModuleVersion = '0.4.1' ModuleVersion = '0.5.0'
# ID used to uniquely identify this module # ID used to uniquely identify this module
GUID = 'f3719c3c-008a-4b25-b94d-fc9f587f62dd' GUID = 'f3719c3c-008a-4b25-b94d-fc9f587f62dd'