开发者

Why is there a difference in performance between Tornado's ioloop and httpserver?

开发者 https://www.devze.com 2023-03-29 04:37 出处:网络
I decided to use Python Tornado as a choice server for my startup, and I was running httpref against two reference Python Tornado implementations to stress test Tornado\'s capabilities. Here are the t

I decided to use Python Tornado as a choice server for my startup, and I was running httpref against two reference Python Tornado implementations to stress test Tornado's capabilities. Here are the two pieces of code that I ran against each other:

iostream:

import errno
import functools
import socket
from tornado import ioloop, iostream

def connection_ready(sock, fd, events):
    while True:
        try:
            connection, address = sock.accept()
        except socket.error, e:
            if e[0] not in (errno.EWOULDBLOCK, errno.EAGAIN):
                raise
            return
        connection.setblocking(0)
        stream = iostream.IOStream(connection)
        message = "You requested %s\n" % "hi"
        stream.write("HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n%s" % (len(message), message), stream.close)


sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setblocking(0)
sock.bind(("", 8011))
sock.l开发者_StackOverflow中文版isten(5000)

io_loop = ioloop.IOLoop.instance()
callback = functools.partial(connection_ready, sock)
io_loop.add_handler(sock.fileno(), callback, io_loop.READ)
try:
    io_loop.start()
except KeyboardInterrupt:
    io_loop.stop()
    print "exited cleanly"

HTTPServer:

from tornado import httpserver
from tornado import ioloop

def handle_request(request):
    message = "You requested %s\n" % "hi"
    request.write("HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n%s" % (len(message), message))
    request.finish()


http_server = httpserver.HTTPServer(handle_request)
http_server.bind(8012)
http_server.start(0)

try:
    ioloop=ioloop.IOLoop.instance()
    ioloop.start()
except KeyboardInterrupt:
    ioloop.stop()

(Note: the first implementation is taken from http://nichol.as/asynchronous-servers-in-python)

I'm worried about the results I got back from httperf --server=localhost --port=8011 --rate=4000 --num-conns=80000:

iostream:

Reply rate [replies/s]: min 3852.3 avg 3973.1 max 4094.5 stddev 112.3 (4 samples)
Reply time [ms]: response 12.2 transfer 0.0
[...]
Errors: total 499 client-timo 0 socket-timo 0 connrefused 0 connreset 0
Errors: fd-unavail 499 addrunavail 0 ftab-full 0 other 0

HTTPServer:

Reply rate [replies/s]: min 0.0 avg 1280.7 max 2138.5 stddev 962.8 (4 samples)
Reply time [ms]: response 334.6 transfer 0.0
[...]
Errors: total 51697 client-timo 0 socket-timo 0 connrefused 0 connreset 0
Errors: fd-unavail 47569 addrunavail 0 ftab-full 0 other 4128

Does anybody have a good explanation for why iostream performs so much better than httpserver? Thanks in advance!


Errors: fd-unavail 47569

This means your machine are out of file descriptors.

Your httperf failed.

0

精彩评论

暂无评论...
验证码 换一张
取 消