day08AB压力测试(同步,异步测试)

同步测试(一直执行一个一直等到执行完)

测试前将cmd ,cd到安装有ab的文件的整个目录下

要用pip install tornado==4.5(在terminal下输入卸载:pip uninstall tornado)

sync_tornado.py


import tornado.web
import tornado.ioloop
import tornado.httpclient


class IndexHandler(tornado.web.RequestHandler):

    def get(self):
        # 路由中传递的参数,/index/?q=python
        q = self.get_argument('q')
        # 向地址发送请求: https://cn.bing.com/search?q=
        client = tornado.httpclient.HTTPClient()
        response = client.fetch('https://cn.bing.com/search?q=%s' % q)
        print(response)
        self.write('同步测试')


def make_app():
    return tornado.web.Application(handlers=[
        (r'/index/', IndexHandler),
    ])


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



异步:会有一个装饰机器让连接不会断掉,并且同时执行多个连接.

aync_tornado.py


import tornado.web
import tornado.ioloop
import tornado.httpclient


class IndexHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    def get(self):
        q = self.get_argument('q')
        client = tornado.httpclient.AsyncHTTPClient()
        client.fetch('https://cn.bing.com/search?q=%s' % q, callback=self.on_response)
        self.write('异步测试')

    def on_response(self, response):
        # 回调,当页面响应,则调用回调函数on_response
        print(response)
        self.write('回调执行')
        self.finish()


def make_app():
    return tornado.web.Application(handlers=[
        (r'/index/', IndexHandler),
    ])

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

aync_tornado2.py(异步镶嵌异步一般不用比较繁琐)


import tornado.web
import tornado.ioloop
import tornado.httpclient


class IndexHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    @tornado.web.gen.coroutine
    def get(self):
        q = self.get_argument('q')
        client = tornado.httpclient.AsyncHTTPClient()
        response = yield client.fetch('https://cn.bing.com/search?q=%s' % q)
        print(response)
        self.write('异步测试')


def make_app():
    return tornado.web.Application(handlers=[
        (r'/index/', IndexHandler),
    ])

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

你可能感兴趣的:(day08AB压力测试(同步,异步测试))