How to Benchmark HTTP Keep-Alive with Python asyncio: A Practical Guide
This article explains Python's asyncio module, presents a practical HTTP keep-alive benchmark tool built with async I/O, details test setup and performance results, and summarizes key asyncio concepts such as event loops, futures, coroutines, and generators.
Introduction
Asynchronous operations are common in computer systems because collaborating entities often run at different speeds. In software development the typical mismatch is between CPU and I/O, which makes asynchronous I/O essential in many frameworks, from browsers to server‑side platforms like node.js. This article focuses on Python asynchronous I/O.
Asyncio Module Overview
Python 3.4 introduced the asyncio module to support asynchronous I/O. Although its API is provisional and may change, asyncio is already powerful and worth learning.
Example: HTTP Keep-Alive Benchmark
Scenario : Use asyncio to create a benchmark tool that opens many HTTP keep‑alive connections to a web server, sending periodic HEAD requests.
Code
import argparse
import asyncio
import functools
import logging
import random
import urllib.parse
loop = asyncio.get_event_loop()
@asyncio.coroutine
def print_http_headers(no, url, keepalive):
url = urllib.parse.urlsplit(url)
wait_for = functools.partial(asyncio.wait_for, timeout=3, loop=loop)
query = ('HEAD {url.path} HTTP/1.1
'
'Host: {url.hostname}
'
'
').format(url=url).encode('utf-8')
rd, wr = yield from wait_for(asyncio.open_connection(url.hostname, 80))
while True:
wr.write(query)
line = yield from wait_for(rd.readline())
if not line:
wr.close()
return no
line = line.decode('utf-8').rstrip()
if not line:
break
logging.debug('(%d) HTTP header> %s' % (no, line))
yield from asyncio.sleep(random.randint(1, keepalive//2))
@asyncio.coroutine
def do_requests(args):
conn_pool = set()
waiter = asyncio.Future()
def _on_complete(fut):
conn_pool.remove(fut)
exc, res = fut.exception(), fut.result()
if exc is not None:
logging.info('conn#{} exception'.format(exc))
else:
logging.info('conn#{} result'.format(res))
if not conn_pool:
waiter.set_result('event loop is done')
for i in range(args.connections):
fut = asyncio.async(print_http_headers(i, args.url, args.keepalive))
fut.add_done_callback(_on_complete)
conn_pool.add(fut)
if i % 10 == 0:
yield from asyncio.sleep(0.01)
logging.info((yield from waiter))
def main():
parser = argparse.ArgumentParser(description='asyncli')
parser.add_argument('url', help='page address')
parser.add_argument('-c', '--connections', type=int, default=1,
help='number of connections simultaneously')
parser.add_argument('-k', '--keepalive', type=int, default=60,
help='HTTP keepalive timeout')
args = parser.parse_args()
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s')
loop.run_until_complete(do_requests(args))
loop.close()
if __name__ == '__main__':
main()Testing and Analysis
Hardware : CPU 2.3 GHz / 2 cores, RAM 2 GB
Software : CentOS 6.5 (kernel 2.6.32), Python 3.3 (pip install asyncio), nginx 1.4.7
Parameters : ulimit -n 10240; nginx worker connections set to 10240.
Start the web server with a single worker process, then launch the benchmark tool to open 10 k connections to nginx's default page:
$ python asyncli.py http://10.211.55.8/ -c 10000nginx log analysis shows an average of 548 requests per second. The top output indicates that the Python process consumes roughly ten times the CPU and memory of nginx, illustrating the performance gap between Python and a highly optimized C server.
Conclusion
Python implementation is concise—under 80 lines using only the standard library—making it much easier to write than an equivalent C solution.
Runtime efficiency is lower; the Python client uses about ten times the CPU and RAM of nginx, reflecting a two‑order‑of‑magnitude difference in performance.
Single‑thread asynchronous I/O versus multi‑thread synchronous I/O: a multi‑threaded approach would require thousands of threads, consuming hundreds of megabytes of memory and incurring heavy context‑switch overhead.
Asyncio Core Concepts
event loop : the single‑threaded loop that drives asynchronous execution.
future : an object representing the result of an asynchronous operation.
coroutine : a function defined with asyncio.coroutine (or async def) that contains the actual async logic.
generator (yield & yield from) : syntax heavily used in asyncio to pause and resume execution.
References
asyncio – Asynchronous I/O, event loop, coroutines and tasks, https://docs.python.org/3/library/asyncio.html
PEP 3156, Asynchronous IO Support Rebooted: the "asyncio" Module, http://legacy.python.org/dev/peps/pep-3156/
PEP 380, Syntax for Delegating to a Subgenerator, http://legacy.python.org/dev/peps/pep-0380/
PEP 342, Coroutines via Enhanced Generators, http://legacy.python.org/dev/peps/pep-0342/
PEP 255, Simple Generators, http://legacy.python.org/dev/peps/pep-0255/
asyncio source code, http://hg.python.org/cpython/file/3.4/Lib/asyncio/
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
