File: tpclient.py

package info (click to toggle)
twisted 25.5.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,560 kB
  • sloc: python: 203,171; makefile: 200; sh: 92; javascript: 36; xml: 31
file content (64 lines) | stat: -rw-r--r-- 1,280 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""Throughput test."""

import sys
import time

from twisted.internet import protocol, reactor
from twisted.python import log

TIMES = 10000
S = "0123456789" * 1240

toReceive = len(S) * TIMES


class Sender(protocol.Protocol):
    def connectionMade(self):
        start()
        self.numSent = 0
        self.received = 0
        self.transport.registerProducer(self, 0)

    def stopProducing(self):
        pass

    def pauseProducing(self):
        pass

    def resumeProducing(self):
        self.numSent += 1
        self.transport.write(S)
        if self.numSent == TIMES:
            self.transport.unregisterProducer()
            self.transport.loseConnection()

    def connectionLost(self, reason):
        shutdown(self.numSent == TIMES)


started = None


def start():
    global started
    started = time.time()


def shutdown(success):
    if not success:
        raise SystemExit("failure or something")
    passed = time.time() - started
    print("Throughput (send): %s kbytes/sec" % ((toReceive / passed) / 1024))
    reactor.stop()


def main():
    f = protocol.ClientFactory()
    f.protocol = Sender
    reactor.connectTCP(sys.argv[1], int(sys.argv[2]), f)
    reactor.run()


if __name__ == "__main__":
    # log.startLogging(sys.stdout)
    main()