Skip to content

Commit

Permalink
Add benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanrw committed Oct 16, 2024
1 parent a4c5157 commit ce09763
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 33 deletions.
37 changes: 4 additions & 33 deletions src/Arch.Benchmarks/Benchmark.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,16 @@
using System.Numerics;
using Arch.Core;
using Arch.Core.Extensions;

using Arch.Core.Utils;

namespace Arch.Benchmarks;

public class Benchmark
{
private static void Main(string[] args)
public static void Main(string[] args)
{
/*
// NOTE: Can this be replaced with ManualConfig.CreateEmpty()?
#pragma warning disable HAA0101 // Array allocation for params parameter
var config = new ManualConfig()
.WithOptions(ConfigOptions.DisableOptimizationsValidator)
.AddValidator(JitOptimizationsValidator.DontFailOnError)
.AddLogger(ConsoleLogger.Default)
.AddColumnProvider(DefaultColumnProviders.Instance);
#pragma warning restore HAA0101 // Array allocation for params parameter
*/



var world = World.Create();
for (var index = 0; index <= 100; index++)
{
world.Create<int>();
}

var desc = new QueryDescription().WithAll<int>();
for (var index = 0; index <= 100000; index++)
{
world.Query(in desc, (ref int i) =>
{
});
}



// NOTE: Is `-- --job` a typo?
// Use: dotnet run -c Release --framework net7.0 -- --job short --filter *IterationBenchmark*
//BenchmarkSwitcher.FromAssembly(typeof(Benchmark).Assembly).Run(args, config);
BenchmarkSwitcher.FromAssembly(typeof(Benchmark).Assembly).Run(args);
//BenchmarkSwitcher.FromAssembly(typeof(Benchmark).Assembly).Run(args, new DebugInProcessConfig());
}
}
67 changes: 67 additions & 0 deletions src/Arch.Benchmarks/DuplicateBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using Arch.Core;
using Arch.Core.Utils;

namespace Arch.Benchmarks;

[HtmlExporter]
//[MemoryDiagnoser]
//[HardwareCounters(HardwareCounter.CacheMisses)]
public class DuplicateBenchmark
{
public int Amount = 100000;

private static readonly ComponentType[] _group = { typeof(Transform), typeof(Velocity) };
private readonly QueryDescription _queryDescription = new(all: _group);

private static World? _world;
private static Entity _entity = Entity.Null;
private static Entity[]? _array = null;

[IterationSetup]
public void Setup()
{
_world = World.Create();
_world.Reserve(_group, 1);
_entity = _world.Create(new Transform { X = 111, Y = 222}, new Velocity { X = 333, Y = 444 });
_array = new Entity[Amount];
}

[IterationCleanup]
public void Cleanup()
{
World.Destroy(_world);
_world = null;
}

/// DuplicateN() method.
[Benchmark]
public void DuplicateNInternal()
{
_world.DuplicateN(_entity, _array.AsSpan());
}

/// DuplicateN() in terms of Duplicate() method.
[Benchmark]
public void DuplicateNDuplicate()
{
for (int i = 0; i < Amount; ++i)
{
_array[i] = _world.Duplicate(_entity);
}
}

/// Benchmark DuplicateN() if implemented via GetAllComponents.
[Benchmark]
public void DuplicateNGetAllComponents()
{
for (int i = 0; i < Amount; ++i)
{
var arch = _world.GetArchetype(_entity);
var copiedEntity = _world.Create(arch.Signature);
foreach (var c in _world.GetAllComponents(_entity))
{
_world.Set(_entity, c);
}
}
}
}

0 comments on commit ce09763

Please sign in to comment.