feat: add basic benchmark for binary parsing
This commit is contained in:
parent
160c90cff5
commit
d54e2504a0
3 changed files with 77 additions and 0 deletions
15
Benchmarks/Benchmarks.csproj
Normal file
15
Benchmarks/Benchmarks.csproj
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<LangVersion>default</LangVersion>
|
||||||
|
<TargetFramework>net7.0</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="BenchmarkDotNet" Version="0.13.7" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
50
Benchmarks/BinaryConversionBenchmarks.cs
Normal file
50
Benchmarks/BinaryConversionBenchmarks.cs
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
using BenchmarkDotNet.Attributes;
|
||||||
|
using BenchmarkDotNet.Jobs;
|
||||||
|
using BenchmarkDotNet.Order;
|
||||||
|
|
||||||
|
namespace Benchmarks;
|
||||||
|
|
||||||
|
[SimpleJob(RuntimeMoniker.NativeAot70)]
|
||||||
|
[SimpleJob(RuntimeMoniker.Net70)]
|
||||||
|
[SimpleJob(RuntimeMoniker.Net60)]
|
||||||
|
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
|
||||||
|
[RankColumn]
|
||||||
|
[MemoryDiagnoser]
|
||||||
|
[ExceptionDiagnoser]
|
||||||
|
[ThreadingDiagnoser]
|
||||||
|
[DisassemblyDiagnoser]
|
||||||
|
public class BinaryConversionBenchmarks
|
||||||
|
{
|
||||||
|
private byte[] data;
|
||||||
|
private int offset;
|
||||||
|
|
||||||
|
[GlobalSetup]
|
||||||
|
public void Setup()
|
||||||
|
{
|
||||||
|
data = RandomNumberGenerator.GetBytes(4000);
|
||||||
|
offset = RandomNumberGenerator.GetInt32(0, 3500);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark]
|
||||||
|
public Int16 BitConverterTest()
|
||||||
|
{
|
||||||
|
return BitConverter.ToInt16(data, offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark]
|
||||||
|
public Int16 BinaryReader()
|
||||||
|
{
|
||||||
|
using MemoryStream ms = new MemoryStream(data);
|
||||||
|
using BinaryReader reader = new BinaryReader(ms);
|
||||||
|
reader.BaseStream.Position = offset;
|
||||||
|
return reader.ReadInt16();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark]
|
||||||
|
public Int16 BinaryPrimitives()
|
||||||
|
{
|
||||||
|
return System.Buffers.Binary.BinaryPrimitives.ReadInt16LittleEndian(
|
||||||
|
new ArraySegment<byte>(data, offset, sizeof(short)));
|
||||||
|
}
|
||||||
|
}
|
12
Benchmarks/Program.cs
Normal file
12
Benchmarks/Program.cs
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
using System.Reflection;
|
||||||
|
using BenchmarkDotNet.Running;
|
||||||
|
|
||||||
|
namespace Benchmarks;
|
||||||
|
|
||||||
|
internal class Program
|
||||||
|
{
|
||||||
|
public static void Main(string[] args)
|
||||||
|
{
|
||||||
|
BenchmarkRunner.Run(Assembly.GetExecutingAssembly());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue