From 3b79b1a7dcf3f4a3d49d0297551bd7e3e4cc1689 Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Mon, 11 Mar 2013 22:03:14 -0400 Subject: Add some benchmarks. --- CMakeLists.txt | 3 ++ README.md | 24 ++++++++------- entityx/Benchmarks_test.cc | 75 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 11 deletions(-) create mode 100644 entityx/Benchmarks_test.cc 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") diff --git a/README.md b/README.md index 4ac3b47..373cd43 100644 --- a/README.md +++ b/README.md @@ -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; -boost::shared_ptr 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()) { + boost::shared_ptr position = entity.component(); + boost::shared_ptr direction = entity.component(); + + // 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 { void update(EntityManager &es, EventManager &events, double dt) override { - boost::shared_ptr position; - boost::shared_ptr 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()) { + boost::shared_ptr position = entity.component(); + boost::shared_ptr direction = entity.component(); + + position->x += direction->x * dt; + position->y += direction->y * dt; } } }; @@ -152,8 +154,8 @@ class CollisionSystem : public System { public: void update(EntityManager &es, EventManager &events, double dt) override { boost::shared_ptr 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()) { + for (auto right_entity : es.entities_with_components()) { if (collide(left_position, right_position)) { events.emit(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 +#include +#include +#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 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 { + void receive(const EntityCreatedEvent &event) {} + void receive(const EntityDestroyedEvent &event) {} +}; + +TEST_F(BenchmarksTest, TestCreateEntitiesWithListener) { + Listener listen; + ev.subscribe(listen); + + boost::timer::auto_cpu_timer t; + uint64_t count = 10000000L; + vector entities; + for (uint64_t i = 0; i < count; i++) { + entities.push_back(em.create()); + } +} + +TEST_F(BenchmarksTest, TestDestroyEntitiesWithListener) { + Listener listen; + ev.subscribe(listen); + + uint64_t count = 10000000L; + vector 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(); + } +} + -- cgit v1.2.3