1 module tests.perf;
2 
3 import tests.runner;
4 
5 import bubel.ecs.core;
6 import bubel.ecs.manager;
7 import bubel.ecs.entity;
8 
9 version(GNU)
10 {
11 	pragma(inline, true) T[n] staticArray(T, size_t n)(auto ref T[n] a)
12 	{
13 		return a;
14 	}
15 }
16 else import std.array : staticArray;
17 
18 import core.stdc.stdio;
19 
20 struct CLong
21 {
22     mixin ECS.Component;
23 
24     alias value this;
25 
26     long value = 10;
27 }
28 
29 struct CShort
30 {
31     mixin ECS.Component;
32 
33     alias value this;
34 
35     short value = 12;
36 }
37 
38 struct CInt
39 {
40     mixin ECS.Component;
41 
42     alias value this;
43 
44     int value = 10;
45 }
46 
47 struct CUInt
48 {
49     mixin ECS.Component;
50 
51     alias value this;
52 
53     uint value = 12;
54 }
55 
56 struct CBig
57 {
58     mixin ECS.Component;
59     uint[32] data;
60 }
61 
62 EntityTemplate* tmpl;
63 
64 void beforeEveryTest()
65 {
66     gEntityManager.initialize(0);
67 
68     gEntityManager.beginRegister();
69 
70     gEntityManager.registerComponent!CLong;
71     gEntityManager.registerComponent!CShort;
72     gEntityManager.registerComponent!CInt;
73     gEntityManager.registerComponent!CUInt;
74     gEntityManager.registerComponent!CBig;
75 
76     gEntityManager.endRegister();
77     tmpl = null;
78 }
79 
80 void afterEveryTest()
81 {
82     if(tmpl)gEntityManager.freeTemplate(tmpl);
83     tmpl = null;
84     gEntityManager.destroy();
85 }
86 
87 void smallTmpl()
88 {
89     tmpl = gEntityManager.allocateTemplate([becsID!CShort].staticArray);
90 }
91 
92 void bigTmpl()
93 {
94     tmpl = gEntityManager.allocateTemplate([becsID!CBig].staticArray);
95 }
96 
97 void multiSmallTmpl()
98 {
99     tmpl = gEntityManager.allocateTemplate([becsID!CShort, becsID!CLong, becsID!CInt, becsID!CUInt].staticArray);
100 }
101 
102 void multiBigTmpl()
103 {
104     tmpl = gEntityManager.allocateTemplate([becsID!CLong, becsID!CInt, becsID!CUInt, becsID!CBig].staticArray);
105 }
106 
107 @("AddEntities100k1comp2b")  @(before, &smallTmpl)
108 unittest
109 {
110     foreach(i; 0..100_000)gEntityManager.addEntity(tmpl);
111 }
112 
113 @("AddEntities100k1comp128b")  @(before, &bigTmpl)
114 unittest
115 {
116     foreach(i; 0..100_000)gEntityManager.addEntity(tmpl);
117 }
118 
119 @("AddEntities100k4comp18b")  @(before, &multiSmallTmpl)
120 unittest
121 {
122     foreach(i; 0..100_000)gEntityManager.addEntity(tmpl);
123 }
124 
125 @("AddEntities100k4comp144b")  @(before, &multiBigTmpl)
126 unittest
127 {
128     foreach(i; 0..100_000)gEntityManager.addEntity(tmpl);
129 }
130 
131 void allocDealloc100k()
132 {
133     foreach(i; 0..100_000)gEntityManager.addEntity(tmpl);
134     gEntityManager.commit();
135     foreach(i; 0..100_000)gEntityManager.removeEntity(EntityID(i + 1,0));
136     gEntityManager.commit();
137 }
138 
139 void smallTmplPreAlloc()
140 {
141     tmpl = gEntityManager.allocateTemplate([becsID!CShort].staticArray);
142     allocDealloc100k();
143 }
144 
145 void bigTmplPreAlloc()
146 {
147     tmpl = gEntityManager.allocateTemplate([becsID!CBig].staticArray);
148     allocDealloc100k();
149 }
150 
151 void multiSmallTmplPreAlloc()
152 {
153     tmpl = gEntityManager.allocateTemplate([becsID!CShort, becsID!CLong, becsID!CInt, becsID!CUInt].staticArray);
154     allocDealloc100k();
155 }
156 
157 void multiBigTmplPreAlloc()
158 {
159     tmpl = gEntityManager.allocateTemplate([becsID!CLong, becsID!CInt, becsID!CUInt, becsID!CBig].staticArray);
160     allocDealloc100k();
161 }
162 
163 @("AddEntities100k1comp2bPreAlloc")  @(before, &smallTmplPreAlloc)
164 unittest
165 {
166     foreach(i; 0..100_000)gEntityManager.addEntity(tmpl);
167 }
168 
169 @("AddEntities100k1comp128bPreAlloc")  @(before, &bigTmplPreAlloc)
170 unittest
171 {
172     foreach(i; 0..100_000)gEntityManager.addEntity(tmpl);
173 }
174 
175 @("AddEntities100k4comp18bPreAlloc")  @(before, &multiSmallTmplPreAlloc)
176 unittest
177 {
178     foreach(i; 0..100_000)gEntityManager.addEntity(tmpl);
179 }
180 
181 @("AddEntities100k4comp144bPreAlloc")  @(before, &multiBigTmplPreAlloc)
182 unittest
183 {
184     foreach(i; 0..100_000)gEntityManager.addEntity(tmpl);
185 }