Skip to content
TKTail

Purpose-built scripts and the TKTail workflow - the source

powershell/idiomatic.ps1

SHA-256 (LF line endings, as in the source ZIP): bbb45a85d87ec7d61eb82736ae950d6f1322e6a957484beac0370ffafc45e90e · MIT license (the benchmark suite only)

<#
.SYNOPSIS
  competitive-bench-v1 - the PowerShell "idiomatic pipeline" convenience baseline: the cmdlets an administrator types
  (Select-String, Get-Content | ConvertFrom-Csv, Where-Object, Group-Object, Sort-Object). Measured separately from the
  optimized streaming baseline (bench.ps1) and never presented as PowerShell's speed - it shows what the quick
  interactive approach costs. Runs on Windows PowerShell 5.1 and PowerShell 7.

.DESCRIPTION
  idiomatic.ps1 -Scenario <id> -InputPath <log>   (prints the same JSON line as bench.ps1; one repetition)
#>
param(
    [Parameter(Mandatory = $true)] [string]$Scenario,
    [Parameter(Mandatory = $true)] [string]$InputPath
)
$ErrorActionPreference = 'Stop'

function Get-W3C([string]$path) {
    $fields = ((Get-Content -Path $path -TotalCount 10 | Where-Object { $_ -like '#Fields: *' } | Select-Object -First 1) -replace '^#Fields: ', '') -split ' '
    Get-Content -Path $path -ReadCount 1000 | ForEach-Object { $_ } | Where-Object { $_ -notlike '#*' } | ConvertFrom-Csv -Delimiter ' ' -Header $fields
}

function Get-Top($records, [string]$key) {
    $top = New-Object System.Collections.Generic.List[object]
    $records | Group-Object -Property $key -NoElement |
        Sort-Object @{ Expression = 'Count'; Descending = $true }, @{ Expression = 'Name'; Descending = $false } |
        Select-Object -First 10 | ForEach-Object { $top.Add(@($_.Name, [long]$_.Count)) }
    return $top
}

$sw = [System.Diagnostics.Stopwatch]::StartNew()
switch ($Scenario) {
    'literal-error' {
        $result = @{ count = (Select-String -Path $InputPath -Pattern 'ERROR' -SimpleMatch -CaseSensitive | Measure-Object).Count }
    }
    'elapsed-ge-1000' {
        $result = @{ count = (Select-String -Path $InputPath -Pattern 'elapsed: (\d+(?:\.\d+)?)ms' -CaseSensitive |
                Where-Object { [double]$_.Matches[0].Groups[1].Value -ge 1000 } | Measure-Object).Count }
    }
    'count-5xx' {
        $result = @{ count = (Get-W3C $InputPath | Where-Object { [int]$_.'sc-status' -ge 500 } | Measure-Object).Count }
    }
    'top-ip-5xx' {
        $errors = @(Get-W3C $InputPath | Where-Object { [int]$_.'sc-status' -ge 500 })
        $result = @{ count = $errors.Count; top = (Get-Top $errors 'c-ip') }
    }
    'per-minute-5xx' {
        $buckets = New-Object System.Collections.Generic.List[object]
        Get-W3C $InputPath | Where-Object { [int]$_.'sc-status' -ge 500 } |
            Group-Object -Property { $_.date + 'T' + $_.time.Substring(0, 5) } -NoElement | Sort-Object -Property Name -CaseSensitive |
            ForEach-Object { $buckets.Add(@($_.Name, [long]$_.Count)) }
        $result = @{ buckets = $buckets }
    }
    default { throw "the idiomatic baseline has no scenario $Scenario" }
}
$sw.Stop()
$self = [System.Diagnostics.Process]::GetCurrentProcess()
$report = [ordered]@{
    implementation      = 'powershell-idiomatic'
    runtime             = 'PowerShell ' + $PSVersionTable.PSVersion.ToString() + ' (' + $PSVersionTable.PSEdition + ')'
    scenario            = $Scenario
    result              = $result
    runsMs              = @([math]::Round($sw.Elapsed.TotalMilliseconds, 3))
    cpuMs               = [math]::Round($self.TotalProcessorTime.TotalMilliseconds, 1)
    peakWorkingSetBytes = $self.PeakWorkingSet64
}
[Console]::Out.WriteLine(($report | ConvertTo-Json -Compress -Depth 6))