diff --git a/include/opendht/indexation/pht.h b/include/opendht/indexation/pht.h index 70a03eeec847f85c092a200077b1ce44e1faa99d..c4013c5a1a612fc1caf65207fca62e9e436a9907 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 f6fcc52251734f12bb028697da5cf8e58205ce5b..2352481757154a3511c2873cfce6d2978d69eaec 100755 --- a/python/tools/benchmark.py +++ b/python/tools/benchmark.py @@ -160,6 +160,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 2c65563e92da824c44a11aed7188d3679f6870b7..67cdd6a4ea67c8ee6687745159d0a27ee7e28e14 100644 --- a/python/tools/dht/tests.py +++ b/python/tools/dht/tests.py @@ -33,6 +33,24 @@ Mbit_format = FuncFormatter(lambda x, pos: '%1.1f' % (x*1024**-2) + 'Mb') def random_hash(): return InfoHash(''.join(random.SystemRandom().choice(string.hexdigits) for _ in range(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(). @@ -160,7 +178,6 @@ class FeatureTest(object): def run(self): raise NotImplementedError('This method must be implemented.') - ################################## # PHT # ################################## @@ -189,6 +206,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() @@ -278,14 +296,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__() @@ -296,7 +318,6 @@ class PhtTest(FeatureTest): DhtNetwork.log(all_entries[p]) PhtTest.drawTrie(all_entries) - ################################## # DHT # ################################## @@ -372,7 +393,6 @@ class DhtFeatureTest(FeatureTest): for n in DhtFeatureTest.foreignNodes: nodes.add(n) - class PersistenceTest(DhtFeatureTest): """ This tests persistence of data on the network. @@ -676,7 +696,7 @@ class PersistenceTest(DhtFeatureTest): hashes = [] - # Generating considerable amount of values of size 1KB. + # Generating considerable amount of values of size 1KB. VALUE_SIZE = 1024 NUM_VALUES = self._num_values if self._num_values else 50 values = [Value(''.join(random.choice(string.hexdigits) for _ in range(VALUE_SIZE)).encode()) for _ in range(NUM_VALUES)] diff --git a/tools/dhtnode.cpp b/tools/dhtnode.cpp index 0410397f1a60c64f1413cc4e32fef78f28565201..da075f503fd224cc87b9d5fbf770dfaf692f6550 100644 --- a/tools/dhtnode.cpp +++ b/tools/dhtnode.cpp @@ -259,10 +259,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 ); }