From 207b60fa002bb226f92190a0cadd3adc6303c090 Mon Sep 17 00:00:00 2001 From: KevinMarquette Date: Sun, 5 Feb 2017 12:13:59 -0800 Subject: [PATCH] Added linenumber support to measure just a section of a script or file. --- Chronometer/Classes/Chronometer.ps1 | 12 ++++++++++-- Chronometer/Public/Get-Chronometer.ps1 | 6 +++++- Tests/Unit.Tests.ps1 | 11 +++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/Chronometer/Classes/Chronometer.ps1 b/Chronometer/Classes/Chronometer.ps1 index ce93929..37eed40 100644 --- a/Chronometer/Classes/Chronometer.ps1 +++ b/Chronometer/Classes/Chronometer.ps1 @@ -3,18 +3,26 @@ class Chronometer [hashtable]$FileMap = @{} $Breakpoint = @() - [void]AddBreakpoint([string[]]$Path) + [void]AddBreakpoint([string[]]$Path, [int[]]$LineNumber) { foreach($file in (Resolve-Path $Path -ea 0)) { $script = [MonitoredScript]@{Path=$file.Path} $lines = $script.SetScript($file) + if($LineNumber -ne $null) + { + $bpLine = $LineNumber + } + else + { + $bpLine = (1..$lines) + } $this.fileMap[$file.Path] = $script $breakpointParam = @{ Script = $file - Line = (1..$lines) + Line = $bpLine Action = {[ScriptProfiler]::RecordExecution( $_) } } $this.breakPoint += Set-PSBreakpoint @breakpointParam diff --git a/Chronometer/Public/Get-Chronometer.ps1 b/Chronometer/Public/Get-Chronometer.ps1 index bc95fa2..c13c51f 100644 --- a/Chronometer/Public/Get-Chronometer.ps1 +++ b/Chronometer/Public/Get-Chronometer.ps1 @@ -15,6 +15,10 @@ function Get-Chronometer [string[]] $Path, + # Line numbers within the script file to measure + [int[]] + $LineNumber = $null, + # The script to start the scrupt or execute other commands [alias('Script','CommandScript')] [scriptblock] @@ -24,7 +28,7 @@ function Get-Chronometer $Chronometer = [Chronometer]::New() Write-Verbose "Setting breapoints" - $Chronometer.AddBreakpoint($Path) + $Chronometer.AddBreakpoint($Path,$LineNumber) if($Chronometer.breakPoint -ne $null) { diff --git a/Tests/Unit.Tests.ps1 b/Tests/Unit.Tests.ps1 index 3d2b475..71f832d 100644 --- a/Tests/Unit.Tests.ps1 +++ b/Tests/Unit.Tests.ps1 @@ -17,6 +17,17 @@ Describe "Basic unit tests" -Tags Build { $results = Get-Chronometer -Path $PSScriptRoot\..\ScratchFiles\example.ps1 -Script {. "$PSScriptRoot\..\ScratchFiles\example.ps1"} $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" {