Skip to content

Commit 552e89b

Browse files
Benchmarks illustrating performance of different initialisation techniques
1 parent 6845e75 commit 552e89b

File tree

4 files changed

+105
-7
lines changed

4 files changed

+105
-7
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
```
2+
3+
BenchmarkDotNet v0.13.12, macOS 15.4.1 (24E263) [Darwin 24.4.0]
4+
Apple M2 Max, 1 CPU, 12 logical and 12 physical cores
5+
.NET SDK 9.0.203
6+
[Host] : .NET 9.0.4 (9.0.425.16305), Arm64 RyuJIT AdvSIMD
7+
DefaultJob : .NET 9.0.4 (9.0.425.16305), Arm64 RyuJIT AdvSIMD
8+
9+
10+
```
11+
| Method | ResolveOptionsWithServiceProvider | Mean | Error | StdDev | Gen0 | Gen1 | Allocated |
12+
|----------------- |---------------------------------- |---------:|--------:|---------:|--------:|-------:|----------:|
13+
| **'Build MAUI App'** | **Directly** | **470.8 μs** | **9.02 μs** | **9.65 μs** | **11.7188** | **2.9297** | **99.25 KB** |
14+
| **'Build MAUI App'** | **ServiceProvider** | **473.6 μs** | **8.73 μs** | **13.85 μs** | **11.7188** | **2.9297** | **98.66 KB** |
15+
| **'Build MAUI App'** | **InvokeConfigOptions** | **462.0 μs** | **8.84 μs** | **10.18 μs** | **11.7188** | **2.9297** | **98.74 KB** |
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using BenchmarkDotNet.Attributes;
2+
using Sentry;
3+
using Sentry.Maui;
4+
5+
public class MvvmBenchmarks
6+
{
7+
private MauiAppBuilder AppBuilder;
8+
9+
[Params(RegisterEventBinderMethod.ServiceProvider, RegisterEventBinderMethod.InvokeConfigOptions, RegisterEventBinderMethod.Directly)]
10+
public RegisterEventBinderMethod ResolveOptionsWithServiceProvider;
11+
12+
[GlobalSetup]
13+
public void Setup()
14+
{
15+
AppBuilder = MauiApp.CreateBuilder()
16+
// This adds Sentry to your Maui application
17+
.UseSentry(options =>
18+
{
19+
// The DSN is the only required option.
20+
options.Dsn = DsnSamples.ValidDsn;
21+
// Automatically create traces for async relay commands in the MVVM Community Toolkit
22+
options.AddCommunityToolkitIntegration();
23+
}, ResolveOptionsWithServiceProvider);
24+
}
25+
26+
[Benchmark(Description = "Build MAUI App")]
27+
public void BuildMauiAppBenchmark()
28+
{
29+
AppBuilder.Build();
30+
}
31+
}

benchmarks/Sentry.Benchmarks/Sentry.Benchmarks.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net8.0</TargetFramework>
5+
<TargetFramework>net9.0</TargetFramework>
66
<IsPackable>false</IsPackable>
7+
<UseMaui>true</UseMaui>
78
</PropertyGroup>
89

910
<ItemGroup>
@@ -13,6 +14,8 @@
1314
</ItemGroup>
1415

1516
<ItemGroup>
17+
<ProjectReference Include="..\..\src\Sentry.Maui.CommunityToolkit.Mvvm\Sentry.Maui.CommunityToolkit.Mvvm.csproj" />
18+
<ProjectReference Include="..\..\src\Sentry.Maui\Sentry.Maui.csproj" />
1619
<ProjectReference Include="..\..\src\Sentry\Sentry.csproj" />
1720
<ProjectReference Include="..\..\src\Sentry.Profiling\Sentry.Profiling.csproj" />
1821
<ProjectReference Include="..\..\test\Sentry.Testing\Sentry.Testing.csproj" />

src/Sentry.Maui/SentryMauiAppBuilderExtensions.cs

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,25 @@
1111
// ReSharper disable once CheckNamespace
1212
namespace Microsoft.Maui.Hosting;
1313

14+
/// <summary>
15+
/// An enum
16+
/// </summary>
17+
public enum RegisterEventBinderMethod
18+
{
19+
/// <summary>
20+
/// Registers the services directly... unable to inject the MvvM integration this way
21+
/// </summary>
22+
Directly,
23+
/// <summary>
24+
/// Register with the service provider
25+
/// </summary>
26+
ServiceProvider,
27+
/// <summary>
28+
/// Instantiate the options directly and invoke the config callback on it
29+
/// </summary>
30+
InvokeConfigOptions
31+
}
32+
1433
/// <summary>
1534
/// Sentry extensions for <see cref="MauiAppBuilder"/>.
1635
/// </summary>
@@ -39,9 +58,10 @@ public static MauiAppBuilder UseSentry(this MauiAppBuilder builder, string dsn)
3958
/// </summary>
4059
/// <param name="builder">The builder.</param>
4160
/// <param name="configureOptions">An action to configure the options.</param>
61+
/// <param name="eventBinderRegistrationMethod"></param>
4262
/// <returns>The <paramref name="builder"/>.</returns>
4363
public static MauiAppBuilder UseSentry(this MauiAppBuilder builder,
44-
Action<SentryMauiOptions>? configureOptions)
64+
Action<SentryMauiOptions>? configureOptions, RegisterEventBinderMethod eventBinderRegistrationMethod = RegisterEventBinderMethod.ServiceProvider)
4565
{
4666
var services = builder.Services;
4767

@@ -57,14 +77,43 @@ public static MauiAppBuilder UseSentry(this MauiAppBuilder builder,
5777
services.AddSingleton<Disposer>();
5878

5979
// Resolve the configured options and register any element event binders from these
60-
IServiceProvider serviceProvider = services.BuildServiceProvider();
61-
var options = serviceProvider.GetRequiredService<IOptions<SentryMauiOptions>>().Value;
62-
services.TryAddSingleton<SentryOptions>(options); // Ensure this doesn't get resolved again in AddSentry
63-
foreach (var eventBinder in options.DefaultEventBinders)
80+
switch (eventBinderRegistrationMethod)
6481
{
65-
eventBinder.Register(services);
82+
case RegisterEventBinderMethod.Directly:
83+
services.AddSingleton<IMauiElementEventBinder, MauiButtonEventsBinder>();
84+
services.AddSingleton<IMauiElementEventBinder, MauiImageButtonEventsBinder>();
85+
services.AddSingleton<IMauiElementEventBinder, MauiGestureRecognizerEventsBinder>();
86+
services.AddSingleton<IMauiElementEventBinder, MauiVisualElementEventsBinder>();
87+
break;
88+
case RegisterEventBinderMethod.InvokeConfigOptions:
89+
var options = new SentryMauiOptions();
90+
configureOptions?.Invoke(options);
91+
services.TryAddSingleton<SentryOptions>(options); // Ensure this doesn't get resolved again in AddSentry
92+
foreach (var eventBinder in options.DefaultEventBinders)
93+
{
94+
eventBinder.Register(services);
95+
}
96+
break;
97+
case RegisterEventBinderMethod.ServiceProvider:
98+
IServiceProvider serviceProvider = services.BuildServiceProvider();
99+
options = serviceProvider.GetRequiredService<IOptions<SentryMauiOptions>>().Value;
100+
services.TryAddSingleton<SentryOptions>(options); // Ensure this doesn't get resolved again in AddSentry
101+
foreach (var eventBinder in options.DefaultEventBinders)
102+
{
103+
eventBinder.Register(services);
104+
}
105+
break;
66106
}
67107

108+
// // Resolve the configured options and register any element event binders from these
109+
// IServiceProvider serviceProvider = services.BuildServiceProvider();
110+
// var options = serviceProvider.GetRequiredService<IOptions<SentryMauiOptions>>().Value;
111+
// services.TryAddSingleton<SentryOptions>(options); // Ensure this doesn't get resolved again in AddSentry
112+
// foreach (var eventBinder in options.DefaultEventBinders)
113+
// {
114+
// eventBinder.Register(services);
115+
// }
116+
68117
// This is ultimately the class that enables all the MauiElementEventBinders above
69118
services.TryAddSingleton<IMauiEventsBinder, MauiEventsBinder>();
70119

0 commit comments

Comments
 (0)