# HG changeset patch # User Gregory Szorc # Date 1538420574 25200 # Node ID dc82ad1b7f77d6e77903db7a196cb588bb25a319 # Parent f2dffa1359c65ea7ecbb0e29d8f54bbe6c159f2e keepalive: track request count and bytes sent I want wire protocol interactions to report the number of requests made and bytes transferred. This commit teaches the very low-level custom HTTPConnection class to track the number of bytes sent to the socket. This may vary from the number of bytes that go on the wire due to e.g. TLS. That's OK. KeepAliveHandler is taught to track the total number of requests and total number of bytes sent across all requests. Differential Revision: https://phab.mercurial-scm.org/D4856 diff -r f2dffa1359c6 -r dc82ad1b7f77 mercurial/keepalive.py --- a/mercurial/keepalive.py Mon Oct 01 12:06:36 2018 -0700 +++ b/mercurial/keepalive.py Mon Oct 01 12:02:54 2018 -0700 @@ -174,6 +174,8 @@ class KeepAliveHandler(object): def __init__(self): self._cm = ConnectionManager() + self.requestscount = 0 + self.sentbytescount = 0 #### Connection Management def open_connections(self): @@ -312,6 +314,8 @@ return r def _start_transaction(self, h, req): + oldbytescount = h.sentbytescount + # What follows mostly reimplements HTTPConnection.request() # except it adds self.parent.addheaders in the mix and sends headers # in a deterministic order (to make testing easier). @@ -346,6 +350,16 @@ if urllibcompat.hasdata(req): h.send(data) + # This will fail to record events in case of I/O failure. That's OK. + self.requestscount += 1 + self.sentbytescount += h.sentbytescount - oldbytescount + + try: + self.parent.requestscount += 1 + self.parent.sentbytescount += h.sentbytescount - oldbytescount + except AttributeError: + pass + class HTTPHandler(KeepAliveHandler, urlreq.httphandler): pass @@ -585,9 +599,11 @@ data = read(blocksize) while data: self.sock.sendall(data) + self.sentbytescount += len(data) data = read(blocksize) else: self.sock.sendall(str) + self.sentbytescount += len(str) except socket.error as v: reraise = True if v[0] == errno.EPIPE: # Broken pipe @@ -624,6 +640,9 @@ send = safesend getresponse = wrapgetresponse(httplib.HTTPConnection) + def __init__(self, *args, **kwargs): + httplib.HTTPConnection.__init__(self, *args, **kwargs) + self.sentbytescount = 0 ######################################################################### ##### TEST FUNCTIONS