diff options
-rw-r--r-- | CMakeLists.txt | 3 | ||||
-rw-r--r-- | README.md | 24 | ||||
-rw-r--r-- | entityx/Benchmarks_test.cc | 75 |
3 files changed, 91 insertions, 11 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 7ee2745..bfc22aa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,7 @@ macro(create_test TARGET_NAME SOURCE) entityx glog ${Boost_LIBRARIES} + ${Boost_TIMER_LIBRARY} ${Boost_SIGNALS_LIBRARY} ${GTEST_BOTH_LIBRARIES} ) @@ -75,6 +76,7 @@ include_directories( set(BUILD_TESTING false CACHE BOOL "Enable building of tests") if (BUILD_TESTING) + find_package(Boost 1.48.0 REQUIRED COMPONENTS signals timer system) enable_testing() find_package(GTest REQUIRED) include_directories(${GTEST_INCLUDE_DIRS}) @@ -82,6 +84,7 @@ if (BUILD_TESTING) create_test(component_test entityx/Components_test.cc) create_test(event_test entityx/Event_test.cc) create_test(system_test entityx/System_test.cc) + create_test(benchmarks_test entityx/Benchmarks_test.cc) endif (BUILD_TESTING) file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/entityx/*.h") @@ -84,10 +84,11 @@ entity.assign(position); To query all entities with a set of components assigned, use ``EntityManager::entities_with_components()``. This method will return only those entities that have *all* of the specified components associated with them, assigning each component pointer to the corresponding component instance: ```c++ -boost::shared_ptr<Position> position; -boost::shared_ptr<Direction> direction; -for (auto entity : entities.entities_with_components(position, direction)) { - // Do things with entity ID, position and direction. +for (auto entity : entities.entities_with_components<Position, Direction>()) { + boost::shared_ptr<Position> position = entity.component<Position>(); + boost::shared_ptr<Direction> direction = entity.component<Direction>(); + + // Do things with entity, position and direction. } ``` @@ -114,11 +115,12 @@ A basic movement system might be implemented with something like the following: ```c++ struct MovementSystem : public System<MovementSystem> { void update(EntityManager &es, EventManager &events, double dt) override { - boost::shared_ptr<Position> position; - boost::shared_ptr<Direction> direction; - for (auto entity : es.entities_with_components(position, direction)) { - position->x += direction->x; - position->y += direction->y; + for (auto entity : es.entities_with_components<Position, Direction>()) { + boost::shared_ptr<Position> position = entity.component<Position>(); + boost::shared_ptr<Direction> direction = entity.component<Direction>(); + + position->x += direction->x * dt; + position->y += direction->y * dt; } } }; @@ -152,8 +154,8 @@ class CollisionSystem : public System<CollisionSystem> { public: void update(EntityManager &es, EventManager &events, double dt) override { boost::shared_ptr<Position> left_position, right_position; - for (auto left_entity : es.entities_with_components(left_position)) { - for (auto right_entity : es.entities_with_components(right_position)) { + for (auto left_entity : es.entities_with_components<Position>()) { + for (auto right_entity : es.entities_with_components<Position>()) { if (collide(left_position, right_position)) { events.emit<Collision>(left_entity, right_entity); } diff --git a/entityx/Benchmarks_test.cc b/entityx/Benchmarks_test.cc new file mode 100644 index 0000000..625af29 --- /dev/null +++ b/entityx/Benchmarks_test.cc @@ -0,0 +1,75 @@ +#include <vector> +#include <gtest/gtest.h> +#include <boost/timer/timer.hpp> +#include "entityx/Entity.h" + +using namespace std; +using namespace entityx; + +class BenchmarksTest : public ::testing::Test { +protected: + BenchmarksTest() : em(ev) {} + + EventManager ev; + EntityManager em; +}; + + +TEST_F(BenchmarksTest, TestCreateEntities) { + boost::timer::auto_cpu_timer t; + + uint64_t count = 10000000L; + for (uint64_t i = 0; i < count; i++) { + em.create(); + } +} + + +TEST_F(BenchmarksTest, TestDestroyEntities) { + uint64_t count = 10000000L; + vector<Entity> entities; + for (uint64_t i = 0; i < count; i++) { + entities.push_back(em.create()); + } + + boost::timer::auto_cpu_timer t; + + for (auto e : entities) { + e.destroy(); + } +} + +struct Listener : public Receiver<Listener> { + void receive(const EntityCreatedEvent &event) {} + void receive(const EntityDestroyedEvent &event) {} +}; + +TEST_F(BenchmarksTest, TestCreateEntitiesWithListener) { + Listener listen; + ev.subscribe<EntityCreatedEvent>(listen); + + boost::timer::auto_cpu_timer t; + uint64_t count = 10000000L; + vector<Entity> entities; + for (uint64_t i = 0; i < count; i++) { + entities.push_back(em.create()); + } +} + +TEST_F(BenchmarksTest, TestDestroyEntitiesWithListener) { + Listener listen; + ev.subscribe<EntityDestroyedEvent>(listen); + + uint64_t count = 10000000L; + vector<Entity> entities; + for (uint64_t i = 0; i < count; i++) { + entities.push_back(em.create()); + } + + boost::timer::auto_cpu_timer t; + + for (auto e : entities) { + e.destroy(); + } +} + |