Skip to content

[.NET 10] Add layout performance logging#30130

Closed
jsuarezruiz wants to merge 74 commits intonet10.0from
fix-28091-II
Closed

[.NET 10] Add layout performance logging#30130
jsuarezruiz wants to merge 74 commits intonet10.0from
fix-28091-II

Conversation

@jsuarezruiz
Copy link
Contributor

@jsuarezruiz jsuarezruiz commented Jun 23, 2025

Description of Change

This PR includes changes to add an initial and basic performance monitoring system for the .NET MAUI framework, focusing on performance layout metrics. The system is integrated directly into the MAUI framework, instruments from the System.Diagnostics.Metrics API.

image

Based on #29972

@jsuarezruiz jsuarezruiz added t/enhancement ☀️ New feature or request perf/general The issue affects performance (runtime speed, memory usage, startup time, etc.) (sub: perf) labels Jun 23, 2025
@jsuarezruiz jsuarezruiz changed the title Fix 28091 ii [.NET 10] Add layout performance logging Jun 23, 2025
@jsuarezruiz jsuarezruiz marked this pull request as ready for review July 2, 2025 10:55
@jsuarezruiz jsuarezruiz requested a review from a team as a code owner July 2, 2025 10:55
@PureWeen PureWeen added this to the .NET 10.0-preview7 milestone Jul 9, 2025
Copilot AI review requested due to automatic review settings July 25, 2025 16:54
Copy link
Contributor

Copilot AI left a comment

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 adds an initial performance monitoring system to .NET MAUI, specifically focusing on layout performance metrics. It introduces a comprehensive profiling infrastructure that tracks measure and arrange pass durations using the System.Diagnostics.Metrics API, allowing developers to monitor layout performance in real-time.

Key changes include:

  • Introduction of a new PerformanceProfiler system with layout tracking capabilities
  • Integration of performance tracking directly into VisualElement's measure and arrange operations
  • Addition of metrics support controlled by runtime feature switches

Reviewed Changes

Copilot reviewed 47 out of 48 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/Core/src/PerformanceProfiler/* New performance profiling infrastructure including tracker interfaces, models, and extension methods
src/Core/src/RuntimeFeature.cs Added IsMetricsSupported feature switch with default true value
src/Core/src/Hosting/MauiAppBuilder.cs Integrated performance configuration into default MAUI app setup
src/Controls/src/Core/VisualElement/VisualElement.cs Added performance tracking to measure and arrange operations
src/Core/tests/UnitTests/PerformanceProfilerTests.cs Comprehensive unit tests for the performance profiling system
src/Controls/tests/TestCases.HostApp/Issues/* Test UI pages demonstrating performance profiling features
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/* UI test automation for performance profiling validation
src/Compatibility/Core/src//.cs Updated existing Performance references to use Internals.Performance namespace
docs/design/FeatureSwitches.md Documentation for the new MetricsSupport feature switch
Comments suppressed due to low confidence (1)

builder.Services.AddSingleton<ILayoutPerformanceTracker, LayoutPerformanceTracker>();

// Initialize the PerformanceProfiler
using (var serviceProvider = builder.Services.BuildServiceProvider())
Copy link

Copilot AI Jul 25, 2025

Choose a reason for hiding this comment

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

Building a service provider during configuration can be expensive and is generally discouraged. Consider registering the initialization as a hosted service or using a factory pattern instead of building the service provider here.

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

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

Is this the only way to get it ? I wonder if it bring side effects if theres a lot of things registered, like user viewModels etc.. DO we have an internal service provider? I remember we had one in the past for our internal stuff.

@dotnet dotnet deleted a comment from PureWeen Jul 31, 2025
Copy link
Member

@rmarinho rmarinho left a comment

Choose a reason for hiding this comment

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

Some comments, and it seems is leaking elements as some device tests are failing,

builder.Services.AddSingleton<ILayoutPerformanceTracker, LayoutPerformanceTracker>();

// Initialize the PerformanceProfiler
using (var serviceProvider = builder.Services.BuildServiceProvider())
Copy link
Member

Choose a reason for hiding this comment

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

Is this the only way to get it ? I wonder if it bring side effects if theres a lot of things registered, like user viewModels etc.. DO we have an internal service provider? I remember we had one in the past for our internal stuff.

_measureDurationHistogram = mauiPerformanceMeter.Meter.CreateHistogram<double>(
"maui.layout.measure.duration",
unit: "ms",
description: "Duration of layout measure passes");
Copy link
Member

Choose a reason for hiding this comment

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

This should be using Resources so it can be localized for the developer no?

break;

default:
Debug.WriteLine($"[Performance Profiler] {_category} took {elapsed:0.00} ms.");
Copy link
Member

Choose a reason for hiding this comment

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

Should we use the logger here ?

/// </summary>
/// <param name="duration">The duration of the arrange pass in milliseconds.</param>
/// <param name="element">The element being arranged, or null if not specified.</param>
public void RecordArrangePass(double duration, object? element = null)
Copy link
Member

Choose a reason for hiding this comment

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

IN what case can element be null here?

builder.Services.AddSingleton<MauiPerformanceMeter>();

// Register core services
builder.Services.AddSingleton<ILayoutPerformanceTracker, LayoutPerformanceTracker>();
Copy link
Member

Choose a reason for hiding this comment

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

Can we use the factory method here, so when is created we can Initialize ?

 builder.Services.AddSingleton<ILayoutPerformanceTracker>( () => 
{ 
   var tracker = LayoutPerformanceTracker>();
   PerformanceProfiler.Initialize(tracker);
  return tracker; 
});

@PureWeen PureWeen closed this Aug 7, 2025
@github-project-automation github-project-automation bot moved this from Changes Requested to Done in MAUI SDK Ongoing Aug 7, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Sep 7, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

p/0 Current heighest priority issues that we are targeting for a release. perf/general The issue affects performance (runtime speed, memory usage, startup time, etc.) (sub: perf) t/enhancement ☀️ New feature or request

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

9 participants