Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
asyncio + uvloop echo server benchmark

asyncio + uvloop echo server benchmark

This is an attempt at a bare minimum client/server benchmark of asyncio with optional use of uvloop. On my 2015 macbook pro, I get almost 2.5X improvement using uvloop in the server.

The client runs PARALLEL tasks at once. Running only a single task results in about 1/8 the throughput of 100 simultaneous tasks.

# with uvloop
$ python client.py
satisfied 100000 requests in 1.41021 seconds (70911.42 reqs/s)

# without uvloop
$ python client.py
satisfied 100000 requests in 3.367228 seconds (29698.02 reqs/s)
import asyncio
from datetime import datetime
PORT = 8080
N = 1000
PARALLEL = 100
async def async_connect(host, port):
reader, writer = await asyncio.open_connection(host, port)
for i in range(N):
payload = bytes("payload-{}\n".format(str(10)), encoding='utf8')
writer.write(payload)
data = await reader.readline()
assert(data == payload)
try:
import uvloop
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
except ImportError:
print("unable to import uvloop, optionally install it for asyncio speed)")
t0 = datetime.now()
loop = asyncio.get_event_loop()
tasks = [asyncio.ensure_future(async_connect("localhost", PORT)) for _ in range(PARALLEL)]
loop.run_until_complete(asyncio.gather(*tasks))
delta = (datetime.now() - t0).total_seconds()
print("satisfied {} requests in {} seconds ({:.2f} reqs/s)".format(N*PARALLEL, delta, N*PARALLEL/delta))
import asyncio
PORT = 8080
async def handle_connection(reader, writer):
print('opening connection')
while True:
data = await reader.readline()
if not data:
break
writer.write(data)
print('closing connection')
writer.close()
try:
import uvloop
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
except ImportError:
print("unable to import uvloop, optionally install it for asyncio speed)")
loop = asyncio.get_event_loop()
asyncio.ensure_future(asyncio.start_server(handle_connection, "localhost", PORT))
loop.run_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.