Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
```

BenchmarkDotNet v0.13.12, macOS 15.4.1 (24E263) [Darwin 24.4.0]
Apple M2 Max, 1 CPU, 12 logical and 12 physical cores
.NET SDK 9.0.203
[Host] : .NET 9.0.4 (9.0.425.16305), Arm64 RyuJIT AdvSIMD
DefaultJob : .NET 9.0.4 (9.0.425.16305), Arm64 RyuJIT AdvSIMD


```
| Method | ResolveOptionsWithServiceProvider | Mean | Error | StdDev | Median | Gen0 | Gen1 | Allocated |
|----------------- |---------------------------------- |---------:|---------:|---------:|---------:|--------:|-------:|----------:|
| **'Build MAUI App'** | **Directly** | **494.3 μs** | **19.62 μs** | **53.38 μs** | **476.0 μs** | **12.6953** | **2.9297** | **105.13 KB** |
| **'Build MAUI App'** | **ServiceProvider** | **488.1 μs** | **8.56 μs** | **12.55 μs** | **486.6 μs** | **15.6250** | **3.9063** | **129.52 KB** |
| **'Build MAUI App'** | **InvokeConfigOptions** | **499.5 μs** | **9.93 μs** | **22.21 μs** | **501.9 μs** | **12.6953** | **3.9063** | **110.23 KB** |
24 changes: 24 additions & 0 deletions benchmarks/Sentry.Benchmarks/MvvmBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using BenchmarkDotNet.Attributes;
using Sentry;
using Sentry.Maui;

public class MvvmBenchmarks
{
[Params(RegisterEventBinderMethod.ServiceProvider, RegisterEventBinderMethod.InvokeConfigOptions, RegisterEventBinderMethod.Directly)]
public RegisterEventBinderMethod ResolveOptionsWithServiceProvider;

[Benchmark(Description = "Build MAUI App")]
public void BuildMauiAppBenchmark()
{
var appBuilder = MauiApp.CreateBuilder()
// This adds Sentry to your Maui application
.UseSentry(options =>
{
// The DSN is the only required option.
options.Dsn = DsnSamples.ValidDsn;
// Automatically create traces for async relay commands in the MVVM Community Toolkit
options.AddCommunityToolkitIntegration();
}, ResolveOptionsWithServiceProvider);
appBuilder.Build();
}
}
5 changes: 4 additions & 1 deletion benchmarks/Sentry.Benchmarks/Sentry.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<IsPackable>false</IsPackable>
<UseMaui>true</UseMaui>
</PropertyGroup>

<ItemGroup>
Expand All @@ -13,6 +14,8 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Sentry.Maui.CommunityToolkit.Mvvm\Sentry.Maui.CommunityToolkit.Mvvm.csproj" />
<ProjectReference Include="..\..\src\Sentry.Maui\Sentry.Maui.csproj" />
<ProjectReference Include="..\..\src\Sentry\Sentry.csproj" />
<ProjectReference Include="..\..\src\Sentry.Profiling\Sentry.Profiling.csproj" />
<ProjectReference Include="..\..\test\Sentry.Testing\Sentry.Testing.csproj" />
Expand Down
61 changes: 55 additions & 6 deletions src/Sentry.Maui/SentryMauiAppBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@
// ReSharper disable once CheckNamespace
namespace Microsoft.Maui.Hosting;

/// <summary>
/// An enum
/// </summary>
public enum RegisterEventBinderMethod
{
/// <summary>
/// Registers the services directly... unable to inject the MvvM integration this way
/// </summary>
Directly,
/// <summary>
/// Register with the service provider
/// </summary>
ServiceProvider,
/// <summary>
/// Instantiate the options directly and invoke the config callback on it
/// </summary>
InvokeConfigOptions
}

/// <summary>
/// Sentry extensions for <see cref="MauiAppBuilder"/>.
/// </summary>
Expand Down Expand Up @@ -39,9 +58,10 @@ public static MauiAppBuilder UseSentry(this MauiAppBuilder builder, string dsn)
/// </summary>
/// <param name="builder">The builder.</param>
/// <param name="configureOptions">An action to configure the options.</param>
/// <param name="eventBinderRegistrationMethod"></param>
/// <returns>The <paramref name="builder"/>.</returns>
public static MauiAppBuilder UseSentry(this MauiAppBuilder builder,
Action<SentryMauiOptions>? configureOptions)
Action<SentryMauiOptions>? configureOptions, RegisterEventBinderMethod eventBinderRegistrationMethod = RegisterEventBinderMethod.ServiceProvider)
{
var services = builder.Services;

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

// Resolve the configured options and register any element event binders from these
IServiceProvider serviceProvider = services.BuildServiceProvider();
var options = serviceProvider.GetRequiredService<IOptions<SentryMauiOptions>>().Value;
services.TryAddSingleton<SentryOptions>(options); // Ensure this doesn't get resolved again in AddSentry
foreach (var eventBinder in options.DefaultEventBinders)
switch (eventBinderRegistrationMethod)
{
eventBinder.Register(services);
case RegisterEventBinderMethod.Directly:
services.AddSingleton<IMauiElementEventBinder, MauiButtonEventsBinder>();
services.AddSingleton<IMauiElementEventBinder, MauiImageButtonEventsBinder>();
services.AddSingleton<IMauiElementEventBinder, MauiGestureRecognizerEventsBinder>();
services.AddSingleton<IMauiElementEventBinder, MauiVisualElementEventsBinder>();
break;
case RegisterEventBinderMethod.InvokeConfigOptions:
var options = new SentryMauiOptions();
configureOptions?.Invoke(options);
services.TryAddSingleton<SentryOptions>(options); // Ensure this doesn't get resolved again in AddSentry
foreach (var eventBinder in options.DefaultEventBinders)
{
eventBinder.Register(services);
}
break;
case RegisterEventBinderMethod.ServiceProvider:
IServiceProvider serviceProvider = services.BuildServiceProvider();
options = serviceProvider.GetRequiredService<IOptions<SentryMauiOptions>>().Value;
services.TryAddSingleton<SentryOptions>(options); // Ensure this doesn't get resolved again in AddSentry
foreach (var eventBinder in options.DefaultEventBinders)
{
eventBinder.Register(services);
}
break;
}

// // Resolve the configured options and register any element event binders from these
// IServiceProvider serviceProvider = services.BuildServiceProvider();
// var options = serviceProvider.GetRequiredService<IOptions<SentryMauiOptions>>().Value;
// services.TryAddSingleton<SentryOptions>(options); // Ensure this doesn't get resolved again in AddSentry
// foreach (var eventBinder in options.DefaultEventBinders)
// {
// eventBinder.Register(services);
// }

// This is ultimately the class that enables all the MauiElementEventBinders above
services.TryAddSingleton<IMauiEventsBinder, MauiEventsBinder>();

Expand Down
Loading