Files
Chronometer/Chronometer/Public/Get-Chronometer.ps1
Kevin Marquette 3fdc8a02d9 Create a merged psm1 file when published (#16) !deploy
* Added auto discovery fo files to monitor.
* Fixed scriptanalyzer rule
* Adjusted the parameter attributes
* testing feature specifications
* Added module building
* Exceptions and spacing
2017-05-03 00:08:41 -07:00

79 lines
2.0 KiB
PowerShell

function Get-Chronometer
{
<#
.Description
Loads a script and then tracks the line by line execution times
.Example
Get-Chronometer -Path .\example.ps1 -Script {
.\example.ps1
}
#>
[CmdletBinding()]
param(
# Script file to measure execution times on
[Parameter(
ValueFromPipeline = $true
)]
[Object[]]
$Path,
# Line numbers within the script file to measure
[int[]]
$LineNumber = $null,
# The script to start the scrupt or execute other commands
[Parameter(Position = 0)]
[alias('Script', 'CommandScript')]
[scriptblock]
$ScriptBlock
)
process
{
try
{
if ( $null -eq $Path )
{
$Path = Get-ChildItem -Recurse -Include *.psm1, *.ps1 -File
}
if ( $Path.FullName )
{
$Path = $Path.FullName
}
$Chronometer = [Chronometer]::New()
Write-Verbose "Setting breapoints"
$Chronometer.AddBreakpoint($Path, $LineNumber)
if ( $null -ne $Chronometer.breakPoint -and $null -ne $ScriptBlock )
{
Write-Verbose "Executing Script"
[ScriptProfiler]::Start()
[void] $ScriptBlock.Invoke($Path)
Write-Verbose "Clearing Breapoints"
$Chronometer.ClearBreakpoint()
Write-Verbose "Processing data"
foreach ( $node in [ScriptProfiler]::Queue.GetEnumerator() )
{
$Chronometer.AddExecution($node)
}
Write-Output $Chronometer.GetResults()
}
else
{
Write-Warning "Parsing files did not result in any breakpoints"
}
}
catch
{
$PSCmdlet.ThrowTerminatingError($PSItem)
}
}
}