diff --git a/src/wormhole/servers/transit_server.py b/src/wormhole/servers/transit_server.py index da54e69..2412c89 100644 --- a/src/wormhole/servers/transit_server.py +++ b/src/wormhole/servers/transit_server.py @@ -10,6 +10,18 @@ HOUR = 60*MINUTE DAY = 24*HOUR MB = 1000*1000 +def round_to(size, coarseness): + return int(coarseness*(1+int((size-1)/coarseness))) + +def blur_size(size): + if size == 0: + return 0 + if size < 1e6: + return round_to(size, 10e3) + if size < 1e9: + return round_to(size, 1e6) + return round_to(size, 100e6) + class TransitConnection(protocol.Protocol): def __init__(self): self._got_token = False @@ -173,6 +185,7 @@ class Transit(protocol.ServerFactory, service.MultiService): log.msg("Transit.recordUsage (%dB)" % total_bytes) if self._blur_usage: started = self._blur_usage * (started // self._blur_usage) + total_bytes = blur_size(total_bytes) self._db.execute("INSERT INTO `usage`" " (`type`, `started`, `result`, `total_bytes`," " `total_time`, `waiting_time`)" diff --git a/src/wormhole/test/test_server.py b/src/wormhole/test/test_server.py index 3b8be7e..978fa0d 100644 --- a/src/wormhole/test/test_server.py +++ b/src/wormhole/test/test_server.py @@ -8,7 +8,7 @@ from twisted.internet.threads import deferToThread from twisted.web.client import getPage, Agent, readBody from .. import __version__ from .common import ServerBase -from ..servers import relay_server +from ..servers import relay_server, transit_server from ..twisted.eventsource_twisted import EventSource class Reachable(ServerBase, unittest.TestCase): @@ -429,3 +429,23 @@ class Summary(unittest.TestCase): (1, "quiet", 40, 9)) self.failUnlessEqual(c._summarize(make_moods(None, "scary"), 41), (1, "scary", 40, 9)) + +class Transit(unittest.TestCase): + def test_blur_size(self): + blur = transit_server.blur_size + self.failUnlessEqual(blur(0), 0) + self.failUnlessEqual(blur(1), 10e3) + self.failUnlessEqual(blur(10e3), 10e3) + self.failUnlessEqual(blur(10e3+1), 20e3) + self.failUnlessEqual(blur(15e3), 20e3) + self.failUnlessEqual(blur(20e3), 20e3) + self.failUnlessEqual(blur(1e6), 1e6) + self.failUnlessEqual(blur(1e6+1), 2e6) + self.failUnlessEqual(blur(1.5e6), 2e6) + self.failUnlessEqual(blur(2e6), 2e6) + self.failUnlessEqual(blur(900e6), 900e6) + self.failUnlessEqual(blur(1000e6), 1000e6) + self.failUnlessEqual(blur(1050e6), 1100e6) + self.failUnlessEqual(blur(1100e6), 1100e6) + self.failUnlessEqual(blur(1150e6), 1200e6) +