Skip to content

Commit

Permalink
Added benchmark for Morpeh ECS framework
Browse files Browse the repository at this point in the history
  • Loading branch information
SH42913 authored and Doraku committed Jan 23, 2024
1 parent db67d1d commit 611d76d
Show file tree
Hide file tree
Showing 11 changed files with 614 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Tested frameworks:
- [MonoGame.Extended](https://github.com/craftworkgames/MonoGame.Extended)
- [RelEcs](https://github.com/Byteron/RelEcs)
- [Svelto.ECS](https://github.com/sebas77/Svelto.ECS)
- [Morpeh](https://github.com/scellecs/morpeh)

## [CreateEntityWithOneComponent](results/Ecs.CSharp.Benchmark.CreateEntityWithOneComponent-report-github.md)
Create entities with one component.
Expand Down
1 change: 1 addition & 0 deletions source/Ecs.CSharp.Benchmark/Categories.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ internal static class Categories
public const string MonoGameExtended = "MonoGame.Extended";
public const string RelEcs = "RelEcs";
public const string SveltoECS = "Svelto.ECS";
public const string Morpeh = "Morpeh";

public const string CreateEntity = "CreateEntity";
public const string System = "System";
Expand Down
35 changes: 35 additions & 0 deletions source/Ecs.CSharp.Benchmark/Context/MorpehBaseContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using Scellecs.Morpeh;

namespace Ecs.CSharp.Benchmark.Context
{
internal class MorpehBaseContext : IDisposable
{
public struct Component1 : IComponent
{
public int Value;
}

public struct Component2 : IComponent
{
public int Value;
}

public struct Component3 : IComponent
{
public int Value;
}

public World World { get; }

protected MorpehBaseContext()
{
World = World.Create();
}

public virtual void Dispose()
{
World.Dispose();
}
}
}
40 changes: 40 additions & 0 deletions source/Ecs.CSharp.Benchmark/CreateEntityWithOneComponent/Morpeh.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using BenchmarkDotNet.Attributes;
using Ecs.CSharp.Benchmark.Context;
using Scellecs.Morpeh;

namespace Ecs.CSharp.Benchmark
{
public partial class CreateEntityWithOneComponent
{
[Context]
private readonly MorpehBaseContext _context;

[BenchmarkCategory(Categories.Morpeh)]
[Benchmark]
public void Morpeh_Direct()
{
World world = _context.World;
for (int i = 0; i < EntityCount; ++i)
{
world.CreateEntity().AddComponent<MorpehBaseContext.Component1>();
}

world.Commit();
}

[BenchmarkCategory(Categories.Morpeh)]
[Benchmark]
public void Morpeh_Stash()
{
World world = _context.World;
Stash<MorpehBaseContext.Component1> stash1 = world.GetStash<MorpehBaseContext.Component1>();
for (int i = 0; i < EntityCount; ++i)
{
Entity entity = world.CreateEntity();
stash1.Add(entity);
}

world.Commit();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using BenchmarkDotNet.Attributes;
using Ecs.CSharp.Benchmark.Context;
using Scellecs.Morpeh;

namespace Ecs.CSharp.Benchmark
{
public partial class CreateEntityWithThreeComponents
{
[Context]
private readonly MorpehBaseContext _context;

[BenchmarkCategory(Categories.Morpeh)]
[Benchmark]
public void Morpeh_Direct()
{
World world = _context.World;
for (int i = 0; i < EntityCount; ++i)
{
world.CreateEntity().AddComponent<MorpehBaseContext.Component1>();
world.CreateEntity().AddComponent<MorpehBaseContext.Component2>();
}

world.Commit();
}

[BenchmarkCategory(Categories.Morpeh)]
[Benchmark]
public void Morpeh_Stash()
{
World world = _context.World;
Stash<MorpehBaseContext.Component1> stash1 = world.GetStash<MorpehBaseContext.Component1>();
Stash<MorpehBaseContext.Component2> stash2 = world.GetStash<MorpehBaseContext.Component2>();
for (int i = 0; i < EntityCount; ++i)
{
Entity entity = world.CreateEntity();
stash1.Add(entity);
stash2.Add(entity);
}

world.Commit();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using BenchmarkDotNet.Attributes;
using Ecs.CSharp.Benchmark.Context;
using Scellecs.Morpeh;

namespace Ecs.CSharp.Benchmark
{
public partial class CreateEntityWithTwoComponents
{
[Context]
private readonly MorpehBaseContext _context;

[BenchmarkCategory(Categories.Morpeh)]
[Benchmark]
public void Morpeh_Direct()
{
World world = _context.World;
for (int i = 0; i < EntityCount; ++i)
{
world.CreateEntity().AddComponent<MorpehBaseContext.Component1>();
world.CreateEntity().AddComponent<MorpehBaseContext.Component2>();
world.CreateEntity().AddComponent<MorpehBaseContext.Component3>();
}

world.Commit();
}

[BenchmarkCategory(Categories.Morpeh)]
[Benchmark]
public void Morpeh_Stash()
{
World world = _context.World;
Stash<MorpehBaseContext.Component1> stash1 = world.GetStash<MorpehBaseContext.Component1>();
Stash<MorpehBaseContext.Component2> stash2 = world.GetStash<MorpehBaseContext.Component2>();
Stash<MorpehBaseContext.Component3> stash3 = world.GetStash<MorpehBaseContext.Component3>();
for (int i = 0; i < EntityCount; ++i)
{
Entity entity = world.CreateEntity();
stash1.Add(entity);
stash2.Add(entity);
stash3.Add(entity);
}

world.Commit();
}
}
}
2 changes: 2 additions & 0 deletions source/Ecs.CSharp.Benchmark/Ecs.CSharp.Benchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@

<PackageReference Include="RelEcs" Version="1.5.1" />

<PackageReference Include="Scellecs.Morpeh" Version="2022.2.2" />

<PackageReference Include="Svelto.ECS" Version="3.3.2" />
<PackageReference Include="Svelto.Common" Version="3.3.2" />
</ItemGroup>
Expand Down
95 changes: 95 additions & 0 deletions source/Ecs.CSharp.Benchmark/SystemWithOneComponent/Morpeh.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System;
using BenchmarkDotNet.Attributes;
using Ecs.CSharp.Benchmark.Context;
using Scellecs.Morpeh;

namespace Ecs.CSharp.Benchmark
{
public partial class SystemWithOneComponent
{
private sealed class MorpehContext : MorpehBaseContext
{
private sealed class DirectSystem : ISystem
{
public World World { get; set; }
private Filter _filter;

public void OnAwake()
{
_filter = World.Filter.With<Component1>();
}

public void OnUpdate(float deltaTime)
{
foreach (Entity entity in _filter)
{
++entity.GetComponent<Component1>().Value;
}
}

void IDisposable.Dispose() { }
}

private sealed class StashSystem : ISystem
{
public World World { get; set; }
private Stash<Component1> _stash1;
private Filter _filter;

public void OnAwake()
{
_stash1 = World.GetStash<Component1>();
_filter = World.Filter.With<Component1>();
}

public void OnUpdate(float deltaTime)
{
foreach (Entity entity in _filter)
{
++_stash1.Get(entity).Value;
}
}

public void Dispose()
{
_stash1.Dispose();
}
}

public ISystem MonoThreadDirectSystem { get; }
public ISystem MonoThreadStashSystem { get; }

public MorpehContext(int entityCount, int entityPadding)
{
MonoThreadDirectSystem = new DirectSystem { World = World };
MonoThreadDirectSystem.OnAwake();

MonoThreadStashSystem = new StashSystem { World = World };
MonoThreadStashSystem.OnAwake();

for (int i = 0; i < entityCount; ++i)
{
for (int j = 0; j < entityPadding; ++j)
{
World.CreateEntity();
}

World.CreateEntity().AddComponent<Component1>();
}

World.Commit();
}
}

[Context]
private readonly MorpehContext _context;

[BenchmarkCategory(Categories.Morpeh)]
[Benchmark]
public void Morpeh_Direct() => _context.MonoThreadDirectSystem.OnUpdate(0f);

[BenchmarkCategory(Categories.Morpeh)]
[Benchmark]
public void Morpeh_Stash() => _context.MonoThreadStashSystem.OnUpdate(0f);
}
}
Loading

0 comments on commit 611d76d

Please sign in to comment.