Skip to content

Commit

Permalink
Cleanup the benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
halgari committed Dec 15, 2023
1 parent 33cc07e commit e9646c8
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 106 deletions.
64 changes: 64 additions & 0 deletions benchmarks/NexusMods.EventSourcing.Benchmarks/ABenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NexusMods.EventSourcing.Abstractions;
using NexusMods.EventSourcing.FasterKV;
using NexusMods.EventSourcing.RocksDB;
using NexusMods.EventSourcing.TestModel;
using NexusMods.Paths;
using Settings = NexusMods.EventSourcing.FasterKV.Settings;

namespace NexusMods.EventSourcing.Benchmarks;

public abstract class ABenchmark
{
protected IServiceProvider Services = null!;

public ABenchmark()
{
var host = new HostBuilder()
.ConfigureServices((context, services) =>
{
services.AddEventSourcing()
.AddEvents();
})
.Build();

Services = host.Services;
}

protected IEventStore EventStore = null!;

public void MakeStore(Type type)
{
var serializer = Services.GetRequiredService<EventSerializer>();
IEventStore eventStore;
if (type == typeof(InMemoryEventStore<EventSerializer>))
{
eventStore = new InMemoryEventStore<EventSerializer>(serializer);
}
else if (type == typeof(FasterKVEventStore<EventSerializer>))
{
eventStore = new FasterKVEventStore<EventSerializer>(serializer,
new Settings
{
StorageLocation = FileSystem.Shared.GetKnownPath(KnownPath.EntryDirectory).Combine("FasterKV.EventStore" + Guid.NewGuid())
});
}
else if (type == typeof(RocksDBEventStore<EventSerializer>))
{
eventStore = new RocksDBEventStore<EventSerializer>(serializer,
new RocksDB.Settings
{
StorageLocation = FileSystem.Shared.GetKnownPath(KnownPath.EntryDirectory).Combine("FasterKV.EventStore" + Guid.NewGuid())
});
}
else
{
throw new NotSupportedException($"EventStoreType '{type}' is not supported.");
}

EventStore = eventStore;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,25 @@
using Microsoft.Extensions.Hosting;
using NexusMods.EventSourcing.Abstractions;
using NexusMods.EventSourcing.FasterKV;
using NexusMods.EventSourcing.RocksDB;
using NexusMods.EventSourcing.TestModel;
using NexusMods.EventSourcing.TestModel.Events;
using NexusMods.EventSourcing.TestModel.Model;
using NexusMods.Paths;
using Settings = NexusMods.EventSourcing.FasterKV.Settings;

namespace NexusMods.EventSourcing.Benchmarks;

public class EntityContextBenchmarks

[MemoryDiagnoser]
public class EntityContextBenchmarks : ABenchmark
{
private readonly IServiceProvider _services;
private IEventStore _eventStore = null!;
private EntityId<Loadout>[] _ids = Array.Empty<EntityId<Loadout>>();
private EntityContext _context = null!;

[Params(typeof(InMemoryEventStore<EventSerializer>),
typeof(FasterKVEventStore<EventSerializer>))]
//typeof(FasterKVEventStore<EventSerializer>),
typeof(RocksDBEventStore<EventSerializer>))]
public Type EventStoreType { get; set; } = typeof(InMemoryEventStore<EventSerializer>);

[Params(100, 1000)]
Expand All @@ -28,46 +31,16 @@ public class EntityContextBenchmarks
[Params(100, 1000)]
public int EntityCount { get; set; }

public EntityContextBenchmarks()
{
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 if (EventStoreType == typeof(FasterKVEventStore<EventSerializer>))
{
_eventStore = new FasterKVEventStore<EventSerializer>(_services.GetRequiredService<EventSerializer>(),
new Settings
{
StorageLocation = FileSystem.Shared.GetKnownPath(KnownPath.EntryDirectory).Combine("FasterKV.EventStore" + Guid.NewGuid())
});
}
else
{
throw new NotSupportedException($"EventStoreType '{EventStoreType}' is not supported.");
}

_context = new EntityContext(_eventStore);
_context = new EntityContext(EventStore);

_ids = new EntityId<Loadout>[EntityCount];
for (var e = 0; e < EntityCount; e++)
{
var evt = new CreateLoadout(EntityId<Loadout>.NewId(), $"Loadout {e}");
_eventStore.Add(evt);
EventStore.Add(evt);
_ids[e] = evt.Id;
}

Expand All @@ -76,7 +49,7 @@ public void Setup()
{
for (var e = 0; e < EntityCount; e++)
{
_eventStore.Add(new RenameLoadout(_ids[e], $"Loadout {e} {ev}"));
EventStore.Add(new RenameLoadout(_ids[e], $"Loadout {e} {ev}"));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<ItemGroup>
<ProjectReference Include="..\..\src\NexusMods.EventSourcing.Abstractions\NexusMods.EventSourcing.Abstractions.csproj" />
<ProjectReference Include="..\..\src\NexusMods.EventSourcing.FasterKV\NexusMods.EventSourcing.FasterKV.csproj" />
<ProjectReference Include="..\..\src\NexusMods.EventSourcing.RocksDB\NexusMods.EventSourcing.RocksDB.csproj" />
<ProjectReference Include="..\..\src\NexusMods.EventSourcing\NexusMods.EventSourcing.csproj" />
<ProjectReference Include="..\..\tests\NexusMods.EventSourcing.TestModel\NexusMods.EventSourcing.TestModel.csproj" />
</ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion benchmarks/NexusMods.EventSourcing.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
using NexusMods.EventSourcing;
using NexusMods.EventSourcing.Benchmarks;
using NexusMods.EventSourcing.FasterKV;
using NexusMods.EventSourcing.RocksDB;
using NexusMods.EventSourcing.TestModel;


#if DEBUG
var readBenchmarks = new EntityContextBenchmarks();
readBenchmarks.EventStoreType = typeof(InMemoryEventStore<EventSerializer>);
readBenchmarks.EventStoreType = typeof(RocksDBEventStore<EventSerializer>);
readBenchmarks.EventCount = 10;
readBenchmarks.EntityCount = 10;
Console.WriteLine("Setup");
Expand Down
49 changes: 8 additions & 41 deletions benchmarks/NexusMods.EventSourcing.Benchmarks/ReadBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
using System;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NexusMods.EventSourcing.Abstractions;
using NexusMods.EventSourcing.FasterKV;
using NexusMods.EventSourcing.RocksDB;
using NexusMods.EventSourcing.TestModel;
using NexusMods.EventSourcing.TestModel.Events;
using NexusMods.EventSourcing.TestModel.Model;
using NexusMods.Paths;

namespace NexusMods.EventSourcing.Benchmarks;

[MemoryDiagnoser]
public class ReadBenchmarks
public class ReadBenchmarks : ABenchmark
{
private readonly IServiceProvider _services;
private IEventStore _eventStore = null!;
private EntityId<Loadout>[] _ids = Array.Empty<EntityId<Loadout>>();

[Params(typeof(InMemoryEventStore<EventSerializer>),
typeof(FasterKVEventStore<EventSerializer>))]
//typeof(FasterKVEventStore<EventSerializer>),
typeof(RocksDBEventStore<EventSerializer>))]
public Type EventStoreType { get; set; } = typeof(InMemoryEventStore<EventSerializer>);

[Params(100, 1000)]
Expand All @@ -29,44 +24,16 @@ public class ReadBenchmarks
[Params(100, 1000)]
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 if (EventStoreType == typeof(FasterKVEventStore<EventSerializer>))
{
_eventStore = new FasterKVEventStore<EventSerializer>(_services.GetRequiredService<EventSerializer>(),
new Settings
{
StorageLocation = FileSystem.Shared.GetKnownPath(KnownPath.EntryDirectory).Combine("FasterKV.EventStore" + Guid.NewGuid())
});
}
else
{
throw new NotSupportedException($"EventStoreType '{EventStoreType}' is not supported.");
}
MakeStore(EventStoreType);

_ids = new EntityId<Loadout>[EntityCount];
for (var e = 0; e < EntityCount; e++)
{
var evt = new CreateLoadout(EntityId<Loadout>.NewId(), $"Loadout {e}");
_eventStore.Add(evt);
EventStore.Add(evt);
_ids[e] = evt.Id;
}

Expand All @@ -75,7 +42,7 @@ public void Setup()
{
for (var e = 0; e < EntityCount; e++)
{
_eventStore.Add(new RenameLoadout(_ids[e], $"Loadout {e} {ev}"));
EventStore.Add(new RenameLoadout(_ids[e], $"Loadout {e} {ev}"));
}
}
}
Expand All @@ -85,7 +52,7 @@ public void ReadEvents()
{

var ingester = new Counter();
_eventStore.EventsForEntity(_ids[_ids.Length/2].Value, ingester);
EventStore.EventsForEntity(_ids[_ids.Length/2].Value, ingester);
}

private class Counter : IEventIngester
Expand Down
30 changes: 8 additions & 22 deletions benchmarks/NexusMods.EventSourcing.Benchmarks/WriteBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,28 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NexusMods.EventSourcing.Abstractions;
using NexusMods.EventSourcing.RocksDB;
using NexusMods.EventSourcing.TestModel;
using NexusMods.EventSourcing.TestModel.Events;
using NexusMods.EventSourcing.TestModel.Model;

namespace NexusMods.EventSourcing.Benchmarks;

[MemoryDiagnoser]
public class WriteBenchmarks
public class WriteBenchmarks : ABenchmark
{
private readonly IServiceProvider _services;
private InMemoryEventStore<EventSerializer> _eventStore = null!;
private readonly IEvent[] _events;

[Params(typeof(InMemoryEventStore<EventSerializer>))]
[Params(typeof(InMemoryEventStore<EventSerializer>),
//typeof(FasterKVEventStore<EventSerializer>),
typeof(RocksDBEventStore<EventSerializer>))]
public Type EventStoreType { get; set; } = typeof(InMemoryEventStore<EventSerializer>);

[Params(100, 1000, 10000)]
public int EventCount { get; set; } = 100;

public WriteBenchmarks()
public WriteBenchmarks() : base()
{
var host = new HostBuilder()
.ConfigureServices((context, services) =>
{
services.AddEventSourcing()
.AddEvents();
})
.Build();

_events = new IEvent[]
{
Expand All @@ -40,20 +34,12 @@ public WriteBenchmarks()
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.");
}
MakeStore(EventStoreType);
}

[Benchmark]
Expand All @@ -62,7 +48,7 @@ public async Task WriteEvents()
for (var i = 0; i < EventCount; i++)
{
var evnt = _events[i % _events.Length];
_eventStore.Add(evnt);
EventStore.Add(evnt);
}
}
}
6 changes: 3 additions & 3 deletions src/NexusMods.EventSourcing.RocksDB/RocksDBEventStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ public class RocksDBEventStore<TSerializer> : IEventStore
private readonly TSerializer _serializer;
private readonly SpanDeserializer<TSerializer> _deserializer;

public RocksDBEventStore(Settings settings, TSerializer serializer)
public RocksDBEventStore(TSerializer serializer, Settings settings)
{
_serializer = serializer;
_families = new ColumnFamilies();
_families.Add("events", new ColumnFamilyOptions());
_families.Add("entityIndex", new ColumnFamilyOptions());
var options = new DbOptions();
options.SetCreateIfMissing(true);
options.SetCreateIfMissing();
_db = RocksDb.Open(options,
settings.StorageLocation.ToString(), new ColumnFamilies());
_eventsColumn = _db.CreateColumnFamily(new ColumnFamilyOptions(), "events");
Expand Down Expand Up @@ -82,7 +82,7 @@ public void EventsForEntity<TIngester>(EntityId entityId, TIngester ingester) wh
{
options.SetIterateUpperBound(endKeyPtr, 24);
options.SetIterateLowerBound(startKeyPtr, 24);
var iterator = _db.NewIterator(_entityIndexColumn, options);
using var iterator = _db.NewIterator(_entityIndexColumn, options);

iterator.Seek(startKeyPtr, 24);
while (iterator.Valid())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
namespace NexusMods.EventSourcing.Tests.EventStoreTests;

public class RocksDBEventStoreTests(EventSerializer serializer) : AEventStoreTest<RocksDBEventStore<EventSerializer>>(
new RocksDBEventStore<EventSerializer>(new Settings
new RocksDBEventStore<EventSerializer>(serializer, new Settings
{
StorageLocation = FileSystem.Shared.GetKnownPath(KnownPath.EntryDirectory)
.Combine("FasterKV.EventStore" + Guid.NewGuid())
}, serializer));
}));

0 comments on commit e9646c8

Please sign in to comment.