add a benchmark proj
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="HlsfAllocatorBench"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using BenchmarkDotNet.Attributes;
|
||||
using StgSharp.HighPerformance.Memory;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace StgSharp.Benchmark
|
||||
{
|
||||
[MemoryDiagnoser]
|
||||
[InvocationCount(16)]
|
||||
public class HlsfAllocatorBench
|
||||
{
|
||||
|
||||
private const int AllocationCount = 2048;
|
||||
|
||||
private static readonly int[] AllocSizes = [
|
||||
64,
|
||||
96,
|
||||
150,
|
||||
200,
|
||||
400,
|
||||
1024,
|
||||
4096
|
||||
];
|
||||
private HybridLayerSegregatedFitAllocator _hlsfAllocator = null!;
|
||||
private readonly List<HybridLayerSegregatedFitAllocationHandle> _hlsfLocalList = new();
|
||||
private readonly List<nint> _libcLocalList = new();
|
||||
private Random _random = new(42);
|
||||
|
||||
[GlobalCleanup]
|
||||
public void Cleanup()
|
||||
{
|
||||
_hlsfAllocator.Dispose();
|
||||
_hlsfLocalList.Clear();
|
||||
_hlsfLocalList.TrimExcess();
|
||||
_libcLocalList.Clear();
|
||||
_libcLocalList.TrimExcess();
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public void Hlsf()
|
||||
{
|
||||
for (int i = 0; i < AllocationCount; i++)
|
||||
{
|
||||
lock (_hlsfAllocator)
|
||||
{
|
||||
HybridLayerSegregatedFitAllocationHandle allocate = _hlsfAllocator.Alloc(GetNextRandomSize());
|
||||
_hlsfLocalList.Add(allocate);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < _hlsfLocalList.Count; i++)
|
||||
{
|
||||
lock (_hlsfAllocator) {
|
||||
_hlsfAllocator.Free(_hlsfLocalList[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void InvocationSetup()
|
||||
{
|
||||
_hlsfLocalList.Clear();
|
||||
_libcLocalList.Clear();
|
||||
}
|
||||
|
||||
[IterationSetup]
|
||||
public void IterationSetup()
|
||||
{
|
||||
_hlsfLocalList.Clear();
|
||||
_libcLocalList.Clear();
|
||||
}
|
||||
|
||||
[Benchmark(Baseline = true)]
|
||||
public unsafe void Libc()
|
||||
{
|
||||
for (int i = 0; i < AllocationCount; i++)
|
||||
{
|
||||
void* allocate = NativeMemory.AlignedAlloc(GetNextRandomSize(), 64);
|
||||
_libcLocalList.Add((nint)allocate);
|
||||
}
|
||||
|
||||
for (int i = 0; i < _libcLocalList.Count; i++) {
|
||||
NativeMemory.AlignedFree((void*)_libcLocalList[i]);
|
||||
}
|
||||
}
|
||||
|
||||
[GlobalSetup]
|
||||
public void Setup()
|
||||
{
|
||||
_random = new Random(42);
|
||||
_hlsfAllocator = new HybridLayerSegregatedFitAllocator(GetArenaBytes(), align:64);
|
||||
}
|
||||
|
||||
private static nuint GetArenaBytes()
|
||||
{
|
||||
const int defaultMb = 512;
|
||||
const int minMb = 64;
|
||||
int mb = defaultMb;
|
||||
|
||||
string? env = Environment.GetEnvironmentVariable("HLSF_BENCH_ARENA_MB");
|
||||
if (!string.IsNullOrWhiteSpace(env) &&
|
||||
int.TryParse(env, out int parsed) &&
|
||||
parsed >= minMb) {
|
||||
mb = parsed;
|
||||
}
|
||||
|
||||
return (nuint)mb * 1024u * 1024u;
|
||||
}
|
||||
|
||||
private uint GetNextRandomSize() => (uint)AllocSizes[_random.Next(AllocSizes.Length)];
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="Program"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using BenchmarkDotNet.Running;
|
||||
using StgSharp.Benchmark;
|
||||
|
||||
namespace StgSharp.Benchmark
|
||||
{
|
||||
internal static class Program
|
||||
{
|
||||
|
||||
private static void Main(
|
||||
string[] args
|
||||
)
|
||||
{
|
||||
BenchmarkRunner.Run<HlsfAllocatorBench>();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
<Platforms>x64</Platforms>
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<SolutionDir Condition="'$(SolutionDir)'=='' Or '$(SolutionDir)'=='*Undefined*'">$(MSBuildProjectDirectory)\..\</SolutionDir>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\StgSharp.Common\StgSharp.Common.csproj" Properties="DisablePostBuildCopy=true" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<NativeArtifacts Include="$(SolutionDir)build\**\StgSharp.Native*.dll" />
|
||||
<NativeArtifacts Include="$(SolutionDir)build\**\StgSharp.Native*.pdb" />
|
||||
</ItemGroup>
|
||||
|
||||
<Target Name="CopyNativeArtifacts" AfterTargets="Build" Condition="'$(DisableNativeCopy)'!='true'">
|
||||
<Copy SourceFiles="@(NativeArtifacts)" DestinationFolder="$(TargetDir)" SkipUnchangedFiles="true" Condition="@(NativeArtifacts) != ''" />
|
||||
</Target>
|
||||
</Project>
|
||||
Reference in New Issue
Block a user