【问题标题】:Why is there a difference in performance between Tornado's ioloop and httpserver?为什么 Tornado 的 ioloop 和 httpserver 在性能上有差异?
【发布时间】:2011-08-20 03:03:36
【问题描述】:

我决定使用 Python Tornado 作为我的创业公司的选择服务器,并且我正在针对两个参考 Python Tornado 实现运行 httpref 以对 Tornado 的功能进行压力测试。以下是我相互运行的两段代码:

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.listen(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"

HTTP服务器:

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()

(注:第一个实现取自http://nichol.as/asynchronous-servers-in-python

我很担心我从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

HTTP服务器:

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

有没有人能很好地解释为什么 iostream 的性能比 httpserver 好得多?提前致谢!

【问题讨论】:

    标签: python tornado stress-testing


    【解决方案1】:

    错误:fd-unavail 47569

    这意味着你的机器没有文件描述符。

    您的 httperf 失败。

    【讨论】:

      猜你喜欢
      • 2015-06-04
      • 2017-08-05
      • 1970-01-01
      • 2015-02-12
      • 1970-01-01
      • 2012-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多