Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -HumanReadable switch for ms duration formatting #16

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Tests/AstVisitor.class.Tests.Ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Import-Module "$PSScriptRoot\..\src\MeasureScript.psd1" -Force
Import-Module "$PSScriptRoot\..\src\PSProfiler.psd1" -Force

InModuleScope -ModuleName MeasureScript -ScriptBlock {
InModuleScope -ModuleName PSProfiler -ScriptBlock {

Describe '[AstVisitor]-[Constructors]'{

Expand Down
4 changes: 2 additions & 2 deletions Tests/Profiler.class.Tests.Ps1
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@


Import-Module "$PSScriptRoot\..\src\MeasureScript.psd1" -Force
Import-Module "$PSScriptRoot\..\src\PSProfiler.psd1" -Force

InModuleScope "MeasureScript"{
InModuleScope "PSProfiler"{

Describe '[Profiler]-[Constructors]'{

Expand Down
21 changes: 19 additions & 2 deletions Tests/TimeLine.class.Tests.Ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Import-Module "$PSScriptRoot\..\src\MeasureScript.psd1" -Force
Import-Module "$PSScriptRoot\..\src\PSProfiler.psd1" -Force

InModuleScope -ModuleName MeasureScript -ScriptBlock {
InModuleScope -ModuleName PSProfiler -ScriptBlock {


Describe '[TimeLine]-[Constructors]'{
Expand Down Expand Up @@ -82,6 +82,23 @@ $Instance = [TimeLine]::New()

} #End It Block

It '[TimeLine] --> GetTotalFormatted() : [string] - should return type [string]' {
$Instance = [TimeLine]::New()
($Instance.GetTotalFormatted($true)).GetType().Name | should be String
}

It '[TimeLine] --> GetTotalFormatted($true) : [string] - should return ms' {
$Instance = [TimeLine]::New()
$Instance.Add([TimeSpan]::FromMilliseconds(500))
$Instance.GetTotalFormatted($true) | should be "500ms"
}

It '[TimeLine] --> GetTotalFormatted($false) : [string] - should return timespan format' {
$Instance = [TimeLine]::New()
$Instance.Add([TimeSpan]::FromMilliseconds(500))
$Instance.GetTotalFormatted($false) | should be "00:00.5000000"
}

#Public Method
It '[TimeLine] --> GetAverage() : [TimeSpan] - should Not Throw' {

Expand Down
8 changes: 8 additions & 0 deletions src/Classes/TimeLine.class.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ class TimeLine
return $this.Total
}

[String]GetTotalFormatted([boolean]$HumanReadable)
{
if ($HumanReadable) {
return '{0}ms' -f $([math]::Round($this.Total.TotalMilliseconds))
}
return '{0:mm\:ss\.fffffff}' -f $this.Total
}

[TimeSpan]GetAverage()
{
if($count = $this.GetCount() -eq 0){
Expand Down
4 changes: 2 additions & 2 deletions src/PSProfiler.format.ps1xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ FormatData for MeasureScript
<TableColumnItem>
<ScriptBlock>
if ($_.Top -and $PSStyle -ne $null) {
$PSStyle.Background.Red + ('{0:mm\:ss\.fffffff}' -f $_.ExecutionTime) + $PSStyle.Reset
$PSStyle.Background.Red + $_.Formatted + $PSStyle.Reset
}
else {
'{0:mm\:ss\.fffffff}' -f $_.ExecutionTime
return $_.Formatted
}
</ScriptBlock>
</TableColumnItem>
Expand Down
6 changes: 4 additions & 2 deletions src/Public/Measure-Script.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Function Measure-Script {
0 5 00:00.0000000

.EXAMPLE
Measure-Scipt -Path c:\PS\GenerateUsername.ps1 -Arguments @{GivenName = "Joe";Surname = "Smith"}
Measure-Script -Path c:\PS\GenerateUsername.ps1 -Arguments @{GivenName = "Joe";Surname = "Smith"}

This will execute and measure the c:\PS\GenerateUsername.ps1 script with the -GivenName and -Surname parameters.

Expand All @@ -72,7 +72,8 @@ Function Measure-Script {
[Parameter(Mandatory=$false,ParameterSetName="__AllParametersets")]
[string]$Name,
[Parameter(Mandatory=$false,ParameterSetName="__AllParametersets")]
[int]$Top = 5
[int]$Top = 5,
[switch]$HumanReadable
)

if($PSCmdlet.ParameterSetName -eq "Path") {
Expand Down Expand Up @@ -135,6 +136,7 @@ Function Measure-Script {
LineNo = $i + 1
ExecutionTime = $executionTimes[$i]
TimeLine = $profiler.TimeLines[$i]
Formatted = $profiler.TimeLines[$i].GetTotalFormatted($HumanReadable)
Line = $lines[$i]
SourceScript = $Source
Top = $executionTimes[$i].Ticks -ge $topLimit
Expand Down