python - 为什么Tornado的ioloop和httpserver的性能有差异?

标签 python tornado stress-testing

我决定使用 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 好得多吗?提前致谢!

最佳答案

Errors: fd-unavail 47569

这意味着您的计算机没有文件描述符。

您的 httperf 失败。

关于python - 为什么Tornado的ioloop和httpserver的性能有差异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7129345/

相关文章:

python - Django URL//双斜杠被删除(可能是 Apache 的错)?

python - Pandas:通过聚合整行得到结果列

android - PySerial Python 错误 : AttributeError: 'Serial' object has no attribute '_port_handle'

python - 如何衡量网络聊天性能?

performance - 如何对自定义Excel加载项和自动化功能进行性能测试

python - 如何使用 telethon 让所有用户进入 Telegram channel ?

根据字典值进行 Python 3D 绘图

iPython 笔记本无法打开某些文件

cassandra-2.0 - 如何使用 cassandra 压力工具

java - 基于netty的压力工具和连接计数