From 6655e99aa35bb28b72206675d9e895f08bb7d9e4 Mon Sep 17 00:00:00 2001 From: Peter Surda Date: Fri, 20 Oct 2017 23:11:33 +0200 Subject: [PATCH] Pending download stats optimisations - tracks separately a global list for a faster sum. Needs slightly more memory --- src/network/bmproto.py | 4 ++++ src/network/objectracker.py | 2 ++ src/network/stats.py | 24 +++++++++++++----------- src/state.py | 3 +++ 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/network/bmproto.py b/src/network/bmproto.py index a3f7d620..5f689307 100644 --- a/src/network/bmproto.py +++ b/src/network/bmproto.py @@ -534,6 +534,10 @@ class BMProto(AdvancedDispatcher, ObjectTracker): del connection.objectsNewToThem[hashId] except KeyError: pass + try: + del state.missingObjects[hashId] + except KeyError: + pass def handle_close(self): self.set_state("close") diff --git a/src/network/objectracker.py b/src/network/objectracker.py index 62016d75..bfb75174 100644 --- a/src/network/objectracker.py +++ b/src/network/objectracker.py @@ -5,6 +5,7 @@ from threading import RLock from debug import logger from inventory import Inventory from network.dandelion import Dandelion +from state import missingObjects haveBloom = False @@ -82,6 +83,7 @@ class ObjectTracker(object): except KeyError: pass if hashId not in Inventory(): + missingObjects[hashId] = None with self.objectsNewToMeLock: self.objectsNewToMe[hashId] = True elif hashId in Dandelion().hashMap: diff --git a/src/network/stats.py b/src/network/stats.py index ade56ac0..80925f7c 100644 --- a/src/network/stats.py +++ b/src/network/stats.py @@ -2,6 +2,7 @@ import time from network.connectionpool import BMConnectionPool import asyncore_pollchoose as asyncore +from state import missingObjects lastReceivedTimestamp = time.time() lastReceivedBytes = 0 @@ -50,19 +51,20 @@ def downloadSpeed(): return currentReceivedSpeed def pendingDownload(): - tmp = {} - for connection in BMConnectionPool().inboundConnections.values() + \ - BMConnectionPool().outboundConnections.values(): - for k in connection.objectsNewToMe.keys(): - tmp[k] = True - return len(tmp) + return len(missingObjects) + #tmp = {} + #for connection in BMConnectionPool().inboundConnections.values() + \ + # BMConnectionPool().outboundConnections.values(): + # for k in connection.objectsNewToMe.keys(): + # tmp[k] = True + #return len(tmp) def pendingUpload(): - tmp = {} - for connection in BMConnectionPool().inboundConnections.values() + \ - BMConnectionPool().outboundConnections.values(): - for k in connection.objectsNewToThem.keys(): - tmp[k] = True + #tmp = {} + #for connection in BMConnectionPool().inboundConnections.values() + \ + # BMConnectionPool().outboundConnections.values(): + # for k in connection.objectsNewToThem.keys(): + # tmp[k] = True #This probably isn't the correct logic so it's disabled #return len(tmp) return 0 diff --git a/src/state.py b/src/state.py index c9cb3d1c..32433e2d 100644 --- a/src/state.py +++ b/src/state.py @@ -43,6 +43,9 @@ trustedPeer = None discoveredPeers = {} +# tracking pending downloads globally, for stats +missingObjects = {} + Peer = collections.namedtuple('Peer', ['host', 'port']) def resetNetworkProtocolAvailability():