Skip to content

perf: cache JsonSerializerOptions in performance runner steps - #9735

Merged
Amaury Levé (Evangelink) merged 1 commit into
mainfrom
dev/amauryleve/cache-jsonserializeroptions-perf-runner
Jul 8, 2026
Merged

perf: cache JsonSerializerOptions in performance runner steps#9735
Amaury Levé (Evangelink) merged 1 commit into
mainfrom
dev/amauryleve/cache-jsonserializeroptions-perf-runner

Conversation

@Evangelink

Copy link
Copy Markdown
Member

Fixes#9714

Summary

Cache the JsonSerializerOptions { WriteIndented = true } instance as a private static readonly field in DotnetTestProcess, and remove the CA1869 pragma suppression that was previously hiding the repeated-construction anti-pattern.

JsonSerializerOptions construction triggers reflection-based scanning of converter types and type metadata. Constructing a new instance on every ExecuteAsync() call repeats this work unnecessarily — a benchmark tool that measures performance should not itself incur avoidable overhead.

Changes

FileChange
test/Performance/MSTest.Performance.Runner/Steps/DotnetTestProcess.csAdded private static readonly JsonSerializerOptions JsonOptions; removed #pragma warning disable/restore CA1869 pair

Note: PlainProcess.cs already had an equivalent cached JsonOptions field on this branch, so no change was needed there. The new field uses the same JsonOptions name for consistency.

Trade-offs

None. Purely mechanical — same behaviour and output, no logic changes.

Co-authored-by: Copilot App 223556219+Copilot@users.noreply.github.com

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
CopilotAI review requested due to automatic review settings July 8, 2026 14:23

CopilotAI 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 is a small performance/best-practices improvement to the internal performance-runner tooling under test/Performance/MSTest.Performance.Runner. It caches the JsonSerializerOptions { WriteIndented = true } instance in DotnetTestProcess as a private static readonly field instead of constructing a new instance on every ExecuteAsync() call, and removes the #pragma warning disable/restore CA1869 pair that was previously suppressing the analyzer warning for repeated construction.

I verified:

  • The new field JsonOptions uses PascalCase, consistent with the repo's private static readonly naming convention and with the identical JsonOptions field already present in the sibling PlainProcess.cs (line 11).
  • The only JsonSerializer.Serialize call in the file (line 124) now uses the cached field, so removing the CA1869 pragma is safe — no remaining inline new JsonSerializerOptions construction sites exist.
  • The UTF-8 BOM on line 1 is preserved.

Changes:

  • Added private static readonly JsonSerializerOptions JsonOptions = new() { WriteIndented = true }; to DotnetTestProcess.
  • Replaced the inline new JsonSerializerOptions { WriteIndented = true } at the serialization call with the cached field.
  • Removed the now-unnecessary #pragma warning disable/restore CA1869 pair.
Show a summary per file
FileDescription
test/Performance/MSTest.Performance.Runner/Steps/DotnetTestProcess.csCaches JsonSerializerOptions in a static readonly field, updates the serialize call to use it, and drops the CA1869 suppression pragma.

Review details

  • Files reviewed: 1/1 changed files
  • Comments generated: 0
  • Review effort level: Medium

@github-actions

Copy link
Copy Markdown
Contributor

🧪 Test quality grade — PR #9735

No new or modified test methods were identified in the changed regions of this PR. Nothing to grade.

Re-run with /grade-tests.

🤖 Automated content by GitHub Copilot. Generated by the Grade Tests on PR (on open / sync) workflow. · 18.9 AIC · ⌖ 7 AIC · ⊞ 9.5K · [◷]( · )

@github-actionsgithub-actionsBot 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.

Note

🤖 Automated review by GitHub Copilot. Generated by the Expert Code Review workflow. To request a follow-up action, reply by tagging @copilot directly.

✅ 22/22 dimensions clean — no findings.

The change correctly caches JsonSerializerOptions as a private static readonly field, consistent with the existing pattern in PlainProcess.cs. JsonSerializerOptions is thread-safe once frozen (which happens on first serialization call), so the static field is safe even under concurrent access. The CA1869 pragma removal is appropriate now that the analyzer's guidance is followed.

@EvangelinkAmaury Levé (Evangelink) added the state/needs-review Awaiting review from the team. label Jul 8, 2026
@Evangelink
Amaury Levé (Evangelink) enabled auto-merge (squash) July 8, 2026 15:26
@Evangelink
Amaury Levé (Evangelink) merged commit e3eb0d1 into mainJul 8, 2026
38 of 41 checks passed
@Evangelink
Amaury Levé (Evangelink) deleted the dev/amauryleve/cache-jsonserializeroptions-perf-runner branch July 8, 2026 16:45
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

state/needs-reviewAwaiting review from the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[efficiency-improver] perf: cache JsonSerializerOptions in performance runner steps

3 participants

@Evangelink@0101