forked from twisted-infra/twisted-benchmarks
/
ssl_connect.py
54 lines (38 loc) · 1.35 KB
/
ssl_connect.py
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
from time import time
from benchlib import driver
from twisted.internet.protocol import Factory, Protocol
from twisted.internet.endpoints import SSL4ServerEndpoint, SSL4ClientEndpoint
from ssl_throughput import cert
from tcp_connect import CloseConnection, Client
class WriteOneByte(Protocol):
def connectionMade(self):
self.transport.write(b"x")
class Client(Client):
protocol = WriteOneByte
def main(reactor, duration):
concurrency = 50
interface = '127.0.0.%d' % (int(time()) % 254 + 1,)
contextFactory = cert.options()
factory = Factory()
factory.protocol = CloseConnection
serverEndpoint = SSL4ServerEndpoint(
reactor, 0, contextFactory, interface=interface)
listen = serverEndpoint.listen(factory)
def cbListening(port):
client = Client(
reactor, SSL4ClientEndpoint(
reactor, interface, port.getHost().port,
contextFactory, bindAddress=(interface, 0)))
d = client.run(concurrency, duration)
def cleanup(passthrough):
d = port.stopListening()
d.addCallback(lambda ignored: passthrough)
return d
d.addCallback(cleanup)
return d
listen.addCallback(cbListening)
return listen
if __name__ == '__main__':
import sys
import ssl_connect
driver(ssl_connect.main, sys.argv)