benchmark.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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, IterateSingleComponent50M) {
  111. using registry_type = entt::DefaultRegistry<Position, Velocity>;
  112. registry_type registry;
  113. std::cout << "Iterating over 50000000 entities, one component" << std::endl;
  114. for (uint64_t i = 0; i < 50000000L; i++) {
  115. registry.create<Position>();
  116. }
  117. Timer timer;
  118. auto view = registry.view<Position>();
  119. for(auto entity: view) {
  120. auto &position = registry.get<Position>(entity);
  121. (void)position;
  122. }
  123. timer.elapsed();
  124. registry.reset();
  125. }
  126. TEST(DefaultRegistry, IterateTwoComponents50M) {
  127. using registry_type = entt::DefaultRegistry<Position, Velocity>;
  128. registry_type registry;
  129. std::cout << "Iterating over 50000000 entities, two components" << std::endl;
  130. for (uint64_t i = 0; i < 50000000L; i++) {
  131. registry.create<Position, Velocity>();
  132. }
  133. Timer timer;
  134. auto view = registry.view<Position, Velocity>();
  135. for(auto entity: view) {
  136. auto &position = registry.get<Position>(entity);
  137. auto &velocity = registry.get<Velocity>(entity);
  138. (void)position;
  139. (void)velocity;
  140. }
  141. timer.elapsed();
  142. registry.reset();
  143. }
  144. TEST(DefaultRegistry, IterateFiveComponents10M) {
  145. using registry_type = entt::DefaultRegistry<Position, Velocity, Comp1, Comp2, Comp3>;
  146. registry_type registry;
  147. std::cout << "Iterating over 10000000 entities, five components" << std::endl;
  148. for (uint64_t i = 0; i < 10000000L; i++) {
  149. registry.create<Position, Velocity, Comp1, Comp2, Comp3>();
  150. }
  151. Timer timer;
  152. auto view = registry.view<Position, Velocity, Comp1, Comp2, Comp3>();
  153. for(auto entity: view) {
  154. auto &position = registry.get<Position>(entity);
  155. auto &velocity = registry.get<Velocity>(entity);
  156. auto &comp1 = registry.get<Comp1>(entity);
  157. auto &comp2 = registry.get<Comp2>(entity);
  158. auto &comp3 = registry.get<Comp3>(entity);
  159. (void)position;
  160. (void)velocity;
  161. (void)comp1;
  162. (void)comp2;
  163. (void)comp3;
  164. }
  165. timer.elapsed();
  166. registry.reset();
  167. }
  168. TEST(DefaultRegistry, IterateTenComponents10M) {
  169. using registry_type = entt::DefaultRegistry<Position, Velocity, Comp1, Comp2, Comp3, Comp4, Comp5, Comp6, Comp7, Comp8>;
  170. registry_type registry;
  171. std::cout << "Iterating over 10000000 entities, ten components" << std::endl;
  172. for (uint64_t i = 0; i < 10000000L; i++) {
  173. registry.create<Position, Velocity, Comp1, Comp2, Comp3, Comp4, Comp5, Comp6, Comp7, Comp8>();
  174. }
  175. Timer timer;
  176. auto view = registry.view<Position, Velocity, Comp1, Comp2, Comp3, Comp4, Comp5, Comp6, Comp7, Comp8>();
  177. for(auto entity: view) {
  178. auto &position = registry.get<Position>(entity);
  179. auto &velocity = registry.get<Velocity>(entity);
  180. auto &comp1 = registry.get<Comp1>(entity);
  181. auto &comp2 = registry.get<Comp2>(entity);
  182. auto &comp3 = registry.get<Comp3>(entity);
  183. auto &comp4 = registry.get<Comp4>(entity);
  184. auto &comp5 = registry.get<Comp5>(entity);
  185. auto &comp6 = registry.get<Comp6>(entity);
  186. auto &comp7 = registry.get<Comp7>(entity);
  187. auto &comp8 = registry.get<Comp8>(entity);
  188. (void)position;
  189. (void)velocity;
  190. (void)comp1;
  191. (void)comp2;
  192. (void)comp3;
  193. (void)comp4;
  194. (void)comp5;
  195. (void)comp6;
  196. (void)comp7;
  197. (void)comp8;
  198. }
  199. timer.elapsed();
  200. registry.reset();
  201. }