Debian Codegraph @

docs/core/benchmarks/tpclient.py

raw
1 """Throughput test."""
2
3 import sys
4 import time
5
6 from twisted.internet import protocol, reactor
7 from twisted.python import log
8
9 TIMES = 10000
10 S = "0123456789" * 1240
11
12 toReceive = len(S) * TIMES
13
14
15 class Sender(protocol.Protocol):
16 def connectionMade(self):
17 start()
18 self.numSent = 0
19 self.received = 0
20 self.transport.registerProducer(self, 0)
21
22 def stopProducing(self):
23 pass
24
25 def pauseProducing(self):
26 pass
27
28 def resumeProducing(self):
29 self.numSent += 1
30 self.transport.write(S)
31 if self.numSent == TIMES:
32 self.transport.unregisterProducer()
33 self.transport.loseConnection()
34
35 def connectionLost(self, reason):
36 shutdown(self.numSent == TIMES)
37
38
39 started = None
40
41
42 def start():
43 global started
44 started = time.time()
45
46
47 def shutdown(success):
48 if not success:
49 raise SystemExit("failure or something")
50 passed = time.time() - started
51 print("Throughput (send): %s kbytes/sec" % ((toReceive / passed) / 1024))
52 reactor.stop()
53
54
55 def main():
56 f = protocol.ClientFactory()
57 f.protocol = Sender
58 reactor.connectTCP(sys.argv[1], int(sys.argv[2]), f)
59 reactor.run()
60
61
62 if __name__ == "__main__":
63 # log.startLogging(sys.stdout)
64 main()