我对cherrypy(使用web.py作为框架)和tornado从互联网上检索网页进行了测试。
我有三个测试用例siege
用于向服务器发送请求(-c 表示用户数;-t 是测试时间)。代码在测试结果下方。
1. web.py (cherrpy)
siege ip -c20 -t100s server can handle 2747requests
siege ip -c200 -t30s server can handle 1361requests
siege ip -c500 -t30s server can handle 170requests
2.龙卷风同步
siege ip -c20 -t100s server can handle 600requests
siege ip -c200 -t30s server can handle 200requests
siege ip -c500 -t30s server can handle 116requests
3. 龙卷风异步
siege ip -c20 -t100s server can handle 3022requests
siege ip -c200 -t30s server can handle 2259requests
siege ip -c500 -t30s server can handle 471requests
性能分析:
tornado 同步 < web.py (cherrypy) < tornado 异步
问题一:
我知道,使用异步架构可以显着提高 Web 服务器的性能。
我很好奇 tornado 异步架构和 web.py (cherry) 之间的区别。
我认为龙卷风同步模式会一一处理请求,但是cherrypy是如何工作的,使用多个线程呢?但是我没有看到内存有很大的增加。Cherrypy 可能会同时处理多个请求。它是如何解决程序阻塞的?
问题2:
我可以在不使用异步技术的情况下提高 tornado 同步模式的性能吗?我认为龙卷风可以做得更好。
Web.py 代码:
import web
import tornado.httpclient
urls = (
'/(.*)', 'hello'
)
app = web.application(urls, globals())
class hello:
def GET(self, name):
client = tornado.httpclient.HTTPClient()
response=client.fetch("http://www.baidu.com/")
return response.body
if __name__ == "__main__":
app.run()
龙卷风同步:
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.httpclient
from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)
class IndexHandler(tornado.web.RequestHandler):
def get(self):
client = tornado.httpclient.HTTPClient()
response = client.fetch("http://www.baidu.com/" )
self.write(response.body)
if __name__=='__main__':
tornado.options.parse_command_line()
app=tornado.web.Application(handlers=[(r'/',IndexHandler)])
http_server=tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
龙卷风异步:
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.httpclient
from tornado.options import define, options
define("port", default=8001, help="run on the given port", type=int)
class IndexHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
client = tornado.httpclient.AsyncHTTPClient()
response = client.fetch("http://www.baidu.com/" ,callback=self.on_response)
def on_response(self,response):
self.write(response.body)
self.finish()
if __name__=='__main__':
tornado.options.parse_command_line()
app=tornado.web.Application(handlers=[(r'/',IndexHandler)])
http_server=tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()