Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions .github/workflows/quality-gates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,20 @@ jobs:
# The log readers on a generated log of 10,000 lines (TST-43), in their declared order and
# outside the coverage run, whose instrumentation would slow what they measure. Each test
# holds an absolute limit with room for a slow runner and a check that four times the lines
# take less than six times as long; the MEASURE lines in the log are the figures the
# performance gate (#136) will record as baselines.
- name: Performance tests
# take less than six times as long. The suite runs three times and writes each measurement
# to measurements.jsonl, and the performance gate (CIC-13) compares the median of each with
# the baseline recorded for this runner image in Tests/Performance/Baselines.psd1. A new
# image has no baseline: the gate warns, prints the entry to record, and passes.
- name: Performance tests and gate
shell: pwsh
run: |
$ErrorActionPreference = 'Stop'
./Tests/Invoke-Tests.ps1 -TestType Performance -OutputPath ./reports/performance -Output Detailed -SummaryPath $env:GITHUB_STEP_SUMMARY
$null = New-Item -ItemType Directory -Path ./reports/performance -Force
$env:ISL_PERFORMANCE_RESULTS = Join-Path -Path (Resolve-Path ./reports/performance) -ChildPath 'measurements.jsonl'
foreach ($run in 1..3) {
./Tests/Invoke-Tests.ps1 -TestType Performance -Output Detailed -OutputPath "./reports/performance/run$run" -SummaryPath $env:GITHUB_STEP_SUMMARY
}
./Build/Test-PerformanceBaseline.ps1 -ResultPath $env:ISL_PERFORMANCE_RESULTS

- name: Upload test results and coverage
if: always()
Expand Down
247 changes: 247 additions & 0 deletions Build/Test-PerformanceBaseline.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
#Requires -Version 5.1

<#
.SYNOPSIS
Compares the measurements of the performance tests with their recorded baselines.

.DESCRIPTION
The performance gate of CIC-13. Tests/Performance writes one JSON line for each measurement
to the file that ISL_PERFORMANCE_RESULTS names: the name of the reader, the lines of the log
it read, the seconds it took and the megabytes its result kept. When the suite has run several
times, the script takes the median of each measurement, so that one slow run does not decide,
and compares it with the baseline recorded for the machine it runs on.

Baselines are recorded for a runner: the image of a GitHub-hosted runner (the ImageOS variable
it sets, such as win25), its architecture and the PowerShell version, as in win25/X64/7.6. A
measurement fails when its median is more than the margin above its baseline: a margin of 0.5
lets it be half as large again. A machine with no baseline, a developer's computer or a new
runner image, is reported and not gated, so that a new image does not fail every build; its
medians are printed in the form of a baseline entry, to be recorded with -Record.

.PARAMETER ResultPath
The file of JSON lines that the performance tests wrote. The tests append to it, so delete it
before a new recording; measurements from more than one PowerShell version are refused.

.PARAMETER BaselinePath
The baselines. Default: Tests/Performance/Baselines.psd1.

.PARAMETER Runner
The runner to compare with. Default: ImageOS/architecture/major.minor of PowerShell on a
GitHub-hosted runner, and the computer's name elsewhere.

.PARAMETER Record
Prints the medians as a baseline entry for the runner and compares nothing.

.EXAMPLE
./Build/Test-PerformanceBaseline.ps1 -ResultPath ./reports/performance/measurements.jsonl

Compares the medians of the measurements with the baselines of this runner.

.EXAMPLE
./Build/Test-PerformanceBaseline.ps1 -ResultPath ./measurements.jsonl -Record

Prints the medians as the baseline entry of this runner.

.INPUTS
None.

.OUTPUTS
None. A line for each measurement, as information records.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$ResultPath,

[Parameter()]
[string]$BaselinePath,

[Parameter()]
[string]$Runner,

[Parameter()]
[switch]$Record
)

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

