benchmark.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 Timer final {
  16. Timer(): start{std::chrono::system_clock::now()} {}
  17. void elapsed() {
  18. auto now = std::chrono::system_clock::now();
  19. std::cout << std::chrono::duration<double>(now - start).count() << " seconds" << std::endl;
  20. }
  21. private:
  22. std::chrono::time_point<std::chrono::system_clock> start;
  23. };
  24. using registry_type = entt::DefaultRegistry<Position, Velocity>;
  25. TEST(DefaultRegistry, Construct) {
  26. registry_type registry;
  27. std::cout << "Constructing 10000000 entities" << std::endl;
  28. Timer timer;
  29. for (uint64_t i = 0; i < 10000000L; i++) {
  30. registry.create();
  31. }
  32. timer.elapsed();
  33. registry.reset();
  34. }
  35. TEST(DefaultRegistry, Destroy) {
  36. registry_type registry;
  37. std::vector<registry_type::entity_type> entities{};
  38. std::cout << "Destroying 10000000 entities" << std::endl;
  39. for (uint64_t i = 0; i < 10000000L; i++) {
  40. entities.push_back(registry.create());
  41. }
  42. Timer timer;
  43. for (auto entity: entities) {
  44. registry.destroy(entity);
  45. }
  46. timer.elapsed();
  47. }
  48. TEST(DefaultRegistry, IterateSingleComponent) {
  49. registry_type registry;
  50. std::cout << "Iterating over 10000000 entities, one component" << std::endl;
  51. for (uint64_t i = 0; i < 10000000L; i++) {
  52. registry.create<Position>();
  53. }
  54. Timer timer;
  55. auto view = registry.view<Position>();
  56. for(auto entity: view) {
  57. auto &position = registry.get<Position>(entity);
  58. (void)position;
  59. }
  60. timer.elapsed();
  61. registry.reset();
  62. }
  63. TEST(DefaultRegistry, IterateCreateDeleteSingleComponent) {
  64. registry_type registry;
  65. std::cout << "Looping 10000 times creating and deleting a random number of entities" << std::endl;
  66. Timer timer;
  67. for(int i = 0; i < 10000; i++) {
  68. for(int j = 0; j < 10000; j++) {
  69. registry.create<Position>();
  70. }
  71. auto view = registry.view<Position>();
  72. for(auto entity: view) {
  73. if(rand() % 2 == 0) {
  74. registry.destroy(entity);
  75. }
  76. }
  77. }
  78. timer.elapsed();
  79. registry.reset();
  80. }
  81. TEST(DefaultRegistry, IterateTwoComponents) {
  82. registry_type registry;
  83. std::cout << "Iterating over 10000000 entities, two components" << std::endl;
  84. for (uint64_t i = 0; i < 10000000L; i++) {
  85. registry.create<Position, Velocity>();
  86. }
  87. Timer timer;
  88. auto view = registry.view<Position, Velocity>();
  89. for(auto entity: view) {
  90. auto &position = registry.get<Position>(entity);
  91. auto &velocity = registry.get<Velocity>(entity);
  92. (void)position;
  93. (void)velocity;
  94. }
  95. timer.elapsed();
  96. registry.reset();
  97. }