Added linenumber support to measure just a section of a script or file.

This commit is contained in:
KevinMarquette
2017-02-05 12:13:59 -08:00
parent bbb7cd508d
commit 207b60fa00
3 changed files with 26 additions and 3 deletions

View File

@@ -3,18 +3,26 @@ class Chronometer
[hashtable]$FileMap = @{} [hashtable]$FileMap = @{}
$Breakpoint = @() $Breakpoint = @()
[void]AddBreakpoint([string[]]$Path) [void]AddBreakpoint([string[]]$Path, [int[]]$LineNumber)
{ {
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($LineNumber -ne $null)
{
$bpLine = $LineNumber
}
else
{
$bpLine = (1..$lines)
}
$this.fileMap[$file.Path] = $script $this.fileMap[$file.Path] = $script
$breakpointParam = @{ $breakpointParam = @{
Script = $file Script = $file
Line = (1..$lines) Line = $bpLine
Action = {[ScriptProfiler]::RecordExecution( $_) } Action = {[ScriptProfiler]::RecordExecution( $_) }
} }
$this.breakPoint += Set-PSBreakpoint @breakpointParam $this.breakPoint += Set-PSBreakpoint @breakpointParam

View File

@@ -15,6 +15,10 @@ function Get-Chronometer
[string[]] [string[]]
$Path, $Path,
# Line numbers within the script file to measure
[int[]]
$LineNumber = $null,
# The script to start the scrupt or execute other commands # The script to start the scrupt or execute other commands
[alias('Script','CommandScript')] [alias('Script','CommandScript')]
[scriptblock] [scriptblock]
@@ -24,7 +28,7 @@ function Get-Chronometer
$Chronometer = [Chronometer]::New() $Chronometer = [Chronometer]::New()
Write-Verbose "Setting breapoints" Write-Verbose "Setting breapoints"
$Chronometer.AddBreakpoint($Path) $Chronometer.AddBreakpoint($Path,$LineNumber)
if($Chronometer.breakPoint -ne $null) if($Chronometer.breakPoint -ne $null)
{ {

View File

@@ -17,6 +17,17 @@ Describe "Basic unit tests" -Tags Build {
$results = Get-Chronometer -Path $PSScriptRoot\..\ScratchFiles\example.ps1 -Script {. "$PSScriptRoot\..\ScratchFiles\example.ps1"} $results = Get-Chronometer -Path $PSScriptRoot\..\ScratchFiles\example.ps1 -Script {. "$PSScriptRoot\..\ScratchFiles\example.ps1"}
$results | Should Not BeNullOrEmpty $results | Should Not BeNullOrEmpty
} }
it "Executes a script with linenumbers and gives results" {
# Get-Chronometer -Path ScratchFiles\example.ps1 -Script {"Test"}
$params = @{
Path = "$PSScriptRoot\..\ScratchFiles\example.ps1"
Script = {. "$PSScriptRoot\..\ScratchFiles\example.ps1"}
LineNumber = 2,3,5,6
}
$results = Get-Chronometer @params
$results | Should Not BeNullOrEmpty
}
} }
Context "Function: Format-Chronometer" { Context "Function: Format-Chronometer" {