# Windows PowerShell 5.1 has no $PSScriptRoot while it binds the parameters, so the defaults are set here
if (-not $BaselinePath) {
$repositoryRoot = Split-Path -Path $PSScriptRoot -Parent
$testsFolder = Join-Path -Path $repositoryRoot -ChildPath 'Tests'
$performanceFolder = Join-Path -Path $testsFolder -ChildPath 'Performance'
$BaselinePath = Join-Path -Path $performanceFolder -ChildPath 'Baselines.psd1'
}
if (-not $Runner) {
$version = '{0}.{1}' -f $PSVersionTable.PSVersion.Major, $PSVersionTable.PSVersion.Minor
$architecture = [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture
$machine = if ($env:ImageOS) { $env:ImageOS } else { $env:COMPUTERNAME }
$Runner = '{0}/{1}/{2}' -f $machine, $architecture, $version
}

function Format-Invariant {
<#
.SYNOPSIS
Formats a string with its numbers written as the invariant culture writes them.

.DESCRIPTION
The -f operator writes a number in the current culture, 1,06 in German, which a baseline
entry pasted into Baselines.psd1 would then read as an array. This writes 1.06 whatever the
culture.

.PARAMETER Format
The format string, as for -f.

.PARAMETER Argument
The values to put into it.

.EXAMPLE
Format-Invariant -Format '{0} s' -Argument 1.06

Returns '1.06 s' in every culture.

.INPUTS
None.

.OUTPUTS
System.String. The formatted string.
#>
[CmdletBinding()]
[OutputType([string])]
param(
[Parameter(Mandatory)]
[string]$Format,

[Parameter(Mandatory)]
[AllowNull()]
[object[]]$Argument
)

[string]::Format([System.Globalization.CultureInfo]::InvariantCulture, $Format, $Argument)
}


function Get-Median {
<#
.SYNOPSIS
Gets the median of a set of numbers.

.DESCRIPTION
Sorts the numbers and returns the middle one, or the mean of the two in the middle when
there is an even number of them.

.PARAMETER Value
The numbers.

.EXAMPLE
Get-Median -Value 3, 1, 2

Returns 2.

.INPUTS
None.

.OUTPUTS
System.Double. The median.
#>
[CmdletBinding()]
[OutputType([double])]
param(
[Parameter(Mandatory)]
[double[]]$Value
)

$sorted = @($Value | Sort-Object)
$middle = [math]::Floor($sorted.Count / 2)
if ($sorted.Count % 2 -eq 1) {
$sorted[$middle]
} else {
($sorted[$middle - 1] + $sorted[$middle]) / 2
}
}


$lines = @([System.IO.File]::ReadAllLines((Resolve-Path -LiteralPath $ResultPath).ProviderPath) |
Where-Object { $_.Trim() })
if ($lines.Count -eq 0) {
throw "No measurements in '$ResultPath': the performance tests wrote nothing."
}
$results = @($lines | ForEach-Object { $_ | ConvertFrom-Json })
# One recording per file: medians over two PowerShell versions, or two versions of the code, would
# describe neither
$versions = @($results | ForEach-Object { $_.PSObject.Properties['PowerShell'] } | Where-Object { $_ } |
ForEach-Object { "$($_.Value)" } | Sort-Object -Unique)
if ($versions.Count -gt 1) {
throw ("The measurements in '$ResultPath' come from more than one PowerShell version " +
"($($versions -join ', ')). Delete the file and record again.")
}
$medians = @(
foreach ($group in @($results | Group-Object -Property Name, Lines | Sort-Object -Property Name)) {
$first = $group.Group[0]
[pscustomobject]@{
Key = '{0}/{1}' -f $first.Name, $first.Lines
Runs = $group.Count
Seconds = [math]::Round((Get-Median -Value @($group.Group.Seconds)), 2)
Megabytes = [math]::Round((Get-Median -Value @($group.Group.Megabytes)), 1)
}
}
)

if ($Record) {
$entry = foreach ($median in $medians) {
$values = $median.Key, $median.Seconds, $median.Megabytes
Format-Invariant -Format " '{0}' = @{{ Seconds = {1}; Megabytes = {2} }}" -Argument $values
}
Write-Information -InformationAction Continue -MessageData (
"Baseline entry for '$Runner':`n '$Runner' = @{`n$($entry -join "`n")`n }")
return
}

$baselines = Import-PowerShellDataFile -LiteralPath $BaselinePath
foreach ($key in 'Margin', 'Runners') {
if (-not $baselines.ContainsKey($key)) {
throw "The baselines '$BaselinePath' have no '$key' table."
}
}
if (-not $baselines['Runners'].ContainsKey($Runner)) {
$measured = ($medians | ForEach-Object {
Format-Invariant -Format '{0} {1} s {2} MB' -Argument $_.Key, $_.Seconds, $_.Megabytes
}) -join '; '
Write-Warning ("No performance baseline for the runner '$Runner', so nothing is gated. Measured " +
"(median of the runs): $measured. Record one with -Record.")
return
}

$runnerBaselines = $baselines['Runners'][$Runner]
$failures = [System.Collections.Generic.List[string]]::new()
foreach ($median in $medians) {
if (-not $runnerBaselines.ContainsKey($median.Key)) {
Write-Information -InformationAction Continue -MessageData (
"$($median.Key): $($median.Seconds) s, $($median.Megabytes) MB, no baseline")
continue
}
$baseline = $runnerBaselines[$median.Key]
foreach ($measure in 'Seconds', 'Megabytes') {
$limit = [math]::Round($baseline[$measure] * (1 + $baselines['Margin'][$measure]), 2)
$value = $median.$measure
$verdict = if ($value -gt $limit) { 'over' } else { 'within' }
$values = $median.Key, $value, $measure, $verdict, $limit, $baseline[$measure], $median.Runs
$lineFormat = '{0}: {1} {2}, {3} the limit {4} (baseline {5}, median of {6} runs)'
$line = Format-Invariant -Format $lineFormat -Argument $values
Write-Information -MessageData $line -InformationAction Continue
if ($verdict -eq 'over') {
$failures.Add($line)
}
}
}
foreach ($baselineKey in $runnerBaselines.Keys) {
if ($baselineKey -notin $medians.Key) {
$failures.Add("$($baselineKey): has a baseline, and the performance tests measured nothing for it")
}
}
if ($failures.Count -gt 0) {
throw "The performance gate failed for '$Runner':`n$($failures -join "`n")"
}
Write-Information -InformationAction Continue -MessageData (
"Performance gate: $($medians.Count) measurement(s) within the baselines of '$Runner'.")
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ entries are carried over from the manifest's release notes.

### Added

- A performance gate in CI: the tests job runs the performance tests three times and fails when
the median time of a reader at 10,000 lines is more than twice its baseline for that runner
image, or its memory a quarter more, recorded in `Tests/Performance/Baselines.psd1` (#136).
- Performance tests for `Get-IntuneAgentLog` and `Get-IntuneAgentTimeline` on a generated log of
10,000 lines, run with `Tests/Invoke-Tests.ps1 -TestType Performance` and in CI, and security
tests, tagged `Security` and run with `-Tag Security`, that a `-Credential` password reaches the
Expand Down
1 change: 1 addition & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ vault is searched, and the secret stays a `SecureString` until the publish call.
./Tests/Invoke-Tests.ps1 # every suite, shuffled; suites that need elevation, a lab account or PSScriptAnalyzer skip themselves
./Tests/Invoke-Tests.ps1 -CodeCoverage # the same with the 80% coverage gate, as the tests job runs it
./Tests/Invoke-Tests.ps1 -TestType Performance # the log readers on a generated log, not part of the default run
./Build/Test-PerformanceBaseline.ps1 -ResultPath <file> -Record # baselines from ISL_PERFORMANCE_RESULTS, see the tests job
./Tests/Invoke-Tests.ps1 -TestType Unit -Tag Security # where a -Credential password goes, and the checks on input
./Build/Invoke-Analysis.ps1 # PSScriptAnalyzer with the settings file, as the tests job runs it
./Build/Build-Help.ps1 -SkipUpdate # rebuild MAML after editing docs/, the form CI compares
Expand Down
18 changes: 17 additions & 1 deletion Tests/Performance/AgentLogReader.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
memory it holds against a limit with room for a slow runner, and the time at four times the
lines against six times the time of the smaller log, which a reader that grows linearly stays
well inside and one that grows with the square of the lines does not. Every measurement is
written to the information stream as a MEASURE line, for the baselines of the performance gate.
written to the information stream as a MEASURE line and, when ISL_PERFORMANCE_RESULTS names a
file, as a JSON line to it, which the performance gate (Build/Test-PerformanceBaseline.ps1)
compares with Baselines.psd1.

The readers run once on a small log before they are measured, so that the first measurement
does not carry the one-time cost of compiling the module's script blocks and expressions. On a
Expand Down Expand Up @@ -145,6 +147,20 @@ BeforeAll {
$line = 'MEASURE {0} lines={1} seconds={2} megabytes={3} powershell={4}' -f $Name, $Lines,
$measurement.Seconds, $measurement.Megabytes, $PSVersionTable.PSVersion
Write-Information -MessageData $line -InformationAction Continue
# For the performance gate (Build/Test-PerformanceBaseline.ps1): one JSON line for each
# measurement, appended to the file the variable names
if ($env:ISL_PERFORMANCE_RESULTS) {
$record = [ordered]@{
Name = $Name
Lines = $Lines
Seconds = $measurement.Seconds
Megabytes = $measurement.Megabytes
PowerShell = "$($PSVersionTable.PSVersion)"
}
$json = ($record | ConvertTo-Json -Compress) + "`n"
$utf8 = [System.Text.UTF8Encoding]::new($false)
[System.IO.File]::AppendAllText($env:ISL_PERFORMANCE_RESULTS, $json, $utf8)
Comment thread
fadwen-ai marked this conversation as resolved.
Resolved
}
$measurement
}

