forked from RMC-14/RMC-14
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntityManagerGetAllComponents.cs
96 lines (79 loc) · 3.02 KB
/
EntityManagerGetAllComponents.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using BenchmarkDotNet.Attributes;
using Moq;
using Robust.Shared.Analyzers;
using Robust.Shared.Exceptions;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Reflection;
namespace Content.Benchmarks
{
[Virtual]
public partial class EntityManagerGetAllComponents
{
private IEntityManager _entityManager;
[Params(5000)] public int N { get; set; }
public static void TestRun()
{
var x = new EntityManagerGetAllComponents
{
N = 500
};
x.Setup();
x.Run();
}
[GlobalSetup]
public void Setup()
{
// Initialize component manager.
IoCManager.InitThread();
IoCManager.Register<IEntityManager, EntityManager>();
IoCManager.Register<IRuntimeLog, RuntimeLog>();
IoCManager.Register<ILogManager, LogManager>();
IoCManager.Register<IDynamicTypeFactory, DynamicTypeFactory>();
IoCManager.Register<IEntitySystemManager, EntitySystemManager>();
IoCManager.RegisterInstance<IReflectionManager>(new Mock<IReflectionManager>().Object);
var dummyReg = new ComponentRegistration(
"Dummy",
typeof(DummyComponent),
CompIdx.Index<DummyComponent>());
var componentFactory = new Mock<IComponentFactory>();
componentFactory.Setup(p => p.GetComponent<DummyComponent>()).Returns(new DummyComponent());
componentFactory.Setup(m => m.GetIndex(typeof(DummyComponent))).Returns(CompIdx.Index<DummyComponent>());
componentFactory.Setup(p => p.GetRegistration(It.IsAny<DummyComponent>())).Returns(dummyReg);
componentFactory.Setup(p => p.GetAllRegistrations()).Returns(new[] { dummyReg });
componentFactory.Setup(p => p.GetAllRefTypes()).Returns(new[] { CompIdx.Index<DummyComponent>() });
IoCManager.RegisterInstance<IComponentFactory>(componentFactory.Object);
IoCManager.BuildGraph();
_entityManager = IoCManager.Resolve<IEntityManager>();
_entityManager.Initialize();
// Initialize N entities with one component.
for (var i = 0; i < N; i++)
{
var entity = _entityManager.SpawnEntity(null, EntityCoordinates.Invalid);
_entityManager.AddComponent<DummyComponent>(entity);
}
}
[Benchmark]
public int Run()
{
var count = 0;
foreach (var _ in _entityManager.EntityQuery<DummyComponent>(true))
{
count += 1;
}
return count;
}
[Benchmark]
public int Noop()
{
var count = 0;
_entityManager.TryGetComponent(default, out DummyComponent _);
return count;
}
private sealed partial class DummyComponent : Component
{
}
}
}