|
| 1 | +#Requires -Version 7.0 |
| 2 | +[CmdletBinding()] |
| 3 | +[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '', Justification = 'Performance script intentionally uses Write-Host')] |
| 4 | +[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', 'ResultsPath', Justification = 'Used via splatting')] |
| 5 | +param( |
| 6 | + [Parameter(Mandatory)] |
| 7 | + [string] $Iteration, |
| 8 | + |
| 9 | + [string[]] $Subset = @('Roboto', 'Open Sans', 'Inter'), |
| 10 | + |
| 11 | + [switch] $IncludeAll, |
| 12 | + |
| 13 | + [string] $ResultsPath = (Join-Path $PSScriptRoot 'perf-results.jsonl') |
| 14 | +) |
| 15 | + |
| 16 | +function Invoke-Uninstall { |
| 17 | + param([string[]] $Names) |
| 18 | + foreach ($n in $Names) { |
| 19 | + $installed = Get-Font -Scope CurrentUser -ErrorAction SilentlyContinue | Where-Object { $_.Name -like "$n*" } |
| 20 | + if ($installed) { |
| 21 | + Uninstall-Font -Name $installed.Name -Scope CurrentUser -ErrorAction SilentlyContinue |
| 22 | + } |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +function Measure-Scenario { |
| 27 | + param( |
| 28 | + [string] $Name, |
| 29 | + [scriptblock] $Setup, |
| 30 | + [scriptblock] $Action |
| 31 | + ) |
| 32 | + Write-Host "[$Iteration] Setup : $Name" |
| 33 | + & $Setup | Out-Null |
| 34 | + [System.GC]::Collect() |
| 35 | + [System.GC]::WaitForPendingFinalizers() |
| 36 | + Write-Host "[$Iteration] Measure : $Name" |
| 37 | + $sw = [System.Diagnostics.Stopwatch]::StartNew() |
| 38 | + $err = $null |
| 39 | + try { & $Action | Out-Null } catch { $err = $_.Exception.Message } |
| 40 | + $sw.Stop() |
| 41 | + $obj = [pscustomobject]@{ |
| 42 | + Iteration = $Iteration |
| 43 | + Scenario = $Name |
| 44 | + DurationMs = [int]$sw.Elapsed.TotalMilliseconds |
| 45 | + DurationS = [math]::Round($sw.Elapsed.TotalSeconds, 2) |
| 46 | + Timestamp = (Get-Date).ToString('o') |
| 47 | + Error = $err |
| 48 | + Module = (Get-Module GoogleFonts | Select-Object -First 1 -ExpandProperty Version).ToString() |
| 49 | + } |
| 50 | + ($obj | ConvertTo-Json -Compress) | Add-Content -LiteralPath $ResultsPath |
| 51 | + Write-Host "[$Iteration] Result : $Name -> $($obj.DurationS)s" |
| 52 | + return $obj |
| 53 | +} |
| 54 | + |
| 55 | +$results = @() |
| 56 | + |
| 57 | +$results += Measure-Scenario -Name 'Single-Roboto' ` |
| 58 | + -Setup { Invoke-Uninstall -Names 'Roboto' } ` |
| 59 | + -Action { Install-GoogleFont -Name 'Roboto' } |
| 60 | + |
| 61 | +$results += Measure-Scenario -Name "Subset-$($Subset -join '+')" ` |
| 62 | + -Setup { Invoke-Uninstall -Names $Subset } ` |
| 63 | + -Action { Install-GoogleFont -Name $Subset } |
| 64 | + |
| 65 | +$results += Measure-Scenario -Name 'Subset-AlreadyInstalled' ` |
| 66 | + -Setup { } ` |
| 67 | + -Action { Install-GoogleFont -Name $Subset } |
| 68 | + |
| 69 | +if ($IncludeAll) { |
| 70 | + $results += Measure-Scenario -Name 'All' ` |
| 71 | + -Setup { } ` |
| 72 | + -Action { Install-GoogleFont -All } |
| 73 | +} |
| 74 | + |
| 75 | +Write-Host '' |
| 76 | +Write-Host "Summary for iteration '$Iteration':" |
| 77 | +$results | Format-Table -AutoSize Iteration, Scenario, DurationS, Module |
0 commit comments