| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459 |
- #include <gtest/gtest.h>
- #include <registry.hpp>
- #include <iostream>
- #include <cstddef>
- #include <chrono>
- #include <vector>
- struct Position {
- uint64_t x;
- uint64_t y;
- };
- struct Velocity {
- uint64_t x;
- uint64_t y;
- };
- template<std::size_t>
- struct Comp {};
- struct Timer final {
- Timer(): start{std::chrono::system_clock::now()} {}
- void elapsed() {
- auto now = std::chrono::system_clock::now();
- std::cout << std::chrono::duration<double>(now - start).count() << " seconds" << std::endl;
- }
- private:
- std::chrono::time_point<std::chrono::system_clock> start;
- };
- TEST(DefaultRegistry, Construct) {
- using registry_type = entt::DefaultRegistry<Position, Velocity>;
- registry_type registry;
- std::cout << "Constructing 10000000 entities" << std::endl;
- Timer timer;
- for (uint64_t i = 0; i < 10000000L; i++) {
- registry.create();
- }
- timer.elapsed();
- registry.reset();
- }
- TEST(DefaultRegistry, Destroy) {
- using registry_type = entt::DefaultRegistry<Position, Velocity>;
- registry_type registry;
- std::vector<registry_type::entity_type> entities{};
- std::cout << "Destroying 10000000 entities" << std::endl;
- for (uint64_t i = 0; i < 10000000L; i++) {
- entities.push_back(registry.create());
- }
- Timer timer;
- for (auto entity: entities) {
- registry.destroy(entity);
- }
- timer.elapsed();
- }
- TEST(DefaultRegistry, IterateCreateDeleteSingleComponent) {
- using registry_type = entt::DefaultRegistry<Position, Velocity>;
- registry_type registry;
- std::cout << "Looping 10000 times creating and deleting a random number of entities" << std::endl;
- Timer timer;
- for(int i = 0; i < 10000; i++) {
- for(int j = 0; j < 10000; j++) {
- registry.create<Position>();
- }
- auto view = registry.view<Position>();
- for(auto entity: view) {
- if(rand() % 2 == 0) {
- registry.destroy(entity);
- }
- }
- }
- timer.elapsed();
- registry.reset();
- }
- TEST(DefaultRegistry, IterateSingleComponent10M) {
- using registry_type = entt::DefaultRegistry<Position, Velocity>;
- registry_type registry;
- std::cout << "Iterating over 10000000 entities, one component" << std::endl;
- for (uint64_t i = 0; i < 10000000L; i++) {
- registry.create<Position>();
- }
- Timer timer;
- auto view = registry.view<Position>();
- for(auto entity: view) {
- auto &position = registry.get<Position>(entity);
- (void)position;
- }
- timer.elapsed();
- registry.reset();
- }
- TEST(DefaultRegistry, IterateTwoComponents10M) {
- using registry_type = entt::DefaultRegistry<Position, Velocity>;
- registry_type registry;
- std::cout << "Iterating over 10000000 entities, two components" << std::endl;
- for (uint64_t i = 0; i < 10000000L; i++) {
- registry.create<Position, Velocity>();
- }
- Timer timer;
- auto view = registry.view<Position, Velocity>();
- for(auto entity: view) {
- auto &position = registry.get<Position>(entity);
- auto &velocity = registry.get<Velocity>(entity);
- (void)position;
- (void)velocity;
- }
- timer.elapsed();
- registry.reset();
- }
- TEST(DefaultRegistry, IterateTwoComponents10MHalf) {
- using registry_type = entt::DefaultRegistry<Position, Velocity>;
- registry_type registry;
- std::cout << "Iterating over 10000000 entities, two components, half of the entities have all the components" << std::endl;
- for (uint64_t i = 0; i < 10000000L; i++) {
- auto entity = registry.create<Velocity>();
- if(i % 2) { registry.assign<Position>(entity); }
- }
- Timer timer;
- auto view = registry.view<Position, Velocity>();
- for(auto entity: view) {
- auto &position = registry.get<Position>(entity);
- auto &velocity = registry.get<Velocity>(entity);
- (void)position;
- (void)velocity;
- }
- timer.elapsed();
- registry.reset();
- }
- TEST(DefaultRegistry, IterateTwoComponents10MOne) {
- using registry_type = entt::DefaultRegistry<Position, Velocity>;
- registry_type registry;
- std::cout << "Iterating over 10000000 entities, two components, only one entity has all the components" << std::endl;
- for (uint64_t i = 0; i < 10000000L; i++) {
- auto entity = registry.create<Velocity>();
- if(i == 5000000L) { registry.assign<Position>(entity); }
- }
- Timer timer;
- auto view = registry.view<Position, Velocity>();
- for(auto entity: view) {
- auto &position = registry.get<Position>(entity);
- auto &velocity = registry.get<Velocity>(entity);
- (void)position;
- (void)velocity;
- }
- timer.elapsed();
- registry.reset();
- }
- TEST(DefaultRegistry, IterateSingleComponent50M) {
- using registry_type = entt::DefaultRegistry<Position, Velocity>;
- registry_type registry;
- std::cout << "Iterating over 50000000 entities, one component" << std::endl;
- for (uint64_t i = 0; i < 50000000L; i++) {
- registry.create<Position>();
- }
- Timer timer;
- auto view = registry.view<Position>();
- for(auto entity: view) {
- auto &position = registry.get<Position>(entity);
- (void)position;
- }
- timer.elapsed();
- registry.reset();
- }
- TEST(DefaultRegistry, IterateTwoComponents50M) {
- using registry_type = entt::DefaultRegistry<Position, Velocity>;
- registry_type registry;
- std::cout << "Iterating over 50000000 entities, two components" << std::endl;
- for (uint64_t i = 0; i < 50000000L; i++) {
- registry.create<Position, Velocity>();
- }
- Timer timer;
- auto view = registry.view<Position, Velocity>();
- for(auto entity: view) {
- auto &position = registry.get<Position>(entity);
- auto &velocity = registry.get<Velocity>(entity);
- (void)position;
- (void)velocity;
- }
- timer.elapsed();
- registry.reset();
- }
- TEST(DefaultRegistry, IterateFiveComponents10M) {
- using registry_type = entt::DefaultRegistry<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>;
- registry_type registry;
- std::cout << "Iterating over 10000000 entities, five components" << std::endl;
- for (uint64_t i = 0; i < 10000000L; i++) {
- registry.create<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>();
- }
- Timer timer;
- auto view = registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>();
- for(auto entity: view) {
- auto &position = registry.get<Position>(entity);
- auto &velocity = registry.get<Velocity>(entity);
- auto &comp1 = registry.get<Comp<1>>(entity);
- auto &comp2 = registry.get<Comp<2>>(entity);
- auto &comp3 = registry.get<Comp<3>>(entity);
- (void)position;
- (void)velocity;
- (void)comp1;
- (void)comp2;
- (void)comp3;
- }
- timer.elapsed();
- registry.reset();
- }
- TEST(DefaultRegistry, IterateTenComponents10M) {
- using registry_type = entt::DefaultRegistry<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>;
- registry_type registry;
- std::cout << "Iterating over 10000000 entities, ten components" << std::endl;
- for (uint64_t i = 0; i < 10000000L; i++) {
- registry.create<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
- }
- Timer timer;
- auto view = registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
- for(auto entity: view) {
- auto &position = registry.get<Position>(entity);
- auto &velocity = registry.get<Velocity>(entity);
- auto &comp1 = registry.get<Comp<1>>(entity);
- auto &comp2 = registry.get<Comp<2>>(entity);
- auto &comp3 = registry.get<Comp<3>>(entity);
- auto &comp4 = registry.get<Comp<4>>(entity);
- auto &comp5 = registry.get<Comp<5>>(entity);
- auto &comp6 = registry.get<Comp<6>>(entity);
- auto &comp7 = registry.get<Comp<7>>(entity);
- auto &comp8 = registry.get<Comp<8>>(entity);
- (void)position;
- (void)velocity;
- (void)comp1;
- (void)comp2;
- (void)comp3;
- (void)comp4;
- (void)comp5;
- (void)comp6;
- (void)comp7;
- (void)comp8;
- }
- timer.elapsed();
- registry.reset();
- }
- TEST(DefaultRegistry, IterateTenComponents10MHalf) {
- using registry_type = entt::DefaultRegistry<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>;
- registry_type registry;
- std::cout << "Iterating over 10000000 entities, ten components, half of the entities have all the components" << std::endl;
- for (uint64_t i = 0; i < 10000000L; i++) {
- auto entity = registry.create<Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
- if(i % 2) { registry.assign<Position>(entity); }
- }
- Timer timer;
- auto view = registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
- for(auto entity: view) {
- auto &position = registry.get<Position>(entity);
- auto &velocity = registry.get<Velocity>(entity);
- auto &comp1 = registry.get<Comp<1>>(entity);
- auto &comp2 = registry.get<Comp<2>>(entity);
- auto &comp3 = registry.get<Comp<3>>(entity);
- auto &comp4 = registry.get<Comp<4>>(entity);
- auto &comp5 = registry.get<Comp<5>>(entity);
- auto &comp6 = registry.get<Comp<6>>(entity);
- auto &comp7 = registry.get<Comp<7>>(entity);
- auto &comp8 = registry.get<Comp<8>>(entity);
- (void)position;
- (void)velocity;
- (void)comp1;
- (void)comp2;
- (void)comp3;
- (void)comp4;
- (void)comp5;
- (void)comp6;
- (void)comp7;
- (void)comp8;
- }
- timer.elapsed();
- registry.reset();
- }
- TEST(DefaultRegistry, IterateTenComponents10MOne) {
- using registry_type = entt::DefaultRegistry<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>;
- registry_type registry;
- std::cout << "Iterating over 10000000 entities, ten components, only one entity has all the components" << std::endl;
- for (uint64_t i = 0; i < 10000000L; i++) {
- auto entity = registry.create<Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
- if(i == 5000000L) { registry.assign<Position>(entity); }
- }
- Timer timer;
- auto view = registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
- for(auto entity: view) {
- auto &position = registry.get<Position>(entity);
- auto &velocity = registry.get<Velocity>(entity);
- auto &comp1 = registry.get<Comp<1>>(entity);
- auto &comp2 = registry.get<Comp<2>>(entity);
- auto &comp3 = registry.get<Comp<3>>(entity);
- auto &comp4 = registry.get<Comp<4>>(entity);
- auto &comp5 = registry.get<Comp<5>>(entity);
- auto &comp6 = registry.get<Comp<6>>(entity);
- auto &comp7 = registry.get<Comp<7>>(entity);
- auto &comp8 = registry.get<Comp<8>>(entity);
- (void)position;
- (void)velocity;
- (void)comp1;
- (void)comp2;
- (void)comp3;
- (void)comp4;
- (void)comp5;
- (void)comp6;
- (void)comp7;
- (void)comp8;
- }
- timer.elapsed();
- registry.reset();
- }
- TEST(DefaultRegistry, SortSingle) {
- using registry_type = entt::DefaultRegistry<Position>;
- registry_type registry;
- std::vector<registry_type::entity_type> entities{};
- std::cout << "Sort 10000000 entities" << std::endl;
- for (uint64_t i = 0; i < 10000000L; i++) {
- auto entity = registry.create();
- entities.push_back(entity);
- registry.assign<Position>(entity, i, i);
- }
- Timer timer;
- registry.sort<Position>([](const auto &lhs, const auto &rhs) {
- return lhs.x < rhs.x && lhs.y < rhs.y;
- });
- timer.elapsed();
- }
- TEST(DefaultRegistry, SortMulti) {
- using registry_type = entt::DefaultRegistry<Position, Velocity>;
- registry_type registry;
- std::vector<registry_type::entity_type> entities{};
- std::cout << "Sort 10000000 entities" << std::endl;
- for (uint64_t i = 0; i < 10000000L; i++) {
- auto entity = registry.create();
- entities.push_back(entity);
- registry.assign<Position>(entity, i, i);
- registry.assign<Velocity>(entity, i, i);
- }
- registry.sort<Position>([](const auto &lhs, const auto &rhs) {
- return lhs.x < rhs.x && lhs.y < rhs.y;
- });
- Timer timer;
- registry.sort<Velocity, Position>();
- timer.elapsed();
- }
|