]> code.bitgloo.com Git - clyne/entityx.git/commitdiff
Add some benchmarks.
authorAlec Thomas <alec@swapoff.org>
Tue, 12 Mar 2013 02:03:14 +0000 (22:03 -0400)
committerAlec Thomas <alec@swapoff.org>
Tue, 12 Mar 2013 02:03:14 +0000 (22:03 -0400)
CMakeLists.txt
README.md
entityx/Benchmarks_test.cc [new file with mode: 0644]

index 7ee2745468f64a495834d5cd0a84ccc159d78c43..bfc22aa5ccd0f94ff45073db2c70fe99cfa9a9a0 100644 (file)
@@ -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")
index 4ac3b47b60074b0fe7c21d595cc33fb362e2a8ed..373cd4304e846aa0317038024d7be1c525c62e6a 100644 (file)
--- 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> 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 (file)
index 0000000..625af29
--- /dev/null
@@ -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();
+  }
+}
+