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.
Benchmark work and performance tuning of serialization
- Loading branch information
Showing
11 changed files
with
165 additions
and
38 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
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
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
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
69 changes: 69 additions & 0 deletions
69
benchmarks/NexusMods.EventSourcing.Benchmarks/SerializationBenchmarks.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,69 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Security.Cryptography.X509Certificates; | ||
using BenchmarkDotNet.Attributes; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using NexusMods.EventSourcing.Abstractions; | ||
using NexusMods.EventSourcing.Serialization; | ||
using NexusMods.EventSourcing.TestModel.Events; | ||
using NexusMods.EventSourcing.TestModel.Model; | ||
|
||
namespace NexusMods.EventSourcing.Benchmarks; | ||
|
||
[MemoryDiagnoser] | ||
public class SerializationBenchmarks : ABenchmark | ||
{ | ||
private readonly IEvent[] _events; | ||
private BinaryEventSerializer _serializer = null!; | ||
private byte[][] _serializedEvents = null!; | ||
|
||
public SerializationBenchmarks() | ||
{ | ||
_events = | ||
[ | ||
new CreateLoadout(EntityId<Loadout>.NewId(), "Test"), | ||
new RenameLoadout(EntityId<Loadout>.NewId(), "Test"), | ||
new AddMod("New Mod", true, EntityId<Mod>.NewId(), new EntityId<Loadout>()), | ||
new AddCollection(EntityId<Collection>.NewId(), "NewCollection", EntityId<Loadout>.NewId(), | ||
[EntityId<Mod>.NewId()]), | ||
new DeleteMod(EntityId<Mod>.NewId(), EntityId<Loadout>.NewId()), | ||
new RenameLoadout(EntityId<Loadout>.NewId(), "Test"), | ||
new SwapModEnabled(EntityId<Mod>.NewId(), true) | ||
]; | ||
} | ||
|
||
[GlobalSetup] | ||
public void Setup() | ||
{ | ||
_serializer = Services.GetRequiredService<BinaryEventSerializer>(); | ||
|
||
_serializedEvents = _events.Select(evnt => _serializer.Serialize(evnt).ToArray()).ToArray(); | ||
} | ||
|
||
[Benchmark] | ||
public int Serialize() | ||
{ | ||
var size = 0; | ||
for (var i = 0; i < _events.Length; i++) | ||
{ | ||
var evnt = _events[i]; | ||
size += _serializer.Serialize(evnt).Length; | ||
} | ||
|
||
return size; | ||
} | ||
|
||
[Benchmark] | ||
public int Deserialize() | ||
{ | ||
var size = 0; | ||
for (var i = 0; i < _serializedEvents.Length; i++) | ||
{ | ||
var evnt = _serializedEvents[i]; | ||
_serializer.Deserialize(evnt); | ||
size += 1; | ||
} | ||
return size; | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,64 @@ | ||
using System; | ||
using System.Buffers; | ||
using System.Runtime.CompilerServices; | ||
using Reloaded.Memory.Extensions; | ||
|
||
namespace NexusMods.EventSourcing; | ||
|
||
public class PooledMemoryBufferWriter : IBufferWriter<byte> | ||
public sealed class PooledMemoryBufferWriter : IBufferWriter<byte> | ||
{ | ||
private IMemoryOwner<byte> _data; | ||
private IMemoryOwner<byte> _owner; | ||
private Memory<byte> _data; | ||
private int _idx; | ||
private int _size; | ||
|
||
public PooledMemoryBufferWriter(int initialCapacity = 1024) | ||
{ | ||
_data = MemoryPool<byte>.Shared.Rent(initialCapacity); | ||
_owner = MemoryPool<byte>.Shared.Rent(initialCapacity); | ||
_data = _owner.Memory; | ||
_idx = 0; | ||
_size = initialCapacity; | ||
} | ||
|
||
public void Reset() | ||
{ | ||
_idx = 0; | ||
} | ||
|
||
public ReadOnlySpan<byte> GetWrittenSpan() => _data.Memory[.._idx].Span; | ||
public ReadOnlySpan<byte> GetWrittenSpan() => _data.Span.SliceFast(0, _idx); | ||
|
||
private void Expand() | ||
{ | ||
var newSize = _data.Memory.Length * 2; | ||
var newSize = _data.Length * 2; | ||
var newData = MemoryPool<byte>.Shared.Rent(newSize); | ||
_data.Memory.CopyTo(newData.Memory); | ||
_data.Dispose(); | ||
_data = newData; | ||
_data.CopyTo(newData.Memory); | ||
_owner.Dispose(); | ||
_owner = newData; | ||
_data = newData.Memory; | ||
_size = newSize; | ||
} | ||
|
||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void Advance(int count) | ||
{ | ||
if (_idx + count > _data.Memory.Length) | ||
if (_idx + count > _size) | ||
Expand(); | ||
_idx += count; | ||
} | ||
|
||
public Memory<byte> GetMemory(int sizeHint = 0) | ||
{ | ||
if (_idx + sizeHint > _data.Memory.Length) | ||
if (_idx + sizeHint > _size) | ||
Expand(); | ||
return _data.Memory[_idx..]; | ||
return _data[_idx..]; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public Span<byte> GetSpan(int sizeHint = 0) | ||
{ | ||
if (_idx + sizeHint > _data.Memory.Length) | ||
if (_idx + sizeHint > _size) | ||
Expand(); | ||
return _data.Memory.Span[_idx..]; | ||
return _data.Span.SliceFast(_idx); | ||
} | ||
} |
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
Oops, something went wrong.