1
+ # Usage:
2
+ #
3
+ # For single measurement:
4
+ #
5
+ # .\Measure-CommandPerformance.ps1 -Commands @("dotnet test --no-build")
6
+ #
7
+ # For standard build / test measurements:
8
+ #
9
+ # $commandSetsBase = @(
10
+ # @{ Command = "dotnet build"; Preparation = "dotnet clean" },
11
+ # @{ Command = "dotnet build" },
12
+ # @{ Command = "dotnet test --no-build" }
13
+ # )
14
+ # .\Measure-CommandPerformance.ps1 -CommandSets $commandSetsBase
15
+ #
16
+ # For Cucumber Messages measurements
17
+ #
18
+ # $commandSetsCM = @(
19
+ # @{ Command = "dotnet build"; Preparation = "dotnet clean" },
20
+ # @{ Command = "dotnet build" },
21
+ # @{ Command = "dotnet test --no-build"; Preparation = '$env:REQNROLL_FORMATTERS_DISABLED = "true"' },
22
+ # @{ Command = "dotnet test --no-build"; Preparation = '$env:REQNROLL_FORMATTERS_DISABLED = "false"' }
23
+ # )
24
+ # .\Measure-CommandPerformance.ps1 -CommandSets $commandSetsCM
25
+ param (
26
+ [Parameter (Mandatory = $false )]
27
+ [string []]$Commands ,
28
+ [Parameter (Mandatory = $false )]
29
+ [hashtable []]$CommandSets
30
+ )
31
+
32
+ function Measure-Command {
33
+ param (
34
+ [string ]$Command ,
35
+ [string ]$PreparationCommand = $null
36
+ )
37
+
38
+ Write-Host " Warming up: $Command "
39
+ # Warmup run (not measured)
40
+ if ($PreparationCommand ) {
41
+ Invoke-Expression $PreparationCommand | Out-Null
42
+ }
43
+ Invoke-Expression $Command | Out-Null
44
+
45
+ $measurements = @ ()
46
+
47
+ for ($i = 1 ; $i -le 3 ; $i ++ ) {
48
+ if ($PreparationCommand ) {
49
+ Write-Host " Preparation ($i /3): $PreparationCommand "
50
+ Invoke-Expression $PreparationCommand | Out-Null
51
+ }
52
+ Write-Host " Measuring ($i /3): $Command "
53
+ $sw = [System.Diagnostics.Stopwatch ]::StartNew()
54
+ Invoke-Expression $Command | Out-Null
55
+ $sw.Stop ()
56
+ $elapsed = $sw.Elapsed.TotalSeconds
57
+ $measurements += $elapsed
58
+ Write-Host (" Run ${i} : {0:N3} seconds" -f $elapsed )
59
+ }
60
+
61
+ $avg = ($measurements | Measure-Object - Average).Average
62
+ $min = ($measurements | Measure-Object - Minimum).Minimum
63
+ $max = ($measurements | Measure-Object - Maximum).Maximum
64
+
65
+ Write-Host (" Summary for '$Command ':" )
66
+ Write-Host (" Average: {0:N3} s" -f $avg )
67
+ Write-Host (" Min: {0:N3} s" -f $min )
68
+ Write-Host (" Max: {0:N3} s" -f $max )
69
+ Write-Host " "
70
+ }
71
+
72
+ # Backwards compatibility: If CommandSets is not given, fall back to Commands array
73
+ if ($CommandSets -and $CommandSets.Count -gt 0 ) {
74
+ foreach ($cmdset in $CommandSets ) {
75
+ $cmd = $cmdset.Command
76
+ $prep = $null
77
+ if ($cmdset.ContainsKey (" Preparation" )) {
78
+ $prep = $cmdset.Preparation
79
+ }
80
+ Measure-Command - Command $cmd - PreparationCommand $prep
81
+ }
82
+ } else {
83
+ foreach ($cmd in $Commands ) {
84
+ Measure-Command - Command $cmd
85
+ }
86
+ }
0 commit comments