From 041e280bde7369b29700bb9f3eaa3d39d60ed5e6 Mon Sep 17 00:00:00 2001
From: kaldoran <kaldoran@live.fr>
Date: Wed, 13 Apr 2016 10:31:40 -0400
Subject: [PATCH] benchmark: timing pht insert operations

---
 include/opendht/indexation/pht.h | 10 +++++-----
 python/tools/benchmark.py        |  1 +
 python/tools/dht/tests.py        | 33 +++++++++++++++++++++++++-------
 tools/dhtnode.cpp                |  6 +++++-
 4 files changed, 37 insertions(+), 13 deletions(-)

diff --git a/include/opendht/indexation/pht.h b/include/opendht/indexation/pht.h
index 70a03eee..c4013c5a 100644
--- a/include/opendht/indexation/pht.h
+++ b/include/opendht/indexation/pht.h
@@ -200,7 +200,7 @@ private:
                 curr_node = std::make_shared<Node>();
                 root_ = curr_node;
             }
-            
+
             curr_node->last_reply = now;
 
             /* Iterate through all bit of the Blob */
@@ -209,7 +209,7 @@ private:
                 /* According to the bit define which node is the next one */
                 auto& next = ( p.isActivBit(i) ) ? curr_node->right_child : curr_node->left_child;
 
-                /** 
+                /**
                  * If lock, node exists
                  * else create it
                  */
@@ -239,7 +239,7 @@ private:
         int lookup(const Prefix& p) {
             int pos = 0;
             auto now = clock::now(), last_node_time = now;
-            
+
             /* Before lookup remove the useless one [i.e. too old] */
             while ( leaves_.size() > 0 &&  leaves_.begin()->first + NODE_EXPIRE_TIME < now ) {
                 leaves_.erase(leaves_.begin());
@@ -264,11 +264,11 @@ private:
 
             if ( pos > 0 ) {
                 auto to_erase = leaves_.find(last_node_time);
-                if ( to_erase != leaves_.end() ) 
+                if ( to_erase != leaves_.end() )
                     leaves_.erase( to_erase );
 
                 leaves_.emplace( std::move(now), std::move(curr_node) );
-            } 
+            }
 
             return --pos;
         }
diff --git a/python/tools/benchmark.py b/python/tools/benchmark.py
index ad36a40c..36c5e74a 100755
--- a/python/tools/benchmark.py
+++ b/python/tools/benchmark.py
@@ -171,6 +171,7 @@ if __name__ == '__main__':
     featureArgs.add_argument('--pht', action='store_true', default=False,
             help='Launches PHT benchmark test. '\
                     'Available args for "-t" are: insert. '\
+                    'Timer available by adding "timer" to "-o" args'\
                     'Use "-m" option for fixing number of keys to create during the test.')
     featureArgs.add_argument('--data-persistence', action='store_true', default=0,
             help='Launches data persistence benchmark test. '\
diff --git a/python/tools/dht/tests.py b/python/tools/dht/tests.py
index 944a6662..76b6f788 100644
--- a/python/tools/dht/tests.py
+++ b/python/tools/dht/tests.py
@@ -49,6 +49,24 @@ def random_hash():
     """
     return InfoHash(random_str_val(size=40).encode())
 
+def timer(f, *args):
+    """
+    Start a timer which count time taken for execute function f
+
+    @param f : Function to time
+    @type  f : function
+
+    @param args : Arguments of the function f
+    @type  args : list
+
+    @rtype : timer
+    @return : Time taken by the function f
+    """
+    start = time.time()
+    f(*args)
+
+    return time.time() - start
+
 def reset_before_test(featureTestMethod):
     """
     This is a decorator for all test methods needing reset().
@@ -191,7 +209,6 @@ class FeatureTest(object):
     def run(self):
         raise NotImplementedError('This method must be implemented.')
 
-
 ##################################
 #               PHT              #
 ##################################
@@ -220,6 +237,7 @@ class PhtTest(FeatureTest):
         """
         super(PhtTest, self).__init__(test, workbench)
         self._num_keys = opts['num_keys'] if 'num_keys' in opts else 32
+        self._timer = True if 'timer' in opts else False
 
     def _reset(self):
         super(PhtTest, self)._reset()
@@ -309,14 +327,18 @@ class PhtTest(FeatureTest):
         for key in keys:
             PhtTest.key = key
             with FeatureTest.lock:
-                pht.insert(key, IndexValue(random_hash()), PhtTest.insertDoneCb)
+                time_taken = timer(pht.insert, key, IndexValue(random_hash()), PhtTest.insertDoneCb)
+                if self._timer:
+                    DhtNetwork.log('This insert step took : ', time_taken, 'second')
                 FeatureTest.lock.wait()
 
         # Recover entries now that the trie is complete.
         for key in keys:
             PhtTest.key = key
             with FeatureTest.lock:
-                pht.lookup(key, PhtTest.lookupCb, PhtTest.lookupDoneCb)
+                time_taken = timer(pht.lookup, key, PhtTest.lookupCb, PhtTest.lookupDoneCb)
+                if self._timer:
+                    DhtNetwork.log('This lookup step took : ', time_taken, 'second')
                 FeatureTest.lock.wait()
 
             all_entries[PhtTest.prefix] = [e.__str__()
@@ -327,7 +349,6 @@ class PhtTest(FeatureTest):
             DhtNetwork.log(all_entries[p])
         PhtTest.drawTrie(all_entries)
 
-
 ##################################
 #               DHT              #
 ##################################
@@ -403,7 +424,6 @@ class DhtFeatureTest(FeatureTest):
                 for n in DhtFeatureTest.foreignNodes:
                     nodes.add(n)
 
-
 class PersistenceTest(DhtFeatureTest):
     """
     This tests persistence of data on the network.
@@ -748,8 +768,7 @@ class PersistenceTest(DhtFeatureTest):
 
         hashes = []
 
-        # Generating considerable amount of values of size 1KB.
-        # TODO: Utiliser fonction _initialSetOfValues.
+    # Generating considerable amount of values of size 1KB.
         VALUE_SIZE = 1024
         NUM_VALUES = self._num_values if self._num_values else 50
         values = [Value(random_str_val(size=VALUE_SIZE).encode()) for _ in range(NUM_VALUES)]
diff --git a/tools/dhtnode.cpp b/tools/dhtnode.cpp
index ee3199be..0a0b27c8 100644
--- a/tools/dhtnode.cpp
+++ b/tools/dhtnode.cpp
@@ -276,10 +276,14 @@ void cmd_loop(std::shared_ptr<DhtRunner>& dht, std::map<std::string, dht::indexa
                     }
                     std::cout << "Pht::lookup: done." << std::endl;
                 },
-                [](bool ok) {
+                [start](bool ok) {
                     if (not ok) {
                         std::cout << "Pht::lookup: dht Get failed." << std::endl;
                     }
+
+                    auto end = std::chrono::high_resolution_clock::now();
+                    std::cout << "Pht::lookup: took " << print_dt(end-start) << "s)" << std::endl;
+
                 }, exact_match.size() != 0 and exact_match == "false" ? false : true
             );
         }
-- 
GitLab