Last active
May 6, 2023 10:26
-
-
Save tinuwalther/83799c22f948fb7b473bd81f1a2b648a to your computer and use it in GitHub Desktop.
PerformanceTests
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
<# | |
Tests for https://hcritter.devdojo.com/powershell-performance-test-file-reading | |
#> | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$false)] | |
[Switch]$Measure | |
) | |
# Create a file with 50'000 lines | |
$LargeFile = 'D:\dummy.txt' | |
if(Test-Path $largeFile){ | |
Write-Host "$LargeFile already exists" -ForegroundColor Green | |
}else{ | |
Write-Host "$LargeFile not exists, try to create it" -ForegroundColor Cyan | |
1..50000 | ForEach-Object { Add-Content -Path $LargeFile -Value "This is a very large Testfile $($_)" } | |
} | |
"$LargeFile {0:N2}MB" -f ((Get-Item $LargeFile | Select-Object -exp Length)/1mb) | Write-Host | |
$SearchPattern = '45123' | |
if($Measure){ | |
$StartTime = Get-Date | |
Write-Host "Read $LargeFile and return the time to saerch for a the pattern $SearchPattern" -ForegroundColor Green | |
@( | |
# Get-Content | |
Measure-Command { | |
$File = Get-Content $LargeFile | |
foreach ($Line in $File){ | |
$Line | Select-String -Pattern $SearchPattern | |
#break | |
} | |
} | Select-Object @{N='Method';E={'Get-Content'}}, @{N='Seconds';E={$_.Seconds}}, @{N='Milliseconds';E={$_.Milliseconds}}, @{N='TotalMilliseconds';E={$_.TotalMilliseconds}} | |
# System.IO.File::ReadAllLines() | |
Measure-Command { | |
$File = [system.io.file]::ReadAllLines($LargeFile) | |
foreach ($Line in $File){ | |
$Line | Select-String -Pattern $SearchPattern | |
#break | |
} | |
} | Select-Object @{N='Method';E={'System.IO.File::ReadAllLines()'}}, @{N='Seconds';E={$_.Seconds}}, @{N='Milliseconds';E={$_.Milliseconds}}, @{N='TotalMilliseconds';E={$_.TotalMilliseconds}} | |
# [System.IO.StreamReader]::ReadLine() - Classic Way | |
Measure-Command { | |
$sread = [System.IO.StreamReader]::new($largefile) | |
while ($sread.ReadLine()) { | |
$_ | Select-String -Pattern $SearchPattern | |
#break | |
} | |
} | Select-Object @{N='Method';E={'[System.IO.StreamReader]::ReadLine() - Classic Way'}}, @{N='Seconds';E={$_.Seconds}}, @{N='Milliseconds';E={$_.Milliseconds}}, @{N='TotalMilliseconds';E={$_.TotalMilliseconds}} | |
# [System.IO.StreamReader]::ReadLine() - Peek | |
Measure-Command { | |
$sread = [System.IO.StreamReader]::new($largefile) | |
while ($sread.Peek() -gt -1) { | |
$sread.ReadLine() | Select-String -Pattern $SearchPattern | |
#break | |
} | |
} | Select-Object @{N='Method';E={'[System.IO.StreamReader]::ReadLine() - Peek'}}, @{N='Seconds';E={$_.Seconds}}, @{N='Milliseconds';E={$_.Milliseconds}}, @{N='TotalMilliseconds';E={$_.TotalMilliseconds}} | |
# [System.IO.StreamReader]::ReadLine() - End Of Stream | |
Measure-Command { | |
$sread = [System.IO.StreamReader]::new($largefile) | |
while ($sread.EndOfStream -eq $false) { | |
$sread.ReadLine() | Select-String -Pattern $SearchPattern | |
#break | |
} | |
} | Select-Object @{N='Method';E={'[System.IO.StreamReader]::ReadLine() - End Of Stream'}}, @{N='Seconds';E={$_.Seconds}}, @{N='Milliseconds';E={$_.Milliseconds}}, @{N='TotalMilliseconds';E={$_.TotalMilliseconds}} | |
# Switch | |
Measure-Command { | |
switch -File ($LargeFile){ | |
Default { | |
$_ | Select-String -Pattern $SearchPattern | |
#break | |
} | |
} | |
} | Select-Object @{N='Method';E={'Switch'}}, @{N='Seconds';E={$_.Seconds}}, @{N='Milliseconds';E={$_.Milliseconds}}, @{N='TotalMilliseconds';E={$_.TotalMilliseconds}} | |
) | Sort-Object TotalMilliseconds | |
$TimeSpan = New-TimeSpan -Start $StartTime -End (Get-Date) | |
$Formatted = $TimeSpan | ForEach-Object { | |
'{1:0}h {2:0}m {3:0}s {4:000}ms' -f $_.Days, $_.Hours, $_.Minutes, $_.Seconds, $_.Milliseconds | |
} | |
Write-Host $('Finished in:', $Formatted -Join ' ') -ForegroundColor Green | |
}else{ | |
Write-Host "Read the $LargeFile with the Get-Content and return the matching line for $SearchPattern" -ForegroundColor Green | |
$StartTime = Get-Date | |
$File = Get-Content $LargeFile | |
$MatchingLine = foreach ($Line in $File){ | |
$Line | Select-String -Pattern $SearchPattern | |
} | |
$TimeSpan = New-TimeSpan -Start $StartTime -End (Get-Date) | |
$Formatted = $TimeSpan | ForEach-Object { | |
'{1:0}h {2:0}m {3:0}s {4:000}ms' -f $_.Days, $_.Hours, $_.Minutes, $_.Seconds, $_.Milliseconds | |
} | |
Write-Host $MatchingLine | |
Write-Host $('Finished in:', $Formatted -Join ' ') -ForegroundColor Green | |
Write-Host "Read the $LargeFile with the Switch-Statement and return the matching line for $SearchPattern" -ForegroundColor Green | |
$StartTime = Get-Date | |
$MatchingLine = switch -File ($LargeFile){ | |
Default { | |
$_ | Select-String -Pattern $SearchPattern | |
} | |
} | |
$TimeSpan = New-TimeSpan -Start $StartTime -End (Get-Date) | |
$Formatted = $TimeSpan | ForEach-Object { | |
'{1:0}h {2:0}m {3:0}s {4:000}ms' -f $_.Days, $_.Hours, $_.Minutes, $_.Seconds, $_.Milliseconds | |
} | |
Write-Host $MatchingLine | |
Write-Host $('Finished in:', $Formatted -Join ' ') -ForegroundColor Green | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment