2019-06-02 01:17:07 +02:00
|
|
|
using BenchmarkDotNet.Attributes;
|
|
|
|
|
using Moq;
|
2022-10-11 20:51:52 +02:00
|
|
|
using Robust.Shared.Analyzers;
|
2020-06-22 04:43:47 -04:00
|
|
|
using Robust.Shared.Exceptions;
|
2019-06-02 01:17:07 +02:00
|
|
|
using Robust.Shared.GameObjects;
|
|
|
|
|
using Robust.Shared.IoC;
|
2020-06-22 04:43:47 -04:00
|
|
|
using Robust.Shared.Log;
|
2021-09-28 13:35:29 +02:00
|
|
|
using Robust.Shared.Map;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Shared.Reflection;
|
2019-06-02 01:17:07 +02:00
|
|
|
|
|
|
|
|
namespace Content.Benchmarks
|
|
|
|
|
{
|
2022-10-11 20:51:52 +02:00
|
|
|
[Virtual]
|
2023-08-22 18:14:33 -07:00
|
|
|
public partial class EntityManagerGetAllComponents
|
2019-06-02 01:17:07 +02:00
|
|
|
{
|
2021-09-28 13:35:29 +02:00
|
|
|
private IEntityManager _entityManager;
|
2019-06-02 01:17:07 +02:00
|
|
|
|
2020-06-22 04:43:47 -04:00
|
|
|
[Params(5000)] public int N { get; set; }
|
|
|
|
|
|
|
|
|
|
public static void TestRun()
|
|
|
|
|
{
|
2021-09-28 13:35:29 +02:00
|
|
|
var x = new EntityManagerGetAllComponents
|
2020-06-22 04:43:47 -04:00
|
|
|
{
|
|
|
|
|
N = 500
|
|
|
|
|
};
|
|
|
|
|
x.Setup();
|
|
|
|
|
x.Run();
|
|
|
|
|
}
|
2019-06-02 01:17:07 +02:00
|
|
|
|
|
|
|
|
[GlobalSetup]
|
|
|
|
|
public void Setup()
|
|
|
|
|
{
|
|
|
|
|
// Initialize component manager.
|
|
|
|
|
IoCManager.InitThread();
|
|
|
|
|
|
2021-09-28 13:35:29 +02:00
|
|
|
IoCManager.Register<IEntityManager, EntityManager>();
|
2020-06-22 04:43:47 -04:00
|
|
|
IoCManager.Register<IRuntimeLog, RuntimeLog>();
|
|
|
|
|
IoCManager.Register<ILogManager, LogManager>();
|
|
|
|
|
IoCManager.Register<IDynamicTypeFactory, DynamicTypeFactory>();
|
|
|
|
|
IoCManager.Register<IEntitySystemManager, EntitySystemManager>();
|
|
|
|
|
IoCManager.RegisterInstance<IReflectionManager>(new Mock<IReflectionManager>().Object);
|
2019-06-02 01:17:07 +02:00
|
|
|
|
2022-05-28 16:29:12 +02:00
|
|
|
var dummyReg = new ComponentRegistration(
|
|
|
|
|
"Dummy",
|
|
|
|
|
typeof(DummyComponent),
|
|
|
|
|
CompIdx.Index<DummyComponent>());
|
2019-06-02 01:17:07 +02:00
|
|
|
|
|
|
|
|
var componentFactory = new Mock<IComponentFactory>();
|
|
|
|
|
componentFactory.Setup(p => p.GetComponent<DummyComponent>()).Returns(new DummyComponent());
|
2022-05-28 16:29:12 +02:00
|
|
|
componentFactory.Setup(p => p.GetRegistration(It.IsAny<DummyComponent>())).Returns(dummyReg);
|
2024-01-06 04:24:42 -05:00
|
|
|
componentFactory.Setup(p => p.GetAllRegistrations()).Returns(new[] { dummyReg });
|
2023-07-01 08:11:01 -07:00
|
|
|
componentFactory.Setup(p => p.GetAllRefTypes()).Returns(new[] { CompIdx.Index<DummyComponent>() });
|
2019-06-02 01:17:07 +02:00
|
|
|
|
|
|
|
|
IoCManager.RegisterInstance<IComponentFactory>(componentFactory.Object);
|
|
|
|
|
|
|
|
|
|
IoCManager.BuildGraph();
|
2021-09-28 13:35:29 +02:00
|
|
|
_entityManager = IoCManager.Resolve<IEntityManager>();
|
|
|
|
|
_entityManager.Initialize();
|
2019-06-02 01:17:07 +02:00
|
|
|
|
|
|
|
|
// Initialize N entities with one component.
|
|
|
|
|
for (var i = 0; i < N; i++)
|
|
|
|
|
{
|
2021-09-28 13:35:29 +02:00
|
|
|
var entity = _entityManager.SpawnEntity(null, EntityCoordinates.Invalid);
|
|
|
|
|
_entityManager.AddComponent<DummyComponent>(entity);
|
2019-06-02 01:17:07 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Benchmark]
|
|
|
|
|
public int Run()
|
|
|
|
|
{
|
|
|
|
|
var count = 0;
|
|
|
|
|
|
2021-09-28 13:35:29 +02:00
|
|
|
foreach (var _ in _entityManager.EntityQuery<DummyComponent>(true))
|
2019-06-02 01:17:07 +02:00
|
|
|
{
|
|
|
|
|
count += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-22 04:43:47 -04:00
|
|
|
[Benchmark]
|
|
|
|
|
public int Noop()
|
|
|
|
|
{
|
|
|
|
|
var count = 0;
|
|
|
|
|
|
2021-09-28 13:35:29 +02:00
|
|
|
_entityManager.TryGetComponent(default, out DummyComponent _);
|
2020-06-22 04:43:47 -04:00
|
|
|
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-22 18:14:33 -07:00
|
|
|
private sealed partial class DummyComponent : Component
|
2019-06-02 01:17:07 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|