我决定使用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"
的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()
(注意:第一个实现来自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
的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
有没有人能够很好地解释为什么iostream的性能比httpserver好得多?提前谢谢!
答案 0 :(得分:1)
错误:fd-unavail 47569
这意味着您的计算机没有文件描述符。
你的httperf失败了。