python3.7 和python3.6 压力测试 aiohttp-tornado-uvloop

Posted 74 months ago python python3.7 python3.6 uvloop aiohttp torando linux ab

统一的ab


ab -n 10000 -c 1000 "http://0.0.0.0:8080/"

结果



aiohttp
	asyncio
		3.7 : 4000
		3.6 : 3300
	uvloop
		3.7 : 4300
		3.6 : 4700
tornado
	ioloop
		3.7 : 3100
		3.6 : 1300
	uvloop
		3.7 : 1700
		3.6 : 1700

aiohttp


from aiohttp import web

async def handle(request):
    name = request.match_info.get('name', "Anonymous")
    text = "Hello, " + name
    return web.Response(text=text)

app = web.Application()
app.add_routes([web.get('/', handle),
                web.get('/{name}', handle)])

web.run_app(app)

aiohttp + uvloop



from aiohttp import web
import uvloop
import asyncio
async def handle(request):
    name = request.match_info.get('name', "Anonymous")
    text = "Hello, " + name
    return web.Response(text=text)

app = web.Application()
app.add_routes([web.get('/', handle),
                web.get('/{name}', handle)])
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
loop = asyncio.get_event_loop()
app._set_loop(loop)

web.run_app(app)

tornado


import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
    ])

if __name__ == "__main__":
    app = make_app()
    app.listen(8080)
    tornado.ioloop.IOLoop.current().start()

tornado + uvloop


import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

import uvloop
import asyncio

from tornado.platform.asyncio import BaseAsyncIOLoop


class TornadoUvloop(BaseAsyncIOLoop):

    def initialize(self, **kwargs):
        loop = uvloop.new_event_loop()
        try:
            super(TornadoUvloop, self).initialize(
                loop, close_loop=True, **kwargs)
        except Exception:
            loop.close()
            raise

def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
    ])

if __name__ == "__main__":
    app = make_app()
    app.listen(8080)
    tornado.ioloop.IOLoop.configure(TornadoUvloop)
    tornado.ioloop.IOLoop.current().start()

依赖


aiohttp==3.3.2
async-timeout==3.0.0
attrs==18.1.0
chardet==3.0.4
idna==2.7
multidict==4.3.1
tornado==5.0.2
uvloop==0.10.2
yarl==1.2.6

看来python3.7的asyncio的速度有很大的提升

点击评论