Skip to content

Instantly share code, notes, and snippets.

@mrocklin
Created June 11, 2018 13:07
Show Gist options
  • Save mrocklin/609824993811575dd7d774f1eb5becc9 to your computer and use it in GitHub Desktop.
Save mrocklin/609824993811575dd7d774f1eb5becc9 to your computer and use it in GitHub Desktop.
from tornado.tcpserver import TCPServer
from tornado.tcpclient import TCPClient
from tornado.ioloop import IOLoop
from tornado.iostream import StreamClosedError
from tornado import gen
from distributed.utils import format_time
from time import time
class EchoServer(TCPServer):
@gen.coroutine
def handle_stream(self, stream, address):
while True:
try:
data = yield stream.read_bytes(1)
yield stream.write(data)
except StreamClosedError:
break
@gen.coroutine
def f():
port = 1234
n = 1000
server = EchoServer()
server.bind(port)
server.start()
client = TCPClient()
stream = yield client.connect('localhost', port)
start = time()
for i in range(n):
yield stream.write(b'0')
yield stream.read_bytes(1)
stop = time()
print('duration', format_time((stop - start) / n)) # I get around 200us on my machine
if __name__ == '__main__':
IOLoop().run_sync(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment