benchmark.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. #include <gtest/gtest.h>
  2. #include <registry.hpp>
  3. #include <iostream>
  4. #include <cstddef>
  5. #include <chrono>
  6. #include <vector>
  7. struct Position {
  8. uint64_t x;
  9. uint64_t y;
  10. };
  11. struct Velocity {
  12. uint64_t x;
  13. uint64_t y;
  14. };
  15. struct Comp1 {};
  16. struct Comp2 {};
  17. struct Comp3 {};
  18. struct Comp4 {};
  19. struct Comp5 {};
  20. struct Comp6 {};
  21. struct Comp7 {};
  22. struct Comp8 {};
  23. struct Timer final {
  24. Timer(): start{std::chrono::system_clock::now()} {}
  25. void elapsed() {
  26. auto now = std::chrono::system_clock::now();
  27. std::cout << std::chrono::duration<double>(now - start).count() << " seconds" << std::endl;
  28. }
  29. private:
  30. std::chrono::time_point<std::chrono::system_clock> start;
  31. };
  32. TEST(DefaultRegistry, Construct) {
  33. using registry_type = entt::DefaultRegistry<Position, Velocity>;
  34. registry_type registry;
  35. std::cout << "Constructing 10000000 entities" << std::endl;
  36. Timer timer;
  37. for (uint64_t i = 0; i < 10000000L; i++) {
  38. registry.create();
  39. }
  40. timer.elapsed();
  41. registry.reset();
  42. }
  43. TEST(DefaultRegistry, Destroy) {
  44. using registry_type = entt::DefaultRegistry<Position, Velocity>;
  45. registry_type registry;
  46. std::vector<registry_type::entity_type> entities{};
  47. std::cout << "Destroying 10000000 entities" << std::endl;
  48. for (uint64_t i = 0; i < 10000000L; i++) {
  49. entities.push_back(registry.create());
  50. }
  51. Timer timer;
  52. for (auto entity: entities) {
  53. registry.destroy(entity);
  54. }
  55. timer.elapsed();
  56. }
  57. TEST(DefaultRegistry, IterateCreateDeleteSingleComponent) {
  58. using registry_type = entt::DefaultRegistry<Position, Velocity>;
  59. registry_type registry;
  60. std::cout << "Looping 10000 times creating and deleting a random number of entities" << std::endl;
  61. Timer timer;
  62. for(int i = 0; i < 10000; i++) {
  63. for(int j = 0; j < 10000; j++) {
  64. registry.create<Position>();
  65. }
  66. auto view = registry.view<Position>();
  67. for(auto entity: view) {
  68. if(rand() % 2 == 0) {
  69. registry.destroy(entity);
  70. }
  71. }
  72. }
  73. timer.elapsed();
  74. registry.reset();
  75. }
  76. TEST(DefaultRegistry, IterateSingleComponent10M) {
  77. using registry_type = entt::DefaultRegistry<Position, Velocity>;
  78. registry_type registry;
  79. std::cout << "Iterating over 10000000 entities, one component" << std::endl;
  80. for (uint64_t i = 0; i < 10000000L; i++) {
  81. registry.create<Position>();
  82. }
  83. Timer timer;
  84. auto view = registry.view<Position>();
  85. for(auto entity: view) {
  86. auto &position = registry.get<Position>(entity);
  87. (void)position;
  88. }
  89. timer.elapsed();
  90. registry.reset();
  91. }
  92. TEST(DefaultRegistry, IterateTwoComponents10M) {
  93. using registry_type = entt::DefaultRegistry<Position, Velocity>;
  94. registry_type registry;
  95. std::cout << "Iterating over 10000000 entities, two components" << std::endl;
  96. for (uint64_t i = 0; i < 10000000L; i++) {
  97. registry.create<Position, Velocity>();
  98. }
  99. Timer timer;
  100. auto view = registry.view<Position, Velocity>();
  101. for(auto entity: view) {
  102. auto &position = registry.get<Position>(entity);
  103. auto &velocity = registry.get<Velocity>(entity);
  104. (void)position;
  105. (void)velocity;
  106. }
  107. timer.elapsed();
  108. registry.reset();
  109. }
  110. TEST(DefaultRegistry, IterateTwoComponents10MHalf) {
  111. using registry_type = entt::DefaultRegistry<Position, Velocity>;
  112. registry_type registry;
  113. std::cout << "Iterating over 10000000 entities, two components, half of the entities have all the components" << std::endl;
  114. for (uint64_t i = 0; i < 10000000L; i++) {
  115. auto entity = registry.create<Velocity>();
  116. if(i % 2) { registry.assign<Position>(entity); }
  117. }
  118. Timer timer;
  119. auto view = registry.view<Position, Velocity>();
  120. for(auto entity: view) {
  121. auto &position = registry.get<Position>(entity);
  122. auto &velocity = registry.get<Velocity>(entity);
  123. (void)position;
  124. (void)velocity;
  125. }
  126. timer.elapsed();
  127. registry.reset();
  128. }
  129. TEST(DefaultRegistry, IterateTwoComponents10MOne) {
  130. using registry_type = entt::DefaultRegistry<Position, Velocity>;
  131. registry_type registry;
  132. std::cout << "Iterating over 10000000 entities, two components, only one entity has all the components" << std::endl;
  133. for (uint64_t i = 0; i < 10000000L; i++) {
  134. auto entity = registry.create<Velocity>();
  135. if(i == 5000000L) { registry.assign<Position>(entity); }
  136. }
  137. Timer timer;
  138. auto view = registry.view<Position, Velocity>();
  139. for(auto entity: view) {
  140. auto &position = registry.get<Position>(entity);
  141. auto &velocity = registry.get<Velocity>(entity);
  142. (void)position;
  143. (void)velocity;
  144. }
  145. timer.elapsed();
  146. registry.reset();
  147. }
  148. TEST(DefaultRegistry, IterateSingleComponent50M) {
  149. using registry_type = entt::DefaultRegistry<Position, Velocity>;
  150. registry_type registry;
  151. std::cout << "Iterating over 50000000 entities, one component" << std::endl;
  152. for (uint64_t i = 0; i < 50000000L; i++) {
  153. registry.create<Position>();
  154. }
  155. Timer timer;
  156. auto view = registry.view<Position>();
  157. for(auto entity: view) {
  158. auto &position = registry.get<Position>(entity);
  159. (void)position;
  160. }
  161. timer.elapsed();
  162. registry.reset();
  163. }
  164. TEST(DefaultRegistry, IterateTwoComponents50M) {
  165. using registry_type = entt::DefaultRegistry<Position, Velocity>;
  166. registry_type registry;
  167. std::cout << "Iterating over 50000000 entities, two components" << std::endl;
  168. for (uint64_t i = 0; i < 50000000L; i++) {
  169. registry.create<Position, Velocity>();
  170. }
  171. Timer timer;
  172. auto view = registry.view<Position, Velocity>();
  173. for(auto entity: view) {
  174. auto &position = registry.get<Position>(entity);
  175. auto &velocity = registry.get<Velocity>(entity);
  176. (void)position;
  177. (void)velocity;
  178. }
  179. timer.elapsed();
  180. registry.reset();
  181. }
  182. TEST(DefaultRegistry, IterateFiveComponents10M) {
  183. using registry_type = entt::DefaultRegistry<Position, Velocity, Comp1, Comp2, Comp3>;
  184. registry_type registry;
  185. std::cout << "Iterating over 10000000 entities, five components" << std::endl;
  186. for (uint64_t i = 0; i < 10000000L; i++) {
  187. registry.create<Position, Velocity, Comp1, Comp2, Comp3>();
  188. }
  189. Timer timer;
  190. auto view = registry.view<Position, Velocity, Comp1, Comp2, Comp3>();
  191. for(auto entity: view) {
  192. auto &position = registry.get<Position>(entity);
  193. auto &velocity = registry.get<Velocity>(entity);
  194. auto &comp1 = registry.get<Comp1>(entity);
  195. auto &comp2 = registry.get<Comp2>(entity);
  196. auto &comp3 = registry.get<Comp3>(entity);
  197. (void)position;
  198. (void)velocity;
  199. (void)comp1;
  200. (void)comp2;
  201. (void)comp3;
  202. }
  203. timer.elapsed();
  204. registry.reset();
  205. }
  206. TEST(DefaultRegistry, IterateTenComponents10M) {
  207. using registry_type = entt::DefaultRegistry<Position, Velocity, Comp1, Comp2, Comp3, Comp4, Comp5, Comp6, Comp7, Comp8>;
  208. registry_type registry;
  209. std::cout << "Iterating over 10000000 entities, ten components" << std::endl;
  210. for (uint64_t i = 0; i < 10000000L; i++) {
  211. registry.create<Position, Velocity, Comp1, Comp2, Comp3, Comp4, Comp5, Comp6, Comp7, Comp8>();
  212. }
  213. Timer timer;
  214. auto view = registry.view<Position, Velocity, Comp1, Comp2, Comp3, Comp4, Comp5, Comp6, Comp7, Comp8>();
  215. for(auto entity: view) {
  216. auto &position = registry.get<Position>(entity);
  217. auto &velocity = registry.get<Velocity>(entity);
  218. auto &comp1 = registry.get<Comp1>(entity);
  219. auto &comp2 = registry.get<Comp2>(entity);
  220. auto &comp3 = registry.get<Comp3>(entity);
  221. auto &comp4 = registry.get<Comp4>(entity);
  222. auto &comp5 = registry.get<Comp5>(entity);
  223. auto &comp6 = registry.get<Comp6>(entity);
  224. auto &comp7 = registry.get<Comp7>(entity);
  225. auto &comp8 = registry.get<Comp8>(entity);
  226. (void)position;
  227. (void)velocity;
  228. (void)comp1;
  229. (void)comp2;
  230. (void)comp3;
  231. (void)comp4;
  232. (void)comp5;
  233. (void)comp6;
  234. (void)comp7;
  235. (void)comp8;
  236. }
  237. timer.elapsed();
  238. registry.reset();
  239. }
  240. TEST(DefaultRegistry, IterateTenComponents10MHalf) {
  241. using registry_type = entt::DefaultRegistry<Position, Velocity, Comp1, Comp2, Comp3, Comp4, Comp5, Comp6, Comp7, Comp8>;
  242. registry_type registry;
  243. std::cout << "Iterating over 10000000 entities, ten components, half of the entities have all the components" << std::endl;
  244. for (uint64_t i = 0; i < 10000000L; i++) {
  245. auto entity = registry.create<Velocity, Comp1, Comp2, Comp3, Comp4, Comp5, Comp6, Comp7, Comp8>();
  246. if(i % 2) { registry.assign<Position>(entity); }
  247. }
  248. Timer timer;
  249. auto view = registry.view<Position, Velocity, Comp1, Comp2, Comp3, Comp4, Comp5, Comp6, Comp7, Comp8>();
  250. for(auto entity: view) {
  251. auto &position = registry.get<Position>(entity);
  252. auto &velocity = registry.get<Velocity>(entity);
  253. auto &comp1 = registry.get<Comp1>(entity);
  254. auto &comp2 = registry.get<Comp2>(entity);
  255. auto &comp3 = registry.get<Comp3>(entity);
  256. auto &comp4 = registry.get<Comp4>(entity);
  257. auto &comp5 = registry.get<Comp5>(entity);
  258. auto &comp6 = registry.get<Comp6>(entity);
  259. auto &comp7 = registry.get<Comp7>(entity);
  260. auto &comp8 = registry.get<Comp8>(entity);
  261. (void)position;
  262. (void)velocity;
  263. (void)comp1;
  264. (void)comp2;
  265. (void)comp3;
  266. (void)comp4;
  267. (void)comp5;
  268. (void)comp6;
  269. (void)comp7;
  270. (void)comp8;
  271. }
  272. timer.elapsed();
  273. registry.reset();
  274. }
  275. TEST(DefaultRegistry, IterateTenComponents10MOne) {
  276. using registry_type = entt::DefaultRegistry<Position, Velocity, Comp1, Comp2, Comp3, Comp4, Comp5, Comp6, Comp7, Comp8>;
  277. registry_type registry;
  278. std::cout << "Iterating over 10000000 entities, ten components, only one entity has all the components" << std::endl;
  279. for (uint64_t i = 0; i < 10000000L; i++) {
  280. auto entity = registry.create<Velocity, Comp1, Comp2, Comp3, Comp4, Comp5, Comp6, Comp7, Comp8>();
  281. if(i == 5000000L) { registry.assign<Position>(entity); }
  282. }
  283. Timer timer;
  284. auto view = registry.view<Position, Velocity, Comp1, Comp2, Comp3, Comp4, Comp5, Comp6, Comp7, Comp8>();
  285. for(auto entity: view) {
  286. auto &position = registry.get<Position>(entity);
  287. auto &velocity = registry.get<Velocity>(entity);
  288. auto &comp1 = registry.get<Comp1>(entity);
  289. auto &comp2 = registry.get<Comp2>(entity);
  290. auto &comp3 = registry.get<Comp3>(entity);
  291. auto &comp4 = registry.get<Comp4>(entity);
  292. auto &comp5 = registry.get<Comp5>(entity);
  293. auto &comp6 = registry.get<Comp6>(entity);
  294. auto &comp7 = registry.get<Comp7>(entity);
  295. auto &comp8 = registry.get<Comp8>(entity);
  296. (void)position;
  297. (void)velocity;
  298. (void)comp1;
  299. (void)comp2;
  300. (void)comp3;
  301. (void)comp4;
  302. (void)comp5;
  303. (void)comp6;
  304. (void)comp7;
  305. (void)comp8;
  306. }
  307. timer.elapsed();
  308. registry.reset();
  309. }