Last active
August 22, 2021 15:39
-
-
Save VertigoRay/3bb0166d6a877839b420 to your computer and use it in GitHub Desktop.
Used Pester to do some speed tests, because the comments on my stackoverflow post (http://stackoverflow.com/a/16175967/615422) peaked my interest.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
Invoked the test with the following command, so that I could save the times take the average of each Context: | |
```posh | |
Invoke-Pester -OutputFile test.xml -OutputFormat NUnitXml | |
``` | |
Then I did some math on the results as shown (yes, I use pipelines when not scripting): | |
```posh | |
[xml]$xml = Get-Content .\test.xml | |
$times = @( | |
($xml.'test-results'.'test-suite'.results.'test-suite'.results.'test-case' | ?{ [int]$_.description -le 1000 } | %{ [decimal]$_.time }), | |
($xml.'test-results'.'test-suite'.results.'test-suite'.results.'test-case' | ?{ ([int]$_.description -ge 1001) -and ([int]$_.description -le 2000) } | %{ [decimal]$_.time }), | |
($xml.'test-results'.'test-suite'.results.'test-suite'.results.'test-case' | ?{ [int]$_.description -ge 2001 } | %{ [decimal]$_.time }) | |
) | |
# Get Averages | |
$avgs = $times | %{ $_ | Measure-Object -Average } | |
Write-Host "Averages: $($avgs | Out-String)" | |
# Get Medians | |
$meds = $times | %{ | |
$data = $_ | sort | |
if ($data.count%2) { | |
$MedianValue = $data[[math]::Floor($data.count/2)] | |
} else { | |
$MedianValue = ($data[$data.Count/2],$data[$data.count/2-1] |measure -Average).average | |
} | |
$MedianValue | |
} | |
Write-Host "Medians: $($meds | Out-String)" | |
#Get Modes | |
$mods = $times | %{ '---'; ($_ | group | sort -Descending Count)[0..5] } | |
Write-Host "Modes: $($mods | Out-String)" | |
``` | |
Results are in the comments. | |
#> | |
$computerSystem = Get-CimInstance CIM_ComputerSystem | |
$computerOS = Get-CimInstance CIM_OperatingSystem | |
$computerCPU = Get-CimInstance CIM_Processor | |
@" | |
Manufacturer: {0} | |
Model: {1} | |
CPU: {2} | |
RAM: {3:N2} GB | |
Operating System: {4} | |
Service Pack: {5} | |
PS Version Table: {6} | |
"@ -f ( | |
$computerSystem.Manufacturer, | |
$computerSystem.Model, | |
$computerCPU.Name, | |
($computerSystem.TotalPhysicalMemory/1GB), | |
$computerOS.caption, | |
$computerOS.ServicePackMajorVersion, | |
($PSVersionTable | Out-String) | |
) | |
Describe 'Looping Speed Test' { | |
BeforeEach { | |
$hash = @{ | |
a = 1 | |
b = 2 | |
c = 3 | |
} | |
} | |
Context 'Pipeline Test' { | |
foreach ($i in 1..500) { | |
It $i { | |
{ | |
$hash.Keys | % { "key = $_ , value = " + $hash.Item($_) } | |
} | Should Not Throw | |
} | |
} | |
} | |
Context 'ForEach GetEnumerator Test' { | |
foreach ($i in 1001..1500) { | |
It $i { | |
{ | |
foreach ($h in $hash.GetEnumerator()) { | |
"$($h.Name): $($h.Value)" | |
} | |
} | Should Not Throw | |
} | |
} | |
} | |
Context 'ForEach Keys Test' { | |
foreach ($i in 2001..2500) { | |
It $i { | |
{ | |
foreach ($h in $hash.Keys) { | |
"${h}: $($hash.Item($h))" | |
} | |
} | Should Not Throw | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's my computer information:
Here's my Averages, Medians, and Modes (calculated as shown above, in the comment block):
Note: The first calculation is the Pipeline Test, then the ForEach GetEnumerator Test, then the ForEach Keys Test.
In case you want my values, here's the result of
$times | ConvertTo-Json -Compress
: