Skip to content

Commit

Permalink
Add packet buffer benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaioru committed Aug 28, 2024
1 parent 783db95 commit 7eeca07
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Edelstein.sln
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Edelstein.Protocol.Plugin.G
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Edelstein.Common.Database.Tests", "test\Edelstein.Common.Database.Tests\Edelstein.Common.Database.Tests.csproj", "{F9A5686D-2B9F-4324-B723-2F01D20179C5}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "benchmark", "benchmark", "{9FA21AD7-E78E-45D0-B15C-0BB96A18D55A}"
ProjectSection(SolutionItems) = preProject
benchmarks\Directory.Build.Props = benchmarks\Directory.Build.Props
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Edelstein.Benchmarks", "benchmarks\Edelstein.Benchmarks\Edelstein.Benchmarks.csproj", "{492FE1C7-5672-4BC9-81CC-35FC03D082B7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -141,6 +148,7 @@ Global
{BAD0A50A-E52C-47AC-80AA-1469699F3DDF} = {E50DFCDF-39D5-4D0D-A46E-94D11D795087}
{7F1B5F76-72E1-4BD6-8A3B-C1DB60AD928E} = {82D7864B-19AD-484C-BD2E-897F05B5852C}
{F9A5686D-2B9F-4324-B723-2F01D20179C5} = {9170CBBC-0B0B-4553-AE40-CC48A0D856A1}
{492FE1C7-5672-4BC9-81CC-35FC03D082B7} = {9FA21AD7-E78E-45D0-B15C-0BB96A18D55A}
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6527D18C-B0E2-49BA-B649-4D5DB6216069}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -265,5 +273,9 @@ Global
{F9A5686D-2B9F-4324-B723-2F01D20179C5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F9A5686D-2B9F-4324-B723-2F01D20179C5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F9A5686D-2B9F-4324-B723-2F01D20179C5}.Release|Any CPU.Build.0 = Release|Any CPU
{492FE1C7-5672-4BC9-81CC-35FC03D082B7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{492FE1C7-5672-4BC9-81CC-35FC03D082B7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{492FE1C7-5672-4BC9-81CC-35FC03D082B7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{492FE1C7-5672-4BC9-81CC-35FC03D082B7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
21 changes: 21 additions & 0 deletions benchmarks/Directory.Build.Props
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project>

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<LangVersion>12.0</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsPublishable>false</IsPublishable>
</PropertyGroup>

<PropertyGroup>
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
</ItemGroup>

</Project>
5 changes: 5 additions & 0 deletions benchmarks/Edelstein.Benchmarks/Edelstein.Benchmarks.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\src\protocol\Edelstein.Protocol.Network\Edelstein.Protocol.Network.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using BenchmarkDotNet.Attributes;
using Edelstein.Protocol.Network.Packets;

namespace Edelstein.Benchmarks.Packets;

public partial class Packet_RawVsStructured
{
[Benchmark]
public Span<byte> Raw()
{
using var stream = manager.GetStream();
using var writer = new RawPacketWriter()
.WriteBytes(data);

writer.DispatchTo(stream);
return stream.GetSpan();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using BenchmarkDotNet.Attributes;
using BinarySerialization;
using Edelstein.Protocol.Network.Packets;

namespace Edelstein.Benchmarks.Packets;

public partial class Packet_RawVsStructured
{
[Benchmark]
public Span<byte> Structured()
{
using var stream = manager.GetStream();
using var writer = new RawPacketWriter()
.WriteStructured(new StructuredPacket
{
Data = data
});

writer.DispatchTo(stream);
return stream.GetSpan();
}
}

public record StructuredPacket : StructuredBasePacket
{
[FieldOrder(0)]
public required byte[] Data { get; init; }
}
26 changes: 26 additions & 0 deletions benchmarks/Edelstein.Benchmarks/Packets/Packet_RawVsStructured.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Text;
using BenchmarkDotNet.Attributes;
using BinarySerialization;
using Edelstein.Protocol.Network.Packets;
using Microsoft.IO;

namespace Edelstein.Benchmarks.Packets;

[MemoryDiagnoser]
public partial class Packet_RawVsStructured
{
private const int N = 10000;
private readonly byte[] data;

private readonly RecyclableMemoryStreamManager manager = new(new RecyclableMemoryStreamManager.Options
{
BlockSize = 256,
ThrowExceptionOnToArray = true
});

public Packet_RawVsStructured()
{
data = new byte[N];
new Random(42).NextBytes(data);
}
}
5 changes: 5 additions & 0 deletions benchmarks/Edelstein.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using BenchmarkDotNet.Running;

BenchmarkSwitcher
.FromAssembly(typeof(Program).Assembly)
.Run(args);

0 comments on commit 7eeca07

Please sign in to comment.