Skip to content

Commit cde4a3b

Browse files
committed
Add benchmark mode for only foundatio
1 parent c61b695 commit cde4a3b

6 files changed

Lines changed: 155 additions & 26 deletions

File tree

.vscode/tasks.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,36 @@
4040
"${workspaceFolder}/.github/scripts/Update-BenchmarkDocs.ps1"
4141
],
4242
"problemMatcher": []
43+
},
44+
{
45+
"label": "run foundatio benchmarks",
46+
"type": "shell",
47+
"command": "dotnet",
48+
"args": [
49+
"run",
50+
"-c",
51+
"Release",
52+
"--",
53+
"foundatio"
54+
],
55+
"options": {
56+
"cwd": "${workspaceFolder}/benchmarks/Foundatio.Mediator.Benchmarks"
57+
},
58+
"problemMatcher": []
59+
},
60+
{
61+
"label": "run comparison benchmarks",
62+
"type": "shell",
63+
"command": "dotnet",
64+
"args": [
65+
"run",
66+
"-c",
67+
"Release"
68+
],
69+
"options": {
70+
"cwd": "${workspaceFolder}/benchmarks/Foundatio.Mediator.Benchmarks"
71+
},
72+
"problemMatcher": []
4373
}
4474
]
4575
}

AGENTS.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@ Foundatio.Mediator is a high-performance mediator library for .NET that achieves
1111

1212
**Key innovation**: Zero runtime reflection. All handler dispatch is resolved at compile time via source generators that scan for handlers and emit strongly-typed wrappers with optional C# 11+ interceptor attributes for direct call redirection.
1313

14-
## Commands you can use
14+
## Commands (ALWAYS use these)
1515

16-
Build project (triggers source generators): `dotnet build`
17-
Run tests (validate work): `dotnet test`
18-
Run benchmarks: `cd benchmarks/Foundatio.Mediator.Benchmarks; dotnet run -c Release`
19-
Run samples: `cd samples/ConsoleSample; dotnet run`
20-
Clean (removes source generated files): `dotnet clean`
16+
**IMPORTANT**: Always use these exact commands. Do not create ad-hoc scripts or alternative approaches.
17+
18+
- **Build** (triggers source generators): `dotnet build`
19+
- **Run tests** (validate ALL changes): `dotnet test`
20+
- **Run benchmarks** (measure performance): `cd benchmarks/Foundatio.Mediator.Benchmarks; dotnet run -c Release -- foundatio`
21+
- **Run samples**: `cd samples/ConsoleSample; dotnet run`
22+
- **Clean** (removes generated files): `dotnet clean`
23+
24+
**Workflow**: After making code changes, ALWAYS run `dotnet build` then `dotnet test` to validate your work before considering the task complete.
2125

2226
## Code Conventions
2327

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using BenchmarkDotNet.Attributes;
2+
using Foundatio.Mediator.Benchmarks.Messages;
3+
using Foundatio.Mediator.Benchmarks.Services;
4+
using Microsoft.Extensions.DependencyInjection;
5+
6+
namespace Foundatio.Mediator.Benchmarks;
7+
8+
/// <summary>
9+
/// Foundatio-only benchmarks for measuring library performance improvements.
10+
/// Use this mode when iterating on library performance optimizations.
11+
/// </summary>
12+
[MemoryDiagnoser]
13+
[SimpleJob]
14+
public class FoundatioBenchmarks
15+
{
16+
private IServiceProvider _foundatioServices = null!;
17+
private Foundatio.Mediator.IMediator _foundatioMediator = null!;
18+
19+
private readonly PingCommand _pingCommand = new("test-123");
20+
private readonly GetOrder _getOrder = new(42);
21+
private readonly GetOrderWithDependencies _getOrderWithDependencies = new(42);
22+
private readonly UserRegisteredEvent _userRegisteredEvent = new("User-456", "test@example.com");
23+
24+
[GlobalSetup]
25+
public void Setup()
26+
{
27+
var foundatioServices = new ServiceCollection();
28+
foundatioServices.AddSingleton<IOrderService, OrderService>();
29+
foundatioServices.AddMediator();
30+
_foundatioServices = foundatioServices.BuildServiceProvider();
31+
_foundatioMediator = _foundatioServices.GetRequiredService<Foundatio.Mediator.IMediator>();
32+
}
33+
34+
[GlobalCleanup]
35+
public void Cleanup()
36+
{
37+
(_foundatioServices as IDisposable)?.Dispose();
38+
}
39+
40+
[Benchmark]
41+
public async Task Command()
42+
{
43+
await _foundatioMediator.InvokeAsync(_pingCommand);
44+
}
45+
46+
[Benchmark]
47+
public async Task<Order> Query()
48+
{
49+
return await _foundatioMediator.InvokeAsync<Order>(_getOrder);
50+
}
51+
52+
[Benchmark]
53+
public async Task Publish()
54+
{
55+
await _foundatioMediator.PublishAsync(_userRegisteredEvent);
56+
}
57+
58+
[Benchmark]
59+
public async Task<Order> QueryWithDependencies()
60+
{
61+
return await _foundatioMediator.InvokeAsync<Order>(_getOrderWithDependencies);
62+
}
63+
}

benchmarks/Foundatio.Mediator.Benchmarks/Program.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,22 @@ class Program
66
{
77
static void Main(string[] args)
88
{
9-
Console.WriteLine("Running Foundatio.Mediator vs MediatR vs MassTransit benchmarks...");
10-
var summary = BenchmarkRunner.Run<CoreBenchmarks>();
9+
var mode = args.Length > 0 ? args[0].ToLowerInvariant() : "all";
10+
11+
switch (mode)
12+
{
13+
case "foundatio":
14+
case "f":
15+
Console.WriteLine("Running Foundatio.Mediator-only benchmarks (for performance iteration)...");
16+
BenchmarkRunner.Run<FoundatioBenchmarks>(args: args.Length > 1 ? args[1..] : []);
17+
break;
18+
19+
case "all":
20+
case "compare":
21+
default:
22+
Console.WriteLine("Running Foundatio.Mediator vs MediatR vs MassTransit benchmarks...");
23+
BenchmarkRunner.Run<CoreBenchmarks>(args: args.Length > 1 ? args[1..] : []);
24+
break;
25+
}
1126
}
1227
}
Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
# Foundatio.Mediator Benchmarks
22

3-
This project contains performance benchmarks comparing Foundatio.Mediator against MediatR.
3+
This project contains performance benchmarks comparing Foundatio.Mediator against MediatR and MassTransit.
44

55
## Running the Benchmarks
66

7-
To run the benchmarks:
7+
### Full Comparison (Default)
8+
9+
Run all benchmarks comparing Foundatio.Mediator vs MediatR vs MassTransit:
810

911
```bash
1012
cd benchmarks/Foundatio.Mediator.Benchmarks
1113
dotnet run -c Release
14+
# or explicitly:
15+
dotnet run -c Release -- all
16+
```
17+
18+
### Foundatio-Only Mode
19+
20+
Run only Foundatio.Mediator benchmarks for tracking performance across code changes:
21+
22+
```bash
23+
cd benchmarks/Foundatio.Mediator.Benchmarks
24+
dotnet run -c Release -- foundatio
25+
# or shorthand:
26+
dotnet run -c Release -- f
1227
```
28+
29+
This mode is faster to run and useful when iterating on library performance optimizations.

docs/guide/performance.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ Foundatio Mediator achieves near-direct call performance through C# interceptors
99
1010
| Method | Mean | Allocated |
1111
|:-------|-----:|----------:|
12-
| Direct_Command | 13.82 ns | 0 B |
13-
| Direct_Query | 57.69 ns | 192 B |
14-
| Direct_Event | 14.04 ns | 0 B |
15-
| Direct_QueryWithDependencies | 75.99 ns | 264 B |
16-
| Foundatio_Command | 158.02 ns | 200 B |
17-
| MediatR_Command | 108.61 ns | 128 B |
18-
| MassTransit_Command | 3,039.47 ns | 4168 B |
19-
| Foundatio_Query | 213.05 ns | 464 B |
20-
| MediatR_Query | 142.30 ns | 320 B |
21-
| MassTransit_Query | 11,881.17 ns | 12472 B |
22-
| Foundatio_Publish | 393.09 ns | 648 B |
23-
| MediatR_Publish | 124.28 ns | 288 B |
24-
| MassTransit_Publish | 3,223.86 ns | 4320 B |
25-
| Foundatio_QueryWithDependencies | 225.26 ns | 536 B |
26-
| MediatR_QueryWithDependencies | 159.80 ns | 392 B |
27-
| MassTransit_QueryWithDependencies | 12,399.85 ns | 12545 B |
12+
| Direct_Command | 7.143 ns | 0 B |
13+
| Direct_Query | 30.135 ns | 192 B |
14+
| Direct_Event | 5.898 ns | 0 B |
15+
| Direct_QueryWithDependencies | 34.721 ns | 264 B |
16+
| Foundatio_Command | 64.364 ns | 200 B |
17+
| MediatR_Command | 38.909 ns | 128 B |
18+
| MassTransit_Command | 1,249.631 ns | 4168 B |
19+
| Foundatio_Query | 95.543 ns | 464 B |
20+
| MediatR_Query | 63.362 ns | 320 B |
21+
| MassTransit_Query | 5,583.887 ns | 12472 B |
22+
| Foundatio_Publish | 144.341 ns | 648 B |
23+
| MediatR_Publish | 49.039 ns | 288 B |
24+
| MassTransit_Publish | 1,394.933 ns | 4320 B |
25+
| Foundatio_QueryWithDependencies | 105.046 ns | 536 B |
26+
| MediatR_QueryWithDependencies | 67.966 ns | 392 B |
27+
| MassTransit_QueryWithDependencies | 5,642.623 ns | 12544 B |
2828

2929
## Running Benchmarks Locally
3030

0 commit comments

Comments
 (0)