-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3836e07
commit 879573d
Showing
5 changed files
with
182 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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,139 @@ | ||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Diagnosers; | ||
using BenchmarkDotNet.Jobs; | ||
using Fluss; | ||
using Fluss.Authentication; | ||
using Fluss.Events; | ||
using Fluss.ReadModel; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Benchmark; | ||
|
||
[SimpleJob(baseline: true)] | ||
[SimpleJob(RuntimeMoniker.Net90)] | ||
[MemoryDiagnoser] | ||
public class Bench | ||
{ | ||
[IterationSetup] | ||
public void Setup() | ||
{ | ||
var sc = new ServiceCollection(); | ||
sc.AddEventSourcing(); | ||
sc.AddPoliciesFrom(typeof(Bench).Assembly); | ||
|
||
_sp = sc.BuildServiceProvider(); | ||
} | ||
|
||
ServiceProvider _sp = null!; | ||
|
||
[Benchmark] | ||
public async Task<int> PublishEventsAndReadMixedReadWrite() | ||
{ | ||
var sum = 0; | ||
for (var j = 0; j < 1000; j++) | ||
{ | ||
await _sp.GetSystemUserUnitOfWorkFactory().Commit(async unitOfWork => { | ||
for (var i = 0; i < 50; i++) | ||
{ | ||
await unitOfWork.Publish(new TestEvent(1)); | ||
await unitOfWork.Publish(new TestEvent(2)); | ||
} | ||
}); | ||
|
||
var unitOfWork = _sp.GetSystemUserUnitOfWork(); | ||
var readModel1 = await unitOfWork.GetReadModel<EventsEqualReadModel, int>(1); | ||
var readModel2 = await unitOfWork.GetReadModel<EventsEqualReadModel, int>(2); | ||
sum += readModel1.GotEvents + readModel2.GotEvents; | ||
} | ||
|
||
return sum; | ||
} | ||
|
||
[IterationSetup(Targets = [nameof(PublishEventsAndReadReadHeavySingleReadModel), nameof(PublishEventsAndReadReadHeavyMultipleReadModel)])] | ||
public void SetupHeavyRead() | ||
{ | ||
var sc = new ServiceCollection(); | ||
sc.AddEventSourcing(); | ||
sc.AddPoliciesFrom(typeof(Bench).Assembly); | ||
|
||
_sp = sc.BuildServiceProvider(); | ||
|
||
_sp.GetSystemUserUnitOfWorkFactory().Commit(async unitOfWork => { | ||
for (var i = 0; i < 10000; i++) | ||
{ | ||
await unitOfWork.Publish(new TestEvent(i)); | ||
} | ||
}).AsTask().Wait(); | ||
} | ||
|
||
|
||
[Benchmark] | ||
public async Task<int> PublishEventsAndReadReadHeavySingleReadModel() | ||
{ | ||
var sum = 0; | ||
for (var j = 0; j < 50000; j++) | ||
{ | ||
var unitOfWork = _sp.GetSystemUserUnitOfWork(); | ||
var readModel1 = await unitOfWork.GetReadModel<EventsModEqualReadModel, int>(3); | ||
sum += readModel1.GotEvents; | ||
} | ||
|
||
return sum; | ||
} | ||
|
||
[Benchmark] | ||
public async Task<int> PublishEventsAndReadReadHeavyMultipleReadModel() | ||
{ | ||
var sum = 0; | ||
for (var j = 1; j < 5000; j++) | ||
{ | ||
var unitOfWork = _sp.GetSystemUserUnitOfWork(); | ||
var readModel1 = await unitOfWork.GetReadModel<EventsModEqualReadModel, int>(j); | ||
sum += readModel1.GotEvents; | ||
} | ||
|
||
return sum; | ||
} | ||
} | ||
|
||
public class AllowAllPolicy : Policy { | ||
public ValueTask<bool> AuthenticateEvent(EventEnvelope envelope, IAuthContext authContext) | ||
{ | ||
return ValueTask.FromResult(true); | ||
} | ||
|
||
public ValueTask<bool> AuthenticateReadModel(IReadModel readModel, IAuthContext authContext) | ||
{ | ||
return ValueTask.FromResult(true); | ||
} | ||
} | ||
|
||
public record TestEvent(int Id) : Event; | ||
|
||
public record EventsEqualReadModel : ReadModelWithKey<int> | ||
{ | ||
public int GotEvents { get; private init; } | ||
|
||
protected override EventsEqualReadModel When(EventEnvelope envelope) | ||
{ | ||
return envelope.Event switch | ||
{ | ||
TestEvent testEvent when testEvent.Id == Id => this with { GotEvents = GotEvents + 1 }, | ||
_ => this, | ||
}; | ||
} | ||
} | ||
|
||
public record EventsModEqualReadModel : ReadModelWithKey<int> | ||
{ | ||
public int GotEvents { get; private init; } | ||
|
||
protected override EventsModEqualReadModel When(EventEnvelope envelope) | ||
{ | ||
return envelope.Event switch | ||
{ | ||
TestEvent testEvent when testEvent.Id % Id == 0 => this with { GotEvents = GotEvents + 1 }, | ||
_ => this, | ||
}; | ||
} | ||
} |
This file contains 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,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFrameworks>net9.0;net8.0</TargetFrameworks> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" /> | ||
<PackageReference Include="Enterprize1.BenchmarkDotNet.GitCompare" Version="0.0.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Fluss\Fluss.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains 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,4 @@ | ||
using Benchmark; | ||
using BenchmarkDotNet.Running; | ||
|
||
BenchmarkRunner.Run<Bench>(); |
This file contains 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
This file contains 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