~exarkun/+junk/twisted-benchmarks

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from zope.interface import implements

from twisted.internet.defer import succeed
from twisted.internet.endpoints import TCP4ClientEndpoint
from twisted.cred.portal import IRealm, Portal
from twisted.cred.checkers import InMemoryUsernamePasswordDatabaseDontUse
from twisted.conch.avatar import ConchUser
from twisted.conch.ssh.userauth import SSHUserAuthClient
from twisted.conch.ssh.session import ISession, SSHSession

from ssh_connect import BenchmarkSSHFactory
from tcp import Client

from sshendpoint import SSHCommandClientEndpoint

from benchlib import driver


class SSHPasswordUserAuth(SSHUserAuthClient):
    def __init__(self, user, password, instance):
        SSHUserAuthClient.__init__(self, user, instance)
        self.password = password


    def getPassword(self, prompt=None):
        return succeed(self.password)



class EchoTransport(object):
    def __init__(self, protocol):
        self.protocol = protocol


    def write(self, bytes):
        self.protocol.childDataReceived(1, bytes)


    def loseConnection(self):
        pass



class BenchmarkAvatar(ConchUser):
    implements(ISession)

    def __init__(self):
        ConchUser.__init__(self)
        self.channelLookup['session'] = SSHSession


    def execCommand(self, proto, cmd):
        if cmd == 'chargen':
            self.proto = proto
            transport = EchoTransport(proto)
            proto.makeConnection(transport)
        else:
            raise RuntimeError("Unexpected execCommand")


    def closed(self):
        pass



class BenchmarkRealm(object):
    implements(IRealm)

    def requestAvatar(self, avatarId, mind, *interfaces):
        return (
            interfaces[0],
            BenchmarkAvatar(),
            lambda: None)


def main(reactor, duration):
    chunkSize = 16384

    server = BenchmarkSSHFactory()
    server.portal = Portal(BenchmarkRealm())
    server.portal.registerChecker(
        InMemoryUsernamePasswordDatabaseDontUse(username='password'))

    port = reactor.listenTCP(0, server)
    tcpServer = TCP4ClientEndpoint(reactor, '127.0.0.1', port.getHost().port)
    sshServer = SSHCommandClientEndpoint(
        'chargen', tcpServer,
        lambda command:
            SSHPasswordUserAuth('username', 'password', command))

    client = Client(reactor, sshServer)
    return client.run(duration, chunkSize)



if __name__ == '__main__':
    import sys
    import ssh_throughput
    # from twisted.python.log import startLogging
    # startLogging(sys.stderr, False)
    driver(ssh_throughput.main, sys.argv)