Expand Down
25 changes: 25 additions & 0 deletions Tests/Performance/Baselines.psd1
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# The baselines of the performance gate (CIC-13, Build/Test-PerformanceBaseline.ps1): for each
# runner, the median seconds and the megabytes kept of each reader at 10,000 lines, recorded with
# Test-PerformanceBaseline.ps1 -Record from the hosted runs named in the comment of each runner.
# A median more than the margin above its baseline fails the build.
#
# The margins come from the first recorded runs. Three jobs on win25-vs2026, three runs of the
# suite each: the medians of the jobs were 0.90 to 1.10 seconds for the log reader and 1.24 to
# 1.69 for the timeline, up to a third apart, and the first run of each job took twice as long as
# the other two, which the median leaves out. The memory kept was the same in every run. A median
# twice its baseline, or a quarter more memory, is therefore a change in the code, not the runner.
@{
Margin = @{
Seconds = 1.0
Megabytes = 0.25
}

Runners = @{
# Quality Gates runs 45726300466, 45726303917 and 45726307359, 2026-11-26; the medians of
# the middle one
'win25-vs2026/X64/7.6' = @{
'Get-IntuneAgentLog/10000' = @{ Seconds = 1.06; Megabytes = 25.7 }
'Get-IntuneAgentTimeline/10000' = @{ Seconds = 1.63; Megabytes = 20.2 }
}
}
}
Loading