Skip to content

Batch WithSource project-file rewrites into a single pass - #55269

Merged
Evangelink merged 5 commits into
mainfrom
dev/amauryleve/test-slowness-analysis
Jul 20, 2026
Merged

Evangelink merged 5 commits into
mainfrom
dev/amauryleve/test-slowness-analysis

Conversation

@Evangelink

@Evangelink Evangelink commented Jul 14, 2026 •

Copy link
Copy Markdown
Member

Why

TestAsset.WithSource() is the standard setup path for hundreds of SDK integration tests: it copies a test asset onto disk and then rewrites the project files to substitute the current target framework, the latest runtime identifiers, and every package-version variable. Because this runs during the setup of every WithSource-based test — including fast ones — any wasted work here is pure overhead paid across the whole suite.

Two sources of avoidable work were baked into that setup:

  1. Substitutions re-parsed and re-wrote every project file ~14 times. WithSource applied its ~14 substitutions by calling UpdateProjProperty / ReplacePackageVersionVariable in a loop, and each of those independently reloaded and re-serialized every project file in the asset. A single WithSource call performed on the order of 14 XDocument.Load + File.Save round-trips per project file.

  2. Every project file was written twice. WithSource first File.Copy'd every source file to the destination, and then the substitution pass immediately overwrote each project/.xml file with a fresh Save — so the copy's write was thrown away.

Neither is a headline perf win on its own — real test time is dominated by the dotnet build/restore subprocesses each test spawns. But this is avoidable setup work and, more importantly, avoidable write amplification. Fewer file writes matters most on CI/Helix, where each write is scanned by antivirus and hits slower storage — exactly the environments where test wall-clock is most visible.

What

  • Batch substitutions into a single pass. Extracted the per-document mutation bodies into private static helpers (ApplyUpdateProjProperty, ApplyReplacePackageVersionVariable) and apply all property/package-version substitutions in one load/mutate/save per project file instead of once per substitution. The public UpdateProjProperty / ReplacePackageVersionVariable methods delegate to the same helpers, so external callers are unchanged.
  • Drop the redundant copy. Skip File.Copy for the project/.xml files that the substitution pass rewrites; load those straight from the source and write the substituted result to the destination once. Non-project files are still copied verbatim.

No change to what the test framework produces.

Impact

For a project file in a WithSource asset, the file writes during setup drop from ~15 (1 copy + ~14 substitution saves) to 1. Measured over the full test-asset corpus, the redundant-copy removal alone eliminates 404 writes (~22%) of WithSource's file writes.

Validation

  • Builds cleanly (Microsoft.NET.TestFramework).
  • Behavior equivalence proven empirically, twice:
    • Batched single-save vs original multi-save substitution logic: byte-identical output across all 352 test-asset .csproj files (0 mismatches).
    • Copy-then-rewrite vs rewrite-from-source flow: byte-identical output across all 257 test assets (1439 files, 0 mismatches), confirming File.Copy(source→dest) then load-from-dest is equivalent to loading from source.

TestAsset.WithSource applied ~14 property/package-version substitutions by
calling UpdateProjProperty/ReplacePackageVersionVariable in a loop, each of
which reloaded and rewrote every project file in the asset. That meant ~14
XDocument load+save round-trips per project file during the setup of every
test that calls WithSource (the standard setup path for hundreds of SDK
integration tests).

Extract the per-document mutation bodies into private static helpers and apply
all substitutions in one WithProjectChanges pass, so each project file is
loaded and saved once. Behavior is unchanged: verified byte-identical output
across all 352 test-asset csproj files.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: a22eead4-88ca-4142-a6d8-a7199db73289
Copilot AI review requested due to automatic review settings July 14, 2026 14:22
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
2 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR reduces setup overhead in SDK integration tests by batching TestAsset.WithSource() project-file rewrites into a single load/mutate/save pass per project file, instead of reloading and rewriting the same files once per substitution.

Changes:

  • Batched all WithSource property and package-version substitutions into a single WithProjectChanges(...) call per asset.
  • Extracted the XML mutation bodies into private helpers (ApplyUpdateProjProperty, ApplyReplacePackageVersionVariable) and made the public APIs delegate to them.
Show a summary per file
File Description
test/Microsoft.NET.TestFramework/TestAsset.cs Refactors WithSource to apply all substitutions in one WithProjectChanges pass; introduces shared helper methods for property and package-version mutations.

Copilot's findings

  • Files reviewed: 1/1 changed files
  • Comments generated: 1

Evangelink and others added 4 commits July 14, 2026 16:39
WithSource copied every source file to the destination and then, in the
substitution pass, reloaded and re-saved each project/.xml file with the
target-framework, runtime-identifier, and package-version substitutions
applied. That meant every project file was written twice: once by File.Copy
and again by the substitution save that immediately overwrote it.

Skip the File.Copy for files that the substitution pass rewrites; instead load
those directly from the source and write the substituted result straight to the
destination once. Non-project files are still copied verbatim.

Because File.Copy is an exact byte copy, loading from the source is equivalent
to loading from the copied destination, so output is unchanged: verified
byte-identical across all 257 test assets (1439 files, 0 differences) while
eliminating 404 redundant writes (~22% of WithSource's file writes).

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: a22eead4-88ca-4142-a6d8-a7199db73289
Address review feedback: use OfType<XAttribute>() to filter out null
attributes returned by the LINQ chain instead of a redundant inner null-check.
Behavior is unchanged.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: a22eead4-88ca-4142-a6d8-a7199db73289
@Evangelink
Evangelink enabled auto-merge July 17, 2026 13:22
@Evangelink
Evangelink merged commit 0a307c1 into main Jul 20, 2026
27 checks passed
@Evangelink
Evangelink deleted the dev/amauryleve/test-slowness-analysis branch July 20, 2026 12:54
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jul 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants