~exarkun/+junk/twisted-benchmarks

32 by Jean-Paul Calderone
Add an SSH throughput benchmark
1
from zope.interface import implements
2
3
from twisted.internet.defer import succeed
4
from twisted.internet.endpoints import TCP4ClientEndpoint
5
from twisted.cred.portal import IRealm, Portal
6
from twisted.cred.checkers import InMemoryUsernamePasswordDatabaseDontUse
7
from twisted.conch.avatar import ConchUser
8
from twisted.conch.ssh.userauth import SSHUserAuthClient
9
from twisted.conch.ssh.session import ISession, SSHSession
10
11
from ssh_connect import BenchmarkSSHFactory
12
from tcp import Client
13
14
from sshendpoint import SSHCommandClientEndpoint
15
16
from benchlib import driver
17
18
19
class SSHPasswordUserAuth(SSHUserAuthClient):
20
    def __init__(self, user, password, instance):
21
        SSHUserAuthClient.__init__(self, user, instance)
22
        self.password = password
23
24
25
    def getPassword(self, prompt=None):
26
        return succeed(self.password)
27
28
29
30
class EchoTransport(object):
31
    def __init__(self, protocol):
32
        self.protocol = protocol
33
34
35
    def write(self, bytes):
36
        self.protocol.childDataReceived(1, bytes)
37
38
39
    def loseConnection(self):
40
        pass
41
42
43
44
class BenchmarkAvatar(ConchUser):
45
    implements(ISession)
46
47
    def __init__(self):
48
        ConchUser.__init__(self)
49
        self.channelLookup['session'] = SSHSession
50
51
52
    def execCommand(self, proto, cmd):
53
        if cmd == 'chargen':
54
            self.proto = proto
55
            transport = EchoTransport(proto)
56
            proto.makeConnection(transport)
57
        else:
58
            raise RuntimeError("Unexpected execCommand")
59
60
61
    def closed(self):
62
        pass
63
64
65
66
class BenchmarkRealm(object):
67
    implements(IRealm)
68
69
    def requestAvatar(self, avatarId, mind, *interfaces):
70
        return (
71
            interfaces[0],
72
            BenchmarkAvatar(),
73
            lambda: None)
74
75
76
def main(reactor, duration):
77
    chunkSize = 16384
78
79
    server = BenchmarkSSHFactory()
80
    server.portal = Portal(BenchmarkRealm())
81
    server.portal.registerChecker(
82
        InMemoryUsernamePasswordDatabaseDontUse(username='password'))
83
84
    port = reactor.listenTCP(0, server)
85
    tcpServer = TCP4ClientEndpoint(reactor, '127.0.0.1', port.getHost().port)
86
    sshServer = SSHCommandClientEndpoint(
87
        'chargen', tcpServer,
88
        lambda command:
89
            SSHPasswordUserAuth('username', 'password', command))
90
91
    client = Client(reactor, sshServer)
92
    return client.run(duration, chunkSize)
93
94
95
96
if __name__ == '__main__':
97
    import sys
98
    import ssh_throughput
99
    # from twisted.python.log import startLogging
100
    # startLogging(sys.stderr, False)
101
    driver(ssh_throughput.main, sys.argv)
102