1.带server2.0装饰器
接口访问数据库查询
并发100 平均每秒处理11.8次请求 平均响应时间6944ms
接口不做任何处理
并发100 平均每秒处理99.9次请求 平均响应时间3ms
并发500 平均每秒处理467.9次请求 平均响应时间4ms
并发1000 平均每秒处理936.8次请求 平均响应时间6ms
![](https://images-cdn.shimo.im/dvhwWhIN04wwb2q7/image.png!thumbnail)
再提高并发 并不能再提高处理速度了
小法目前接口
并发100 平均每秒处理11次请求 平均响应时间6647ms
2.接口不使用任何装饰器
接口不做任何处理
并发100 平均每秒处理100.2次请求 平均响应时间3ms
并发1000 平均每秒处理949.2次请求 平均响应时间4ms
并发2000 平均每秒处理1500.4次请求 平均响应时间16ms
接口访问数据库
并发100 平均每秒处理16.2次请求 平均响应时间4086ms
小结
加server2.0装饰器后出错率上升,不加装饰器运行稳定。
并发数主要限制于数据库的访问速度。
server2.0并没有提高并发
3.使用异步装饰器
接口不做任何处理
@gen.coroutine
并发1500 平均每秒处理1296.5次请求 平均响应时间8ms
使用异步访问数据库
并发100 平均每秒处理96.1次请求 平均响应时间39ms
from elasticsearch_async import AsyncElasticsearch
es = AsyncElasticsearch(
hosts = [
"117.78.26.××:××××",
"117.78.26.××:××××"
],
type = "es",
http_auth = ("×××××","×××××"),
timeout = 60
)
@web.asynchronous
@gen.engine
def post(self, *args, **kwargs):
result = {'code': 200, 'msg': '返回成功','data':{}}
body = {"size": 10, "_source": ["id","name"],
"query":
{"bool": {"must": [
{"match":{"name":{"query":"婚姻法"}}},
{"match": {"law_type": {"query": "法律"}}},
]}}}
laws = yield self.es.search("law_search_v2","_doc",body)
# laws = "测试"
result['data']['laws'] = laws
self.finish(result)
并发600 平均每秒处理550次请求 平均响应时间75ms
再增加并发请求数量并发量反而会降低
总结
异步请求数据库稳定性好,速度快,并发量大
目前数据库异步支持
es : elasticsearch-async 支持es6.0
mongodb : motor
mysql : tornado_mysql
接口访问使用tornado.httpclient.AsyncHTTPClient
4.不同异步方式之间的区别
使用@gen.coroutine
代码难度大
使用ThreadPoolExecutor
便与开发