-
Notifications
You must be signed in to change notification settings - Fork 3
Test/Add comprehensive CQRS benchmarking suite with reflection and generated invoker paths #326
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
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
96729dd
test(cqrs): 补充基准与生成器回归基础设施
GeWuYou e6f98cb
test(cqrs): 补充流式请求基准场景
GeWuYou a8f98e4
test(cqrs): 补充请求管道数量矩阵基准
GeWuYou e0bbf13
test(cqrs): 补充请求启动阶段基准
GeWuYou 6e1eaf8
test(cqrs): 补充请求调用器生成路径基准
GeWuYou 449eeb9
feat(cqrs): 补齐 stream invoker 基准对照
GeWuYou 2ac02c1
fix(cqrs): 收敛 benchmark review 修复
GeWuYou f71791a
ci(cqrs): 新增手动 benchmark 工作流
GeWuYou 2cb6216
fix(cqrs): 修复 benchmark 对照宿主与冷启动基线
GeWuYou 6d619b9
fix(cqrs): 收敛 benchmark review 收尾问题
GeWuYou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // Copyright (c) 2025-2026 GeWuYou | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using BenchmarkDotNet.Columns; | ||
| using BenchmarkDotNet.Reports; | ||
| using BenchmarkDotNet.Running; | ||
| using System; | ||
|
|
||
| namespace GFramework.Cqrs.Benchmarks; | ||
|
|
||
| /// <summary> | ||
| /// 为 CQRS benchmark 结果补充可读的场景标签列。 | ||
| /// </summary> | ||
| /// <param name="columnName">列名。</param> | ||
| /// <param name="getValue">从 benchmark case 提取列值的委托。</param> | ||
| public sealed class CustomColumn(string columnName, Func<Summary, BenchmarkCase, string> getValue) : IColumn | ||
| { | ||
| /// <inheritdoc /> | ||
| public string Id => $"{nameof(CustomColumn)}.{ColumnName}"; | ||
|
|
||
| /// <inheritdoc /> | ||
| public string ColumnName { get; } = columnName; | ||
|
|
||
| /// <inheritdoc /> | ||
| public bool AlwaysShow => true; | ||
|
|
||
| /// <inheritdoc /> | ||
| public ColumnCategory Category => ColumnCategory.Params; | ||
|
|
||
| /// <inheritdoc /> | ||
| public int PriorityInCategory => 0; | ||
|
|
||
| /// <inheritdoc /> | ||
| public bool IsNumeric => false; | ||
|
|
||
| /// <inheritdoc /> | ||
| public UnitType UnitType => UnitType.Dimensionless; | ||
|
|
||
| /// <inheritdoc /> | ||
| public string Legend => $"Custom '{ColumnName}' tag column"; | ||
|
|
||
| /// <inheritdoc /> | ||
| public bool IsDefault(Summary summary, BenchmarkCase benchmarkCase) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public bool IsAvailable(Summary summary) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public string GetValue(Summary summary, BenchmarkCase benchmarkCase) | ||
| { | ||
| return getValue(summary, benchmarkCase); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public string GetValue(Summary summary, BenchmarkCase benchmarkCase, SummaryStyle style) | ||
| { | ||
| return GetValue(summary, benchmarkCase); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public override string ToString() | ||
| { | ||
| return ColumnName; | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
GFramework.Cqrs.Benchmarks/GFramework.Cqrs.Benchmarks.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| <!-- | ||
| Copyright (c) 2025-2026 GeWuYou | ||
| SPDX-License-Identifier: Apache-2.0 | ||
| --> | ||
|
|
||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net10.0</TargetFramework> | ||
| <ImplicitUsings>disable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <GenerateDocumentationFile>false</GenerateDocumentationFile> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="BenchmarkDotNet" Version="0.15.6" /> | ||
| <PackageReference Include="MediatR" Version="13.1.0" /> | ||
| <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.7" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\GFramework.Cqrs.Abstractions\GFramework.Cqrs.Abstractions.csproj" /> | ||
| <ProjectReference Include="..\GFramework.Cqrs\GFramework.Cqrs.csproj" /> | ||
| <ProjectReference Include="..\GFramework.Core.Abstractions\GFramework.Core.Abstractions.csproj" /> | ||
| <ProjectReference Include="..\GFramework.Core\GFramework.Core.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // Copyright (c) 2025-2026 GeWuYou | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using GFramework.Cqrs.Abstractions.Cqrs; | ||
|
|
||
| namespace GFramework.Cqrs.Benchmarks.Messaging; | ||
|
|
||
| /// <summary> | ||
| /// 为纯 runtime benchmark 提供最小 CQRS 上下文标记,避免把完整架构上下文初始化成本混入 steady-state dispatch。 | ||
| /// </summary> | ||
| internal sealed class BenchmarkContext : ICqrsContext | ||
| { | ||
| /// <summary> | ||
| /// 共享的最小 CQRS 上下文实例。 | ||
| /// </summary> | ||
| public static BenchmarkContext Instance { get; } = new(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // Copyright (c) 2025-2026 GeWuYou | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using BenchmarkDotNet.Loggers; | ||
| using System; | ||
|
|
||
| namespace GFramework.Cqrs.Benchmarks.Messaging; | ||
|
|
||
| /// <summary> | ||
| /// 为 CQRS benchmark 运行打印并验证当前场景配置,避免矩阵配置与实际运行环境漂移。 | ||
| /// </summary> | ||
| internal static class Fixture | ||
| { | ||
| /// <summary> | ||
| /// 输出当前 benchmark 配置并验证关键环境变量。 | ||
| /// </summary> | ||
| /// <param name="scenario">当前 benchmark 场景名称。</param> | ||
| /// <param name="handlerCount">当前场景的处理器数量。</param> | ||
| /// <param name="pipelineCount">当前场景的 pipeline 行为数量。</param> | ||
| public static void Setup(string scenario, int handlerCount, int pipelineCount) | ||
| { | ||
| ConsoleLogger.Default.WriteLineHeader("GFramework.Cqrs benchmark config"); | ||
| ConsoleLogger.Default.WriteLineInfo($"Scenario = {scenario}"); | ||
| ConsoleLogger.Default.WriteLineInfo($"HandlerCount = {handlerCount}"); | ||
| ConsoleLogger.Default.WriteLineInfo($"PipelineCount = {pipelineCount}"); | ||
|
|
||
| var environmentScenario = Environment.GetEnvironmentVariable("GFRAMEWORK_CQRS_BENCHMARK_SCENARIO"); | ||
| if (!string.IsNullOrWhiteSpace(environmentScenario) && | ||
| !string.Equals(environmentScenario, scenario, StringComparison.Ordinal)) | ||
| { | ||
| throw new InvalidOperationException( | ||
| $"Scenario mismatch. Expected '{environmentScenario}', actual '{scenario}'."); | ||
| } | ||
| } | ||
| } |
98 changes: 98 additions & 0 deletions
98
GFramework.Cqrs.Benchmarks/Messaging/GeneratedRequestInvokerBenchmarkRegistry.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| // Copyright (c) 2025-2026 GeWuYou | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Reflection; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using GFramework.Core.Abstractions.Logging; | ||
| using GFramework.Cqrs.Abstractions.Cqrs; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| namespace GFramework.Cqrs.Benchmarks.Messaging; | ||
|
|
||
| /// <summary> | ||
| /// 为 benchmark 手写一个“生成后等价物” registry,用于驱动真实的 generated invoker provider 运行时接线路径。 | ||
| /// </summary> | ||
| public sealed class GeneratedRequestInvokerBenchmarkRegistry : | ||
| GFramework.Cqrs.ICqrsHandlerRegistry, | ||
| GFramework.Cqrs.ICqrsRequestInvokerProvider, | ||
| GFramework.Cqrs.IEnumeratesCqrsRequestInvokerDescriptors | ||
| { | ||
| private static readonly GFramework.Cqrs.CqrsRequestInvokerDescriptor Descriptor = | ||
| new( | ||
| typeof(IRequestHandler< | ||
| RequestInvokerBenchmarks.GeneratedBenchmarkRequest, | ||
| RequestInvokerBenchmarks.GeneratedBenchmarkResponse>), | ||
| typeof(GeneratedRequestInvokerBenchmarkRegistry).GetMethod( | ||
| nameof(InvokeGeneratedRequestHandler), | ||
| BindingFlags.Public | BindingFlags.Static) | ||
| ?? throw new InvalidOperationException("Missing generated request invoker benchmark method.")); | ||
|
|
||
| private static readonly IReadOnlyList<GFramework.Cqrs.CqrsRequestInvokerDescriptorEntry> Descriptors = | ||
| [ | ||
| new GFramework.Cqrs.CqrsRequestInvokerDescriptorEntry( | ||
| typeof(RequestInvokerBenchmarks.GeneratedBenchmarkRequest), | ||
| typeof(RequestInvokerBenchmarks.GeneratedBenchmarkResponse), | ||
| Descriptor) | ||
| ]; | ||
|
|
||
| /// <summary> | ||
| /// 将 generated benchmark request handler 注册到目标服务集合。 | ||
| /// </summary> | ||
| public void Register(IServiceCollection services, ILogger logger) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(services); | ||
| ArgumentNullException.ThrowIfNull(logger); | ||
|
|
||
| services.AddTransient( | ||
| typeof(IRequestHandler< | ||
| RequestInvokerBenchmarks.GeneratedBenchmarkRequest, | ||
| RequestInvokerBenchmarks.GeneratedBenchmarkResponse>), | ||
| typeof(RequestInvokerBenchmarks.GeneratedBenchmarkRequestHandler)); | ||
| logger.Debug("Registered generated request invoker benchmark handler."); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 返回当前 provider 暴露的全部 generated request invoker 描述符。 | ||
| /// </summary> | ||
| public IReadOnlyList<GFramework.Cqrs.CqrsRequestInvokerDescriptorEntry> GetDescriptors() | ||
| { | ||
| return Descriptors; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 为目标请求/响应类型对返回 generated request invoker 描述符。 | ||
| /// </summary> | ||
| public bool TryGetDescriptor( | ||
| Type requestType, | ||
| Type responseType, | ||
| out GFramework.Cqrs.CqrsRequestInvokerDescriptor? descriptor) | ||
| { | ||
| if (requestType == typeof(RequestInvokerBenchmarks.GeneratedBenchmarkRequest) && | ||
| responseType == typeof(RequestInvokerBenchmarks.GeneratedBenchmarkResponse)) | ||
| { | ||
| descriptor = Descriptor; | ||
| return true; | ||
| } | ||
|
|
||
| descriptor = null; | ||
| return false; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 模拟 generated invoker provider 产出的开放静态调用入口。 | ||
| /// </summary> | ||
| public static ValueTask<RequestInvokerBenchmarks.GeneratedBenchmarkResponse> InvokeGeneratedRequestHandler( | ||
| object handler, | ||
| object request, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| var typedHandler = (IRequestHandler< | ||
| RequestInvokerBenchmarks.GeneratedBenchmarkRequest, | ||
| RequestInvokerBenchmarks.GeneratedBenchmarkResponse>)handler; | ||
| var typedRequest = (RequestInvokerBenchmarks.GeneratedBenchmarkRequest)request; | ||
| return typedHandler.Handle(typedRequest, cancellationToken); | ||
| } | ||
| } |
94 changes: 94 additions & 0 deletions
94
GFramework.Cqrs.Benchmarks/Messaging/GeneratedStreamInvokerBenchmarkRegistry.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| // Copyright (c) 2025-2026 GeWuYou | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Reflection; | ||
| using System.Threading; | ||
| using GFramework.Core.Abstractions.Logging; | ||
| using GFramework.Cqrs.Abstractions.Cqrs; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| namespace GFramework.Cqrs.Benchmarks.Messaging; | ||
|
|
||
| /// <summary> | ||
| /// 为 benchmark 手写一个“生成后等价物” stream registry,用于驱动真实的 generated stream invoker provider 运行时接线路径。 | ||
| /// </summary> | ||
| public sealed class GeneratedStreamInvokerBenchmarkRegistry : | ||
| GFramework.Cqrs.ICqrsHandlerRegistry, | ||
| GFramework.Cqrs.ICqrsStreamInvokerProvider, | ||
| GFramework.Cqrs.IEnumeratesCqrsStreamInvokerDescriptors | ||
| { | ||
| private static readonly GFramework.Cqrs.CqrsStreamInvokerDescriptor Descriptor = | ||
| new( | ||
| typeof(IStreamRequestHandler< | ||
| StreamInvokerBenchmarks.GeneratedBenchmarkStreamRequest, | ||
| StreamInvokerBenchmarks.GeneratedBenchmarkResponse>), | ||
| typeof(GeneratedStreamInvokerBenchmarkRegistry).GetMethod( | ||
| nameof(InvokeGeneratedStreamHandler), | ||
| BindingFlags.Public | BindingFlags.Static) | ||
| ?? throw new InvalidOperationException("Missing generated stream invoker benchmark method.")); | ||
|
|
||
| private static readonly IReadOnlyList<GFramework.Cqrs.CqrsStreamInvokerDescriptorEntry> Descriptors = | ||
| [ | ||
| new GFramework.Cqrs.CqrsStreamInvokerDescriptorEntry( | ||
| typeof(StreamInvokerBenchmarks.GeneratedBenchmarkStreamRequest), | ||
| typeof(StreamInvokerBenchmarks.GeneratedBenchmarkResponse), | ||
| Descriptor) | ||
| ]; | ||
|
|
||
| /// <summary> | ||
| /// 将 generated benchmark stream handler 注册到目标服务集合。 | ||
| /// </summary> | ||
| public void Register(IServiceCollection services, ILogger logger) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(services); | ||
| ArgumentNullException.ThrowIfNull(logger); | ||
|
|
||
| services.AddTransient( | ||
| typeof(IStreamRequestHandler< | ||
| StreamInvokerBenchmarks.GeneratedBenchmarkStreamRequest, | ||
| StreamInvokerBenchmarks.GeneratedBenchmarkResponse>), | ||
| typeof(StreamInvokerBenchmarks.GeneratedBenchmarkStreamHandler)); | ||
| logger.Debug("Registered generated stream invoker benchmark handler."); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 返回当前 provider 暴露的全部 generated stream invoker 描述符。 | ||
| /// </summary> | ||
| public IReadOnlyList<GFramework.Cqrs.CqrsStreamInvokerDescriptorEntry> GetDescriptors() | ||
| { | ||
| return Descriptors; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 为目标流式请求/响应类型对返回 generated stream invoker 描述符。 | ||
| /// </summary> | ||
| public bool TryGetDescriptor( | ||
| Type requestType, | ||
| Type responseType, | ||
| out GFramework.Cqrs.CqrsStreamInvokerDescriptor? descriptor) | ||
| { | ||
| if (requestType == typeof(StreamInvokerBenchmarks.GeneratedBenchmarkStreamRequest) && | ||
| responseType == typeof(StreamInvokerBenchmarks.GeneratedBenchmarkResponse)) | ||
| { | ||
| descriptor = Descriptor; | ||
| return true; | ||
| } | ||
|
|
||
| descriptor = null; | ||
| return false; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 模拟 generated stream invoker provider 产出的开放静态调用入口。 | ||
| /// </summary> | ||
| public static object InvokeGeneratedStreamHandler(object handler, object request, CancellationToken cancellationToken) | ||
| { | ||
| var typedHandler = (IStreamRequestHandler< | ||
| StreamInvokerBenchmarks.GeneratedBenchmarkStreamRequest, | ||
| StreamInvokerBenchmarks.GeneratedBenchmarkResponse>)handler; | ||
| var typedRequest = (StreamInvokerBenchmarks.GeneratedBenchmarkStreamRequest)request; | ||
| return typedHandler.Handle(typedRequest, cancellationToken); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.