Skip to content

Commit

Permalink
Illustrative benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
thygrrr committed Nov 11, 2024
1 parent 56a1284 commit 1b60cd4
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 14 deletions.
54 changes: 54 additions & 0 deletions fennecs.benchmarks/Conceptual/CostCentersBench.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using BenchmarkDotNet.Attributes;
using fennecs;

namespace Benchmark.Conceptual;

public class CostCentersBench
{
[Params(10000)]
public int Count { get; set; }

private World _world = null!;
private Stream<int> _streamOne = null!;
private Stream<int> _streamMany = null!;

[GlobalSetup]
public void Setup()
{
_world = new(Count * 3);

for (var i = 0; i < Count; i++)
{
_world.Spawn().Add(i);
}

for (var i = 0; i < Count; i++)
{
var unique = _world.Spawn();
_world.Spawn().Add(i).Add("relation", unique);
}

_streamOne = _world.Query<int>().Not<string>(Match.Any).Stream();
_streamMany = _world.Query<int>().Has<string>(Match.Any).Stream();

Console.WriteLine($"World: {_world.Count} Entities");
Console.WriteLine($"Stream One: {_streamOne.Count} Entities, {_streamOne.Query.Archetypes.Count} Archetypes");
Console.WriteLine($"Stream Many: {_streamMany.Count} Entities, {_streamMany.Query.Archetypes.Count} Archetypes");
}

[Benchmark]
public int SingleArchetype()
{
var output = 0;
_streamOne.For((ref int value) => { output += value; });
return output;
}

[Benchmark]
public int ManyArchetypes()
{
var output = 0;
_streamMany.For((ref int value) => { output += value; });
return output;
}
}
96 changes: 96 additions & 0 deletions fennecs.benchmarks/Conceptual/EventsVsListsBench.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using BenchmarkDotNet.Attributes;

namespace Benchmark.Conceptual;

public class EventsVsListsBench
{
[Params(100)]
public int Count { get; set; }

private List<Action<int>> _listeners = null!;
private event Action<int>? Event = null!;

[GlobalSetup]
public void Setup()
{
_listeners = new(1_000_000)
{
OnEvent,
};
Event += OnEvent;
}

private int _sum;
private Random _rnd = null!;
private void OnEvent(int number)
{
_sum += number;
}

[IterationSetup]
public void SetupEvents()
{
_sum = 0;
_rnd = new();

_listeners.Clear();
Event = null!;

for (var i = 0; i < Count; i++)
{
_listeners.Add(OnEvent);
Event += OnEvent;
}
}


[Benchmark]
public void AddListeners()
{
for (var i = 0; i < 10_000; i++)
{
_listeners.Add(OnEvent);
}

for (var i = 0; i < 10_000; i++)
{
_listeners.Remove(OnEvent);
}
}

[Benchmark]
public void AddEvents()
{
for (var i = 0; i < 10_000; i++)
{
Event += OnEvent;
}

for (var i = 0; i < 10_000; i++)
{
Event -= OnEvent;
}
}

[Benchmark(Baseline = true)]
public int InvokeEvents()
{
for (var i = 0; i < 1_000_000; i++)
{
var number = _rnd.Next();
Event?.Invoke(number);
}
return _sum;
}

[Benchmark]
public int InvokeListeners()
{
for (var i = 0; i < 1_000_000; i++)
{
var number = _rnd.Next();
foreach (var action in _listeners) action(number);
}
return _sum;
}
}
17 changes: 13 additions & 4 deletions fennecs.benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,18 @@
var config = ManualConfig
.Create(DefaultConfig.Instance)
.WithOptions(ConfigOptions.JoinSummary)
.AddJob(Job.ShortRun.WithRuntime(CoreRuntime.Core90))
.AddJob(Job.ShortRun.WithRuntime(NativeAotRuntime.Net90))

.AddJob(
Job.ShortRun.WithId("Default")
.WithRuntime(CoreRuntime.Core90)
)

/*
.AddJob(
Job.ShortRun.WithId("Native")
.WithRuntime(NativeAotRuntime.Net90)
)
*/
.HideColumns("Job", "Error", "Median", "RatioSD");

// Most relevant vectorization instruction sets, add other intrinsics as needed.
Expand All @@ -25,5 +35,4 @@
if (!AdvSimd.IsSupported) config.AddFilter(new CategoryExclusion(nameof(AdvSimd)));


//BenchmarkRunner.Run<DorakuBenchmarks>(config);
BenchmarkRunner.Run<RWBenchmarks>(config);
BenchmarkRunner.Run<CostCentersBench>(config);
16 changes: 8 additions & 8 deletions fennecs.benchmarks/fennecs.benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@
<PackageId>fennecs.benchmarks</PackageId>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<CheckForOverflowUnderflow>false</CheckForOverflowUnderflow>
<LangVersion>preview</LangVersion>
<LangVersion>13</LangVersion>
<PublishAot>true</PublishAot>
<OptimizationPreference>Speed</OptimizationPreference>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<PlatformTarget>x64</PlatformTarget>
<PlatformTarget>x64</PlatformTarget>
<TieredPGO>true</TieredPGO>
<TieredCompilation>true</TieredCompilation>
<TieredCompilationQuickJit>true</TieredCompilationQuickJit>
<TieredCompilationQuickJitForLoops>true</TieredCompilationQuickJitForLoops>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\fennecs\fennecs.csproj" />
<ProjectReference Include="..\fennecs\fennecs.csproj"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.14.1-nightly.20241107.194" />
<PackageReference Include="BenchmarkDotNet" Version="0.14.1-nightly.20241107.194"/>
</ItemGroup>
</Project>
9 changes: 7 additions & 2 deletions fennecs/fennecs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackageId>fennecs</PackageId>
<Version>0.5.14-beta</Version>
<Version>0.6.0-beta</Version>
<Title>fennecs</Title>
<Product>fennecs Entity-Component System</Product>
<Authors>Moritz Voss, Aaron Winter</Authors>
<Authors>Moritz Voss and Contributors</Authors>
<Company>tiger.blue</Company>
<Description>The tiny ECS that could!</Description>
<Copyright>2024 Moritz Voss, 2022 Aaron Winter</Copyright>
Expand All @@ -34,6 +34,11 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<EnablePackageValidation>true</EnablePackageValidation>
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>

<TieredPGO>true</TieredPGO>
<TieredCompilation>true</TieredCompilation>
<TieredCompilationQuickJit>false</TieredCompilationQuickJit>
<TieredCompilationQuickJitForLoops>false</TieredCompilationQuickJitForLoops>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down

0 comments on commit 1b60cd4

Please sign in to comment.