Skip to content

Commit ff16c67

Browse files
Upgrading performance measuring
1 parent 31d2cf1 commit ff16c67

File tree

5 files changed

+162
-125
lines changed

5 files changed

+162
-125
lines changed

PSModuleDevelopment/PSModuleDevelopment.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
'Get-PSMDTemplate',
7979
'Import-PSMDModuleDebug',
8080
'Invoke-PSMDTemplate',
81-
'Measure-PSMDCommandEx',
81+
'Measure-PSMDCommand',
8282
'Measure-PSMDLinesOfCode',
8383
'New-PSMDDotNetProject',
8484
'New-PSMDHeader',

PSModuleDevelopment/changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Changelog
22
##
33
- New: Export-PSMDString - Parses strings from modules using the PSFramework localization feature.
4+
- Upd: Measure-PSMDCommand - Renamed from Measure-PSMDCommandEx, performance upgrades, adding option for comparing multiple test sets.
45
- Upd: Refactored and updated the ModuleDebug component
56
- Upd: Renamed Get-PSMDHelpEx to Get-PSMDHelp
67
- Upd: Template PSFProject - Adding `-IncludAZ` switch parameter to `vsts-packageFunction.ps1`, making the template include the AZ module as managed dependency.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
function Measure-PSMDCommand
2+
{
3+
4+
<#
5+
.SYNOPSIS
6+
Measures command performance with consecutive tests.
7+
8+
.DESCRIPTION
9+
This function measures the performance of a scriptblock many consective times.
10+
11+
Warning: Running a command repeatedly may not yield reliable information, since repeated executions may benefit from caching or other performance enhancing features, depending on the script content.
12+
This is best suited for measuring the performance of tasks that will later be run repeatedly as well.
13+
It also is useful for mitigating local performance fluctuations when comparing performances.
14+
15+
PARAMETER ScriptBlock
16+
The scriptblock whose performance is to be measure.
17+
18+
PARAMETER Iterations
19+
How many times should this performance test be repeated.
20+
21+
.PARAMETER TestSet
22+
Accepts a hashtable, mapping a name to a specific scriptblock to measure.
23+
This will generate a result grading the performance of the various sets offered.
24+
25+
.EXAMPLE
26+
PS C:\> Measure-PSMDCommand -ScriptBlock { dir \\Server\share } -Iterations 100
27+
28+
This tries to use Get-ChildItem on a remote directory 100 consecutive times, then measures performance and reports common performance indicators (Average duration, Maximum, Minimum, Total)
29+
#>
30+
[CmdletBinding()]
31+
param (
32+
[Parameter(Mandatory = $true, ParameterSetName = 'Script')]
33+
[scriptblock]
34+
$ScriptBlock,
35+
36+
[int]
37+
$Iterations = 1,
38+
39+
[Parameter(Mandatory = $true, ParameterSetName = 'Set')]
40+
[hashtable]
41+
$TestSet
42+
)
43+
44+
Process
45+
{
46+
#region Running an individual testrun
47+
if ($ScriptBlock)
48+
{
49+
[System.Collections.ArrayList]$results = @()
50+
$count = 0
51+
while ($count -lt $Iterations)
52+
{
53+
$null = $results.Add((Measure-Command -Expression $ScriptBlock))
54+
$count++
55+
}
56+
$measured = $results | Measure-Object -Maximum -Minimum -Average -Sum -Property Ticks
57+
[pscustomobject]@{
58+
PSTypeName = 'PSModuleDevelopment.Performance.TestResult'
59+
Results = $results.ToArray()
60+
Max = (New-Object System.TimeSpan($measured.Maximum))
61+
Sum = (New-Object System.TimeSpan($measured.Sum))
62+
Min = (New-Object System.TimeSpan($measured.Minimum))
63+
Average = (New-Object System.TimeSpan($measured.Average))
64+
}
65+
}
66+
#endregion Running an individual testrun
67+
68+
#region Performing a testset
69+
if ($TestSet)
70+
{
71+
$setResult = @{ }
72+
foreach ($testName in $TestSet.Keys)
73+
{
74+
$setResult[$testName] = Measure-PSMDCommand -ScriptBlock $TestSet[$testName] -Iterations $Iterations
75+
}
76+
$fastestResult = $setResult.Values | Sort-Object Average | Select-Object -First 1
77+
78+
$finalResult = foreach ($setName in $setResult.Keys)
79+
{
80+
$resultItem = $setResult[$setName]
81+
[pscustomobject]@{
82+
PSTypeName = 'PSModuleDevelopment.Performance.TestSetItem'
83+
Name = $setName
84+
Efficiency = $resultItem.Average.Ticks / $fastestResult.Average.Ticks
85+
Average = $resultItem.Average
86+
Result = $resultItem
87+
88+
}
89+
}
90+
$finalResult | Sort-Object Efficiency
91+
}
92+
#endregion Performing a testset
93+
}
94+
}

PSModuleDevelopment/functions/performance/Measure-PSMDCommandEx.ps1

Lines changed: 0 additions & 124 deletions
This file was deleted.

PSModuleDevelopment/xml/PSModuleDevelopment.Format.ps1xml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,72 @@
11
<?xml version="1.0" encoding="utf-16"?>
22
<Configuration>
33
<ViewDefinitions>
4+
<!-- PSModuleDevelopment.Performance.TestResult -->
5+
<View>
6+
<Name>PSModuleDevelopment.Performance.TestResult</Name>
7+
<ViewSelectedBy>
8+
<TypeName>PSModuleDevelopment.Performance.TestResult</TypeName>
9+
</ViewSelectedBy>
10+
<TableControl>
11+
<AutoSize/>
12+
<TableHeaders>
13+
<TableColumnHeader/>
14+
<TableColumnHeader/>
15+
<TableColumnHeader/>
16+
<TableColumnHeader/>
17+
</TableHeaders>
18+
<TableRowEntries>
19+
<TableRowEntry>
20+
<TableColumnItems>
21+
<TableColumnItem>
22+
<PropertyName>Average</PropertyName>
23+
</TableColumnItem>
24+
<TableColumnItem>
25+
<PropertyName>Min</PropertyName>
26+
</TableColumnItem>
27+
<TableColumnItem>
28+
<PropertyName>Max</PropertyName>
29+
</TableColumnItem>
30+
<TableColumnItem>
31+
<PropertyName>Sum</PropertyName>
32+
</TableColumnItem>
33+
</TableColumnItems>
34+
</TableRowEntry>
35+
</TableRowEntries>
36+
</TableControl>
37+
</View>
38+
39+
<!-- PSModuleDevelopment.Performance.TestSetItem -->
40+
<View>
41+
<Name>PSModuleDevelopment.Performance.TestSetItem</Name>
42+
<ViewSelectedBy>
43+
<TypeName>PSModuleDevelopment.Performance.TestSetItem</TypeName>
44+
</ViewSelectedBy>
45+
<TableControl>
46+
<AutoSize/>
47+
<TableHeaders>
48+
<TableColumnHeader/>
49+
<TableColumnHeader/>
50+
<TableColumnHeader/>
51+
</TableHeaders>
52+
<TableRowEntries>
53+
<TableRowEntry>
54+
<TableColumnItems>
55+
<TableColumnItem>
56+
<PropertyName>Name</PropertyName>
57+
</TableColumnItem>
58+
<TableColumnItem>
59+
<PropertyName>Efficiency</PropertyName>
60+
</TableColumnItem>
61+
<TableColumnItem>
62+
<PropertyName>Average</PropertyName>
63+
</TableColumnItem>
64+
</TableColumnItems>
65+
</TableRowEntry>
66+
</TableRowEntries>
67+
</TableControl>
68+
</View>
69+
470
<!-- PSModuleDevelopment.PsmdAssembly.Constructor -->
571
<View>
672
<Name>PSModuleDevelopment.PsmdAssembly.Constructor</Name>

0 commit comments

Comments
 (0)