~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
"""
Benchmark for Twisted's (A)synchronous (M)essaging (P)rotocol.
"""

from twisted.internet.protocol import ServerFactory, ClientCreator
from twisted.protocols.amp import String, Integer, Float, ListOf
from twisted.protocols.amp import Command, CommandLocator, AMP

from benchlib import Client, driver


class Benchmark(Command):
    arguments = [
        ('foo', String()),
        ('bar', Integer()),
        ('baz', ListOf(Float())),
        ]



class BenchmarkLocator(CommandLocator):
    @Benchmark.responder
    def benchmark(self, foo, bar, baz):
        return {}



class Client(Client):
    _string = 'hello, world' * 50
    _integer = 123454321
    _list = [1.2, 2.3, 3.4]

    def __init__(self, reactor, port):
        super(Client, self).__init__(reactor)
        self._port = port


    def run(self, *args, **kwargs):
        def connected(proto):
            self._proto = proto
            return super(Client, self).run(*args, **kwargs)
        client = ClientCreator(self._reactor, AMP)
        d = client.connectTCP('127.0.0.1', self._port)
        d.addCallback(connected)
        return d


    def _request(self):
        d = self._proto.callRemote(
            Benchmark, foo=self._string, bar=self._integer, baz=self._list)
        d.addCallback(self._continue)
        d.addErrback(self._stop)


def main(reactor, duration):
    concurrency = 15

    server = ServerFactory()
    server.protocol = lambda: AMP(locator=BenchmarkLocator())
    port = reactor.listenTCP(0, server)
    client = Client(reactor, port.getHost().port)
    d = client.run(concurrency, duration)
    return d


if __name__ == '__main__':
    import sys
    import amp
    driver(amp.main, sys.argv)