python tornado asynchronous performance test

Test both interfaces

# -*- coding:utf-8 -*-

import time
import tornado.web
import tornado.gen
import tornado.ioloop
from tornado.concurrent import run_on_executor
from concurrent.futures import ThreadPoolExecutor


class SyncHandler(tornado.web.RequestHandler):
    def get(self, *args, **kwargs):
        time.sleep( 5 ) # sleep is used to simply refer to a time-consuming io operation
        self.write( " Synchronized Hello World " )


class AsyncHandler(tornado.web.RequestHandler):
    executor = ThreadPoolExecutor(5)

    @tornado.gen.coroutine
    def get(self):
        resp = yield self.sleep_for_result()
        self.write(resp)

    @run_on_executor
    def sleep_for_result(self):
        time.sleep( 5 )
         return  ' Asynchronous Hello World '


application = tornado.web.Application([
    (r'/sync', SyncHandler),
    (r'/async', AsyncHandler),
])

if __name__ == "__main__":
    from tornado.options import options, define

    define("port", default=8080, help="跑在8080", type=int)

    http_server = tornado.httpserver.HTTPServer(application)
    http_server.bind(options.port)
    http_server.start( 1 ) # number of processes
    tornado.ioloop.IOLoop.instance().start()

 

Start the tornado service.

We do not use ab test here, use a more flexible code thread pool to test performance, and use thread pool to request the interface concurrently

 

#coding=utf8
import requests
import time
from tomorrow import threads

@threads(10)
def test_sync():
    print requests.get('http://127.0.0.1:8080/sync').content + '        ' + time.strftime('%H:%M:%S')

@threads(10)
def test_async():
    print requests.get('http://127.0.0.1:8080/async').content + '        ' + time.strftime('%H:%M:%S')


[test_sync() for i in range(100)]

#[test_async() for i in range(100)]

 

The synchronization method is tested as follows:

You can see that when 10 threads request a synchronous interface, a request can be processed every 5 seconds. The tornado process set in the program is 1. If the number of tornado service processes is increased to 4, 4 synchronization requests can be processed every 5 seconds.

 

The asynchronous test is as follows:

It can be seen that when 10 threads request the asynchronous interface, 5 requests can be processed every 5 seconds, because the number of ThreadPoolExecutor(5) set in the code is 5. If set to 8, then 8 requests can be processed every 5 seconds.

 

When doing the login system of China Unicom's central bank credit investigation based on user authorization, it is necessary to verify the account password verification code submitted by the user. In addition, the use of proxy ip time is very unstable, and it takes a lot of time to verify whether the account password submitted by the user can log in to the third-party website. Time, you need to use the asynchronous method of tornado, otherwise it will take a long time to get feedback after the user submits the account password, and the user experience will be very bad.

 

If you use django and use the built-in service, you can process 1 request every 5 seconds, and other requests must wait for the end of the previous request to be processed.

However, django deploys with uwsgi, which will not perform as badly as the built-in service. When deploying with uwsgi, the number of processes and threads are generally set, and more requests can be processed every 5 seconds after deployment.

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325149655&siteId=291194637