benchmark/test.ps1
|
$size = 100000 $result = @() # simple version without timestamp or severity for fair comparison with Out-File and Add-Content function Write-Log { [CmdletBinding()] param( [Parameter(Position = 0, Mandatory = $true)] [ValidateNotNullOrEmpty()] [string]$Message ) if ((-not ($Script:Logger)) -or (-not ($Script:Logger.Writer))) { throw "Logger not initialized. Call Initialize-Logger first." } $Script:Logger.Writer.WriteLine("$Message") } # --- Test 1: FastNativeLogger --- $f = Measure-Command -Expression { Initialize-Logger -LogFile 'benchmark_native.log' foreach ($i in 1..$size) { Write-Log -Message $i } Close-Logger } $result += [pscustomobject]@{ Iterations = $size Test = "FastNativeLogger" TotalMilliseconds = [Math]::Round($f.TotalMilliseconds, 2) } # --- Test 2: Out-File --- $f = Measure-Command -Expression { foreach ($i in 1..$size) { $i | Out-File -FilePath 'benchmark_outfile.log' -Append -Encoding utf8 -Force } } $result += [pscustomobject]@{ Iterations = $size Test = "Out-File" TotalMilliseconds = [Math]::Round($f.TotalMilliseconds, 2) } # --- Test 3: Add-Content --- $f = Measure-Command -Expression { foreach ($i in 1..$size) { Add-Content -Path 'benchmark_addcontent.log' -Value $i -Encoding utf8 -Force } } $result += [pscustomobject]@{ Iterations = $size Test = "Add-Content" TotalMilliseconds = [Math]::Round($f.TotalMilliseconds, 2) } # --- Result --- $result = $result | Sort-Object TotalMilliseconds $result | Select-Object *, @{ Name = 'RelativeSpeed' Expression = { $relativeSpeed = $_.TotalMilliseconds / $result[0].TotalMilliseconds [Math]::Round($relativeSpeed, 2).ToString() + 'x' } } $null = Remove-Item -Path 'benchmark_native.log', 'benchmark_outfile.log', 'benchmark_addcontent.log' -Force |