Skip to content
Merged
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
41 changes: 34 additions & 7 deletions GFramework.Cqrs.Benchmarks/Messaging/NotificationBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using GFramework.Cqrs.Abstractions.Cqrs;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using GeneratedMediator = Mediator.Mediator;

namespace GFramework.Cqrs.Benchmarks.Messaging;

Expand All @@ -27,8 +28,10 @@ public class NotificationBenchmarks
{
private MicrosoftDiContainer _container = null!;
private ICqrsRuntime _runtime = null!;
private ServiceProvider _serviceProvider = null!;
private IPublisher _publisher = null!;
private ServiceProvider _mediatrServiceProvider = null!;
private ServiceProvider _mediatorServiceProvider = null!;
private IPublisher _mediatrPublisher = null!;
private GeneratedMediator _mediator = null!;
private BenchmarkNotification _notification = null!;

/// <summary>
Expand Down Expand Up @@ -67,23 +70,26 @@ public void Setup()
_container,
LoggerFactoryResolver.Provider.CreateLogger(nameof(NotificationBenchmarks)));

_serviceProvider = BenchmarkHostFactory.CreateMediatRServiceProvider(
_mediatrServiceProvider = BenchmarkHostFactory.CreateMediatRServiceProvider(
services => services.AddSingleton<MediatR.INotificationHandler<BenchmarkNotification>, BenchmarkNotificationHandler>(),
typeof(NotificationBenchmarks),
static candidateType => candidateType == typeof(BenchmarkNotificationHandler),
ServiceLifetime.Singleton);
_publisher = _serviceProvider.GetRequiredService<IPublisher>();
_mediatrPublisher = _mediatrServiceProvider.GetRequiredService<IPublisher>();

_mediatorServiceProvider = BenchmarkHostFactory.CreateMediatorServiceProvider(configure: null);
_mediator = _mediatorServiceProvider.GetRequiredService<GeneratedMediator>();

_notification = new BenchmarkNotification(Guid.NewGuid());
}

/// <summary>
/// 释放 MediatR 对照组使用的 DI 宿主。
/// 释放 MediatR 与 `Mediator` 对照组使用的 DI 宿主。
/// </summary>
[GlobalCleanup]
public void Cleanup()
{
BenchmarkCleanupHelper.DisposeAll(_container, _serviceProvider);
BenchmarkCleanupHelper.DisposeAll(_container, _mediatrServiceProvider, _mediatorServiceProvider);
}

/// <summary>
Expand All @@ -101,7 +107,16 @@ public ValueTask PublishNotification_GFrameworkCqrs()
[Benchmark]
public Task PublishNotification_MediatR()
{
return _publisher.Publish(_notification, CancellationToken.None);
return _mediatrPublisher.Publish(_notification, CancellationToken.None);
}

/// <summary>
/// 通过 `Mediator` source-generated concrete mediator 发布 notification,作为高性能对照组。
/// </summary>
[Benchmark]
public ValueTask PublishNotification_Mediator()
{
return _mediator.Publish(_notification, CancellationToken.None);
}

/// <summary>
Expand All @@ -110,13 +125,15 @@ public Task PublishNotification_MediatR()
/// <param name="Id">通知标识。</param>
public sealed record BenchmarkNotification(Guid Id) :
GFramework.Cqrs.Abstractions.Cqrs.INotification,
Mediator.INotification,
MediatR.INotification;

/// <summary>
/// 同时实现 GFramework.CQRS 与 MediatR 契约的最小 notification handler。
/// </summary>
public sealed class BenchmarkNotificationHandler :
GFramework.Cqrs.Abstractions.Cqrs.INotificationHandler<BenchmarkNotification>,
Mediator.INotificationHandler<BenchmarkNotification>,
MediatR.INotificationHandler<BenchmarkNotification>
{
/// <summary>
Expand All @@ -127,6 +144,16 @@ public ValueTask Handle(BenchmarkNotification notification, CancellationToken ca
return ValueTask.CompletedTask;
}

/// <summary>
/// 处理 NuGet `Mediator` notification。
/// </summary>
ValueTask Mediator.INotificationHandler<BenchmarkNotification>.Handle(
BenchmarkNotification notification,
CancellationToken cancellationToken)
{
return Handle(notification, cancellationToken);
}

/// <summary>
/// 处理 MediatR notification。
/// </summary>
Expand Down
Loading
Loading