Repository navigation
PR-CI: Split Build and Analyze into independent LOC-based batched matrices #59444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ac921cb
Split Build/Analyze into independent LOC-weighted matrices
m-nash 590907f
Use separate AnalyzePackageInfoArtifact for analyze stage
m-nash 0dc37fc
Rename LocTargetSeconds -> LocTarget (and -TargetSeconds -> -Target)
m-nash 3f4aaf6
Remove time-unit suffixes from LOC weight log lines
m-nash fbc0eb3
Resolve DirectoryPath against RepoRoot in Get-PackageLocWeights
m-nash a679670
Clarify LOC weights key is ArtifactName
m-nash 4e4eadb
Clarify Target is a goal not a hard cap
m-nash f328047
Rename EnableBuildWeighting -> EnableLocWeighting
m-nash ca50ab0
Make Apply-WeightedBatching synopsis job/unit agnostic
m-nash 890e48b
Align WeightsFile help text with ArtifactName key
m-nash File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| parameters: | ||
| - name: Artifacts | ||
| type: object | ||
| default: [] | ||
| - name: ServiceDirectory | ||
| type: string | ||
| default: not-specified | ||
| - name: SDKType | ||
| type: string | ||
| default: all | ||
| - name: BuildSnippets | ||
| type: boolean | ||
| default: true | ||
| - name: DependsOn | ||
| type: string | ||
| default: '' | ||
| - name: CloudConfig | ||
| type: object | ||
| default: {} | ||
| - name: Matrix | ||
| type: string | ||
| - name: UsePlatformContainer | ||
| type: boolean | ||
| default: false | ||
| - name: OSName | ||
| type: string | ||
|
|
||
| jobs: | ||
| - job: | ||
| displayName: "Analyze" | ||
| dependsOn: ${{ parameters.DependsOn }} | ||
| condition: and(succeededOrFailed(), ne(${{ parameters.Matrix }}, '{}')) | ||
|
|
||
| strategy: | ||
| matrix: $[ ${{ parameters.Matrix }} ] | ||
|
|
||
| pool: | ||
| name: $(LINUXPOOL) | ||
| image: $(LINUXVMIMAGE) | ||
| os: linux | ||
|
|
||
| steps: | ||
| - template: /eng/pipelines/templates/steps/analyze.yml | ||
| parameters: | ||
| Artifacts: ${{ parameters.Artifacts }} | ||
| ServiceDirectory: ${{ parameters.ServiceDirectory }} | ||
| SDKType: ${{ parameters.SDKType }} | ||
| BuildSnippets: ${{ parameters.BuildSnippets }} |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| <# | ||
| .SYNOPSIS | ||
| Applies weighted LPT bin-packing to PackageInfo files for balanced CI test batching. | ||
|
|
||
| .DESCRIPTION | ||
| Reads PackageInfo JSON files and a test weights file, performs LPT (Longest Processing Time) | ||
| bin-packing to create balanced batches, then consolidates the PackageInfo files so that each | ||
| batch becomes a single representative file. The ArtifactName of each consolidated file contains | ||
| the comma-separated names of all packages in that batch, which flows through to the | ||
| ProjectNames parameter in the matrix generation. | ||
|
m-nash marked this conversation as resolved.
Resolved
|
||
|
|
||
| After this script runs, set PRJobBatchSize to 1 so each consolidated file becomes its own job. | ||
|
|
||
| .PARAMETER PackageInfoFolder | ||
| Path to the folder containing PackageInfo JSON files. | ||
|
|
||
| .PARAMETER WeightsFile | ||
| Path to the JSON weights file (package name → seconds). | ||
|
|
||
| .PARAMETER TargetSeconds | ||
| Target maximum time per bucket in seconds for direct packages. Default is 1800 (30 minutes). | ||
|
|
||
| .PARAMETER IndirectTargetSeconds | ||
| Target maximum time per bucket in seconds for indirect packages. Defaults to TargetSeconds if not specified. | ||
| Indirect packages only run on Linux, so they can use a higher target than direct packages which run across all platforms. | ||
|
m-nash marked this conversation as resolved.
Resolved
|
||
|
|
||
| .PARAMETER DefaultWeight | ||
| Weight assigned to packages not found in the weights file. Default is 1. | ||
| #> | ||
|
|
||
| [CmdletBinding()] | ||
| param ( | ||
| [Parameter(Mandatory = $true)][string]$PackageInfoFolder, | ||
| [Parameter(Mandatory = $true)][string]$WeightsFile, | ||
| [Parameter()][int]$TargetSeconds = 1800, | ||
| [Parameter()][int]$IndirectTargetSeconds = 0, | ||
| [Parameter()][int]$DefaultWeight = 1 | ||
| ) | ||
|
|
||
| Set-StrictMode -Version 4 | ||
|
|
||
| if ($IndirectTargetSeconds -le 0) { $IndirectTargetSeconds = $TargetSeconds } | ||
|
|
||
| # Load weights | ||
| $weights = @{} | ||
| if (Test-Path $WeightsFile) { | ||
| try { | ||
| $content = Get-Content $WeightsFile -Raw | ConvertFrom-Json | ||
| $content.PSObject.Properties | ForEach-Object { $weights[$_.Name] = [int]$_.Value } | ||
| } | ||
| catch { | ||
| Write-Warning "Failed to load weights: $($_.Exception.Message). Skipping weighted batching." | ||
| return | ||
| } | ||
| } | ||
|
|
||
| if ($weights.Count -eq 0) { | ||
| Write-Host "No weights available. Skipping weighted batching." | ||
| return | ||
| } | ||
|
|
||
| # Load all PackageInfo files | ||
| $packageFiles = Get-ChildItem -Path $PackageInfoFolder -Filter "*.json" -Recurse | ||
| $packages = @($packageFiles | ForEach-Object { | ||
| $json = Get-Content $_.FullName | ConvertFrom-Json | ||
| [PSCustomObject]@{ | ||
| FilePath = $_.FullName | ||
| FileName = $_.Name | ||
| Json = $json | ||
| } | ||
| }) | ||
|
|
||
| if ($packages.Count -eq 0) { | ||
| Write-Host "No PackageInfo files found. Skipping weighted batching." | ||
| return | ||
| } | ||
|
|
||
| # Separate direct and indirect packages | ||
| $directPackages = @($packages | Where-Object { $_.Json.IncludedForValidation -eq $false }) | ||
| $indirectPackages = @($packages | Where-Object { $_.Json.IncludedForValidation -eq $true }) | ||
|
|
||
| $totalPkgs = $directPackages.Count + $indirectPackages.Count | ||
| $matchedWeights = @($packages | Where-Object { $weights.ContainsKey($_.Json.ArtifactName) }).Count | ||
| Write-Host "Packages: $($directPackages.Count) direct, $($indirectPackages.Count) indirect ($matchedWeights/$totalPkgs have weights)" | ||
|
|
||
| function Apply-LPTBatching { | ||
| param( | ||
| [object[]]$Packages, | ||
| [hashtable]$Weights, | ||
| [int]$TargetSeconds, | ||
| [int]$DefaultWeight, | ||
| [string]$Label | ||
| ) | ||
|
|
||
| if ($Packages.Count -le 1) { | ||
| Write-Host " $Label`: Only $($Packages.Count) package(s), no batching needed." | ||
| return | ||
| } | ||
|
|
||
| # Build weighted items | ||
| $items = @(foreach ($pkg in $Packages) { | ||
| $name = $pkg.Json.ArtifactName | ||
| $weight = if ($Weights.ContainsKey($name)) { [int]$Weights[$name] } else { $DefaultWeight } | ||
| [PSCustomObject]@{ Package = $pkg; Weight = $weight; Name = $name } | ||
| }) | ||
|
|
||
| # Sort by weight descending (LPT: largest first) | ||
| $items = @($items | Sort-Object Weight -Descending) | ||
|
|
||
| # Calculate number of buckets | ||
| [int]$totalWeight = 0 | ||
| foreach ($i in $items) { $totalWeight += $i.Weight } | ||
| $numBuckets = [math]::Max(1, [math]::Ceiling($totalWeight / $TargetSeconds)) | ||
|
|
||
| # Don't create more buckets than packages | ||
| $numBuckets = [math]::Min($numBuckets, $Packages.Count) | ||
|
|
||
| Write-Host " $Label`: $($Packages.Count) packages, total weight ${totalWeight}s, target ${TargetSeconds}s -> $numBuckets buckets" | ||
|
|
||
| # Create buckets | ||
| $buckets = @() | ||
| for ($b = 0; $b -lt $numBuckets; $b++) { | ||
| $buckets += [PSCustomObject]@{ | ||
| Items = [System.Collections.ArrayList]::new() | ||
| TotalWeight = [int]0 | ||
| } | ||
| } | ||
|
|
||
| # Greedy LPT: assign each item to the lightest bucket | ||
| foreach ($item in $items) { | ||
| $lightest = $buckets | Sort-Object TotalWeight | Select-Object -First 1 | ||
| [void]$lightest.Items.Add($item) | ||
| $lightest.TotalWeight += $item.Weight | ||
| } | ||
|
|
||
| # Log bucket distribution with collapsible groups | ||
| $bucketIdx = 1 | ||
| foreach ($bucket in $buckets) { | ||
| $summary = "Bucket ${bucketIdx}: $($bucket.Items.Count) pkgs, weight $($bucket.TotalWeight)s ($([math]::Round($bucket.TotalWeight / 60, 1))m)" | ||
| Write-Host "##[group]$summary" | ||
| foreach ($item in ($bucket.Items | Sort-Object Name)) { | ||
| Write-Host " $($item.Name) ($($item.Weight)s)" | ||
| } | ||
| Write-Host "##[endgroup]" | ||
| $bucketIdx++ | ||
| } | ||
|
|
||
| # Consolidate: for each bucket, keep one representative file with all names | ||
| foreach ($bucket in $buckets) { | ||
| $batchItems = @($bucket.Items) | ||
| if ($batchItems.Count -eq 0) { continue } | ||
|
|
||
| # Use the first (heaviest) package as the representative | ||
| $representative = $batchItems[0].Package | ||
| $allNames = ($batchItems | ForEach-Object { $_.Name }) -join "," | ||
|
|
||
| # Update the representative's ArtifactName to contain all package names | ||
| $representative.Json.ArtifactName = $allNames | ||
| $representative.Json | ConvertTo-Json -Depth 100 | Set-Content $representative.FilePath -Encoding utf8 | ||
|
|
||
| # Delete the other package files in this batch | ||
| for ($i = 1; $i -lt $batchItems.Count; $i++) { | ||
| Remove-Item $batchItems[$i].Package.FilePath -Force | ||
| } | ||
| } | ||
|
|
||
| Write-Host " $Label`: Consolidated $($Packages.Count) files into $numBuckets batch files." | ||
| } | ||
|
|
||
| # Apply LPT batching to direct and indirect packages separately | ||
| if ($directPackages.Count -gt 0) { | ||
| Apply-LPTBatching -Packages $directPackages -Weights $weights ` | ||
| -TargetSeconds $TargetSeconds -DefaultWeight $DefaultWeight -Label "Direct" | ||
| } | ||
|
|
||
| if ($indirectPackages.Count -gt 0) { | ||
| Apply-LPTBatching -Packages $indirectPackages -Weights $weights ` | ||
| -TargetSeconds $IndirectTargetSeconds -DefaultWeight $DefaultWeight -Label "Indirect" | ||
| } | ||
|
|
||
| # Verify | ||
| $remaining = @(Get-ChildItem -Path $PackageInfoFolder -Filter "*.json" -Recurse).Count | ||
| Write-Host "Weighted batching complete. $remaining consolidated PackageInfo files remain." | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.