Skip to content

Add diagnostic metrics/activities #18314

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 20 commits into from
Mar 6, 2025
Merged

Add diagnostic metrics/activities #18314

merged 20 commits into from
Mar 6, 2025

Conversation

maxkatz6
Copy link
Member

@maxkatz6 maxkatz6 commented Feb 25, 2025

What does the pull request do?

  1. Histogram metrics
    avalonia.comp.render.time
    avalonia.comp.update.time
    avalonia.ui.measure.time
    avalonia.ui.arrange.time
    avalonia.ui.render.time
    avalonia.ui.input.time
  2. Observable updowncount metrics
    avalonia.ui.event.handler.count
    avalonia.ui.visual.count
    avalonia.ui.dispatcher.timer.count
  3. Activities:
    Avalonia.AttachingStyle
    Avalonia.FindingResource
    Avalonia.EvaluatingStyle
    Avalonia.MeasuringLayoutable
    Avalonia.ArrangingLayoutable
    Avalonia.PerformingHitTest
    Avalonia.RaisingRoutedEvent

These are not considered a public API and might be removed or changed. As it's likely we didn't get things right on the first try.
We also might want to split histogram metrics per TopLevel. But probably not in this PR.

What is the updated/expected behavior with this PR?

RuntimeCongig Avalonia.Diagnostics.Diagnostic.IsEnabled enabled these meters and activities. Disabled by default.
Additionally, metrics can be additionally disabled via built-in System.Diagnostics.Metrics.Meter.IsSupported.

Fixed issues

Fixes #12032

Benchmarks

Benchmarks without any listeners, just master + with runtime config enabled + without runtime config enabled.
Just to see if this PR doesn't regress anything.
In summary, when Avalonia.Diagnostics.Diagnostic.IsEnabled is enabled, some code paths, especially style attaching, is noticably slower on becnmarks. Makes sense to keep it disabled by default.

Remeasure

master

Method Mean Error StdDev Gen0 Allocated
Remeasure 2.681 ms 0.0536 ms 0.0785 ms 7.8125 76.85 KB

with enabled runtime config

Method Mean Error StdDev Median Gen0 Allocated
Remeasure 2.843 ms 0.0615 ms 0.1784 ms 2.766 ms 7.8125 76.85 KB

with disabled runtime config (default)

Method Mean Error StdDev Median Gen0 Allocated
Remeasure 2.730 ms 0.0645 ms 0.1892 ms 2.789 ms 7.8125 76.85 KB
FindPreResource + FindPostResource + FindNotExistingResource

master

Method Mean Error StdDev Allocated
FindPreResource 323.38 us 3.992 us 3.734 us -
FindPostResource 12.60 us 0.153 us 0.143 us -
FindNotExistingResource 362.47 us 4.056 us 3.595 us -

with enabled runtime config

Method Mean Error StdDev Allocated
FindPreResource 336.48 us 1.867 us 1.559 us -
FindPostResource 13.34 us 0.115 us 0.108 us -
FindNotExistingResource 355.55 us 3.120 us 2.605 us -

with disabled runtime config (default)

Method Mean Error StdDev Allocated
FindPreResource 351.47 us 3.093 us 2.893 us -
FindPostResource 12.65 us 0.090 us 0.075 us -
FindNotExistingResource 356.66 us 4.541 us 4.248 us -
Apply_Simple_Styles

master

Method MatchingStyles NonMatchingStyles Mean Error StdDev Gen0 Gen1 Gen2 Allocated
Apply_Simple_Styles 1 1 190.2 ns 3.69 ns 4.93 ns 0.1404 0.0005 0.0002 1.15 KB
Apply_Simple_Styles 1 5 236.9 ns 5.95 ns 17.54 ns 0.1404 0.0005 0.0002 1.15 KB
Apply_Simple_Styles 1 50 587.3 ns 8.86 ns 7.40 ns 0.1402 - - 1.15 KB
Apply_Simple_Styles 5 1 299.7 ns 6.03 ns 6.19 ns 0.1507 - - 1.23 KB
Apply_Simple_Styles 5 5 325.3 ns 3.47 ns 3.25 ns 0.1507 - - 1.23 KB
Apply_Simple_Styles 5 50 725.9 ns 14.51 ns 20.81 ns 0.1507 - - 1.23 KB
Apply_Simple_Styles 50 1 1,705.0 ns 33.15 ns 44.25 ns 0.2651 - - 2.18 KB
Apply_Simple_Styles 50 5 1,676.6 ns 33.52 ns 69.97 ns 0.2651 - - 2.18 KB
Apply_Simple_Styles 50 50 1,922.0 ns 26.38 ns 22.03 ns 0.2632 - - 2.18 KB

with enabled runtime config

Method MatchingStyles NonMatchingStyles Mean Error StdDev Gen0 Gen1 Allocated
Apply_Simple_Styles 1 1 202.7 ns 3.92 ns 3.85 ns 0.1404 0.0005 1.15 KB
Apply_Simple_Styles 1 5 302.4 ns 6.03 ns 7.18 ns 0.1402 0.0005 1.15 KB
Apply_Simple_Styles 1 50 1,340.0 ns 26.31 ns 46.08 ns 0.1392 - 1.15 KB
Apply_Simple_Styles 5 1 364.7 ns 5.76 ns 5.39 ns 0.1507 - 1.23 KB
Apply_Simple_Styles 5 5 457.9 ns 6.02 ns 5.34 ns 0.1507 - 1.23 KB
Apply_Simple_Styles 5 50 1,450.9 ns 19.74 ns 18.47 ns 0.1507 - 1.23 KB
Apply_Simple_Styles 50 1 2,012.9 ns 24.76 ns 23.16 ns 0.2632 - 2.18 KB
Apply_Simple_Styles 50 5 2,104.4 ns 30.01 ns 28.08 ns 0.2632 - 2.18 KB
Apply_Simple_Styles 50 50 3,123.6 ns 51.32 ns 48.00 ns 0.2632 - 2.18 KB

with disabled runtime config (default)

Method MatchingStyles NonMatchingStyles Mean Error StdDev Gen0 Gen1 Allocated
Apply_Simple_Styles 1 1 188.1 ns 3.77 ns 5.04 ns 0.1404 0.0005 1.15 KB
Apply_Simple_Styles 1 5 221.5 ns 4.41 ns 4.72 ns 0.1404 0.0005 1.15 KB
Apply_Simple_Styles 1 50 621.4 ns 7.46 ns 6.98 ns 0.1402 - 1.15 KB
Apply_Simple_Styles 5 1 306.9 ns 5.31 ns 4.96 ns 0.1507 - 1.23 KB
Apply_Simple_Styles 5 5 337.3 ns 2.48 ns 2.20 ns 0.1507 - 1.23 KB
Apply_Simple_Styles 5 50 684.4 ns 1.90 ns 1.77 ns 0.1507 - 1.23 KB
Apply_Simple_Styles 50 1 1,574.7 ns 5.29 ns 4.69 ns 0.2651 - 2.18 KB
Apply_Simple_Styles 50 5 1,591.5 ns 4.50 ns 3.76 ns 0.2651 - 2.18 KB
Apply_Simple_Styles 50 50 1,952.9 ns 3.82 ns 3.38 ns 0.2632 - 2.18 KB
Apply_Detach_Styles

master

Method Mean Error StdDev Gen0 Allocated
Apply_Detach_Styles 13.64 us 0.267 us 0.467 us 0.6256 5.12 KB

with enabled runtime config

Method Mean Error StdDev Gen0 Allocated
Apply_Detach_Styles 22.19 us 0.210 us 0.197 us 0.6104 5.12 KB

with disabled runtime config (default)

Method Mean Error StdDev Gen0 Allocated
Apply_Detach_Styles 12.87 us 0.048 us 0.045 us 0.6256 5.12 KB
Apply style

master

Method Mean Error StdDev Gen0 Gen1 Allocated
Apply 112.6 ns 2.22 ns 3.78 ns 0.0415 0.0020 348 B
Apply_Toggle 1,023.2 ns 14.39 ns 13.46 ns 0.0415 0.0012 356 B
Apply_Detach 135.1 ns 0.76 ns 0.72 ns 0.0415 0.0014 348 B

with enabled runtime config

Method Mean Error StdDev Gen0 Gen1 Allocated
Apply 124.5 ns 0.74 ns 0.62 ns 0.0415 0.0020 348 B
Apply_Toggle 2,016.0 ns 27.18 ns 25.42 ns 0.0415 - 356 B
Apply_Detach 143.8 ns 2.75 ns 2.82 ns 0.0415 0.0014 348 B

with disabled runtime config (default)

Method Mean Error StdDev Gen0 Gen1 Allocated
Apply 105.7 ns 0.27 ns 0.24 ns 0.0415 0.0020 348 B
Apply_Toggle 1,083.7 ns 1.70 ns 1.42 ns 0.0415 0.0012 356 B
Apply_Detach 123.0 ns 0.45 ns 0.39 ns 0.0415 0.0014 348 B

@avaloniaui-bot
Copy link

You can test this PR using the following package version. 11.3.999-cibuild0055143-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@maxkatz6 maxkatz6 marked this pull request as ready for review March 5, 2025 14:20
public static void InitMetrics()
{
// Metrics
var meter = new Meter("Avalonia.Diagnostic.Meter");
Copy link
Member

@MrJul MrJul Mar 5, 2025

Choose a reason for hiding this comment

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

Shouldn't that be Avalonia.Diagnostics.Meter or Avalonia.Diagnostics.Diagnostic.Meter?

Copy link
Member Author

Choose a reason for hiding this comment

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

This name is public (for listeners, and also devtools UI), so I made it shorter. I don't have strong opinion though.

@maxkatz6 maxkatz6 added this pull request to the merge queue Mar 6, 2025
Merged via the queue into master with commit acd4653 Mar 6, 2025
10 checks passed
@maxkatz6 maxkatz6 deleted the dev-tools-profiling branch March 6, 2025 12:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use System.Diagnostic.Activity for event debugging
3 participants