generated from Nexus-Mods/NexusMods.App.Template
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
7 changed files
with
206 additions
and
3 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
20 changes: 20 additions & 0 deletions
20
benchmarks/NexusMods.EventSourcing.Benchmarks/NexusMods.EventSourcing.Benchmarks.csproj
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,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\NexusMods.EventSourcing.Abstractions\NexusMods.EventSourcing.Abstractions.csproj" /> | ||
<ProjectReference Include="..\..\src\NexusMods.EventSourcing\NexusMods.EventSourcing.csproj" /> | ||
<ProjectReference Include="..\..\tests\NexusMods.EventSourcing.TestModel\NexusMods.EventSourcing.TestModel.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="BenchmarkDotNet" Version="0.13.11" /> | ||
</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,18 @@ | ||
// See https://aka.ms/new-console-template for more information | ||
|
||
using BenchmarkDotNet.Running; | ||
using NexusMods.EventSourcing; | ||
using NexusMods.EventSourcing.Benchmarks; | ||
using NexusMods.EventSourcing.TestModel; | ||
|
||
|
||
#if DEBUG | ||
var readBenchmarks = new ReadBenchmarks(); | ||
readBenchmarks.EventStoreType = typeof(InMemoryEventStore<EventSerializer>); | ||
readBenchmarks.EventCount = 10; | ||
readBenchmarks.EntityCount = 10; | ||
await readBenchmarks.Setup(); | ||
readBenchmarks.ReadEvents(); | ||
#else | ||
BenchmarkRunner.Run<ReadBenchmarks>(); | ||
#endif |
89 changes: 89 additions & 0 deletions
89
benchmarks/NexusMods.EventSourcing.Benchmarks/ReadBenchmarks.cs
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,89 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using BenchmarkDotNet.Attributes; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using NexusMods.EventSourcing.Abstractions; | ||
using NexusMods.EventSourcing.TestModel; | ||
using NexusMods.EventSourcing.TestModel.Events; | ||
using NexusMods.EventSourcing.TestModel.Model; | ||
|
||
namespace NexusMods.EventSourcing.Benchmarks; | ||
|
||
[MemoryDiagnoser] | ||
public class ReadBenchmarks | ||
{ | ||
private readonly IServiceProvider _services; | ||
private InMemoryEventStore<EventSerializer> _eventStore = null!; | ||
private EntityId<Loadout>[] _ids = Array.Empty<EntityId<Loadout>>(); | ||
|
||
[Params(typeof(InMemoryEventStore<EventSerializer>))] | ||
public Type EventStoreType { get; set; } = typeof(InMemoryEventStore<EventSerializer>); | ||
|
||
[Params(100, 10000)] | ||
public int EventCount { get; set; } | ||
|
||
[Params(100, 10000)] | ||
public int EntityCount { get; set; } | ||
|
||
public ReadBenchmarks() | ||
{ | ||
var host = new HostBuilder() | ||
.ConfigureServices((context, services) => | ||
{ | ||
services.AddEventSourcing() | ||
.AddEvents(); | ||
}) | ||
.Build(); | ||
|
||
_services = host.Services; | ||
} | ||
|
||
[GlobalSetup] | ||
public void Setup() | ||
{ | ||
if (EventStoreType == typeof(InMemoryEventStore<EventSerializer>)) | ||
{ | ||
_eventStore = new InMemoryEventStore<EventSerializer>(_services.GetRequiredService<EventSerializer>()); | ||
} | ||
else | ||
{ | ||
throw new NotSupportedException($"EventStoreType '{EventStoreType}' is not supported."); | ||
} | ||
|
||
_ids = new EntityId<Loadout>[EntityCount]; | ||
for (var e = 0; e < EntityCount; e++) | ||
{ | ||
var evt = new CreateLoadout(EntityId<Loadout>.NewId(), $"Loadout {e}"); | ||
_eventStore.Add(evt).GetAwaiter().GetResult(); | ||
_ids[e] = evt.Id; | ||
} | ||
|
||
|
||
for (var ev = 0; ev < EventCount; ev++) | ||
{ | ||
for (var e = 0; e < EntityCount; e++) | ||
{ | ||
_eventStore.Add(new RenameLoadout(_ids[e], $"Loadout {e} {ev}")).GetAwaiter().GetResult(); | ||
} | ||
} | ||
|
||
} | ||
|
||
[Benchmark] | ||
public void ReadEvents() | ||
{ | ||
|
||
var ingester = new Counter(); | ||
_eventStore.EventsForEntity(_ids[_ids.Length/2].Value, ingester); | ||
} | ||
|
||
private class Counter : IEventIngester | ||
{ | ||
public int Count { get; private set; } | ||
public void Ingest(IEvent @event) | ||
{ | ||
Count++; | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
benchmarks/NexusMods.EventSourcing.Benchmarks/WriteBenchmarks.cs
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,68 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using BenchmarkDotNet.Attributes; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using NexusMods.EventSourcing.Abstractions; | ||
using NexusMods.EventSourcing.TestModel; | ||
using NexusMods.EventSourcing.TestModel.Events; | ||
using NexusMods.EventSourcing.TestModel.Model; | ||
|
||
namespace NexusMods.EventSourcing.Benchmarks; | ||
|
||
[MemoryDiagnoser] | ||
public class WriteBenchmarks | ||
{ | ||
private readonly IServiceProvider _services; | ||
private InMemoryEventStore<EventSerializer> _eventStore = null!; | ||
private readonly IEvent[] _events; | ||
|
||
[Params(typeof(InMemoryEventStore<EventSerializer>))] | ||
public Type EventStoreType { get; set; } = typeof(InMemoryEventStore<EventSerializer>); | ||
|
||
[Params(100, 1000, 10000)] | ||
public int EventCount { get; set; } = 100; | ||
|
||
public WriteBenchmarks() | ||
{ | ||
var host = new HostBuilder() | ||
.ConfigureServices((context, services) => | ||
{ | ||
services.AddEventSourcing() | ||
.AddEvents(); | ||
}) | ||
.Build(); | ||
|
||
_events = new IEvent[] | ||
{ | ||
new CreateLoadout(EntityId<Loadout>.NewId(), "Loadout 1"), | ||
new SwapModEnabled(EntityId<Mod>.NewId(), true), | ||
new DeleteMod(EntityId<Mod>.NewId(), EntityId<Loadout>.NewId()) | ||
}; | ||
|
||
_services = host.Services; | ||
} | ||
|
||
[IterationSetup] | ||
public void Setup() | ||
{ | ||
if (EventStoreType == typeof(InMemoryEventStore<EventSerializer>)) | ||
{ | ||
_eventStore = new InMemoryEventStore<EventSerializer>(_services.GetRequiredService<EventSerializer>()); | ||
} | ||
else | ||
{ | ||
throw new NotSupportedException($"EventStoreType '{EventStoreType}' is not supported."); | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public async Task WriteEvents() | ||
{ | ||
for (var i = 0; i < EventCount; i++) | ||
{ | ||
var evnt = _events[i % _events.Length]; | ||
await _eventStore.Add(evnt); | ||
} | ||
} | ||
} |
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