设置:Python 2.7.15,Tornado 5.1
我有一台Web服务器,每秒处理约40个/recommend
请求。
平均响应时间为25毫秒,但差异很大(某些请求可能需要500毫秒以上的时间)。
每个请求在内部生成1-8个Elasticsearch查询(HTTP请求)。 每个Elasticsearch查询可能需要1-150毫秒。
Elasticsearch请求通过elasticsearch-dsl库进行同步处理。
目标是减少I / O等待时间(对Elasticsearch的查询)并每秒处理更多请求,以便减少机器数量。 一件事是不可接受的-我不想增加平均处理时间(25毫秒)。
我在网络上找到了一些龙卷风-elasticsearch的实现,但是由于我只需要使用Elasticsearch(/_search
)的一个端点,所以我试图独自做到这一点。
下面是我的Web服务器的一个退化的实现。在相同的负载(每秒约40个请求)下,平均请求响应时间增加到 200ms !
深入研究,我发现内部异步处理时间(对Elasticsearch的查询)不稳定,每个fetch
调用所花费的时间可能有所不同,并且总平均值(在ab
负载下测试)高。
我正在使用ab
模拟负载并通过打印当前fetch
处理时间,平均fetch
处理时间和最大处理时间来内部测量负载。
一次执行一个请求时(并发1):
ab -p es-query-rcom.txt -T application/json -n 1000 -c 1 -k 'http://localhost:5002/recommend'
我的照片看起来像:[avg req_time: 3, dur: 3] [current req_time: 2, dur: 3] [max req_time: 125, dur: 125] reqs: 8000
但是当我尝试增加并发性(最多8个)时:ab -p es-query-rcom.txt -T application/json -n 1000 -c 8 -k 'http://localhost:5002/recommend'
现在我的照片看起来像:[avg req_time: 6, dur: 13] [current req_time: 4, dur: 4] [max req_time: 73, dur: 84] reqs: 8000
现在平均要求降低了 x2 (或根据我的测量结果 x4 )! 我在这里想念什么?为什么我看到这种退化?
web_server.py:
import tornado
from tornado.httpclient import AsyncHTTPClient
from tornado.options import define, options
from tornado.httpserver import HTTPServer
from web_handler import WebHandler
SERVICE_NAME = 'web_server'
NUM_OF_PROCESSES = 1
class Statistics(object):
def __init__(self):
self.total_requests = 0
self.total_requests_time = 0
self.total_duration = 0
self.max_time = 0
self.max_duration = 0
class RcomService(object):
def __init__(self):
print 'initializing RcomService...'
AsyncHTTPClient.configure("tornado.curl_httpclient.CurlAsyncHTTPClient", max_clients=3)
self.stats = Statistics()
def start(self, port):
define("port", default=port, type=int)
db = self.get_db(self.stats)
routes = self.generate_routes(db)
app = tornado.web.Application(routes)
http_server = HTTPServer(app, xheaders=True)
http_server.bind(options.port)
http_server.start(NUM_OF_PROCESSES)
tornado.ioloop.IOLoop.current().start()
@staticmethod
def generate_routes(db):
return [
(r"/recommend", WebHandler, dict(db=db))
]
@staticmethod
def get_db(stats):
return {
'stats': stats
}
def main():
port = 5002
print('starting %s on port %s', SERVICE_NAME, port)
rcom_service = RcomService()
rcom_service.start(port)
if __name__ == '__main__':
main()
web_handler.py:
import time
import ujson
from tornado import gen
from tornado.gen import coroutine
from tornado.httpclient import AsyncHTTPClient
from tornado.web import RequestHandler
class WebHandler(RequestHandler):
def initialize(self, db):
self.stats = db['stats']
@coroutine
def post(self, *args, **kwargs):
result = yield self.wrapper_innear_loop([{}, {}, {}, {}, {}, {}, {}, {}]) # dummy queries (empty)
self.write({
'res': result
})
@coroutine
def wrapper_innear_loop(self, queries):
result = []
for q in queries: # queries are performed serially
res = yield self.async_fetch_gen(q)
result.append(res)
raise gen.Return(result)
@coroutine
def async_fetch_gen(self, query):
url = 'http://localhost:9200/my_index/_search'
headers = {
'Content-Type': 'application/json',
'Connection': 'keep-alive'
}
http_client = AsyncHTTPClient()
start_time = int(round(time.time() * 1000))
response = yield http_client.fetch(url, method='POST', body=ujson.dumps(query), headers=headers)
end_time = int(round(time.time() * 1000))
duration = end_time - start_time
body = ujson.loads(response.body)
request_time = int(round(response.request_time * 1000))
self.stats.total_requests += 1
self.stats.total_requests_time += request_time
self.stats.total_duration += duration
if self.stats.max_time < request_time:
self.stats.max_time = request_time
if self.stats.max_duration < duration:
self.stats.max_duration = duration
duration_avg = self.stats.total_duration / self.stats.total_requests
time_avg = self.stats.total_requests_time / self.stats.total_requests
print "[avg req_time: " + str(time_avg) + ", dur: " + str(duration_avg) + \
"] [current req_time: " + str(request_time) + ", dur: " + str(duration) + "] [max req_time: " + \
str(self.stats.max_time) + ", dur: " + str(self.stats.max_duration) + "] reqs: " + \
str(self.stats.total_requests)
raise gen.Return(body)
我尝试使用Simple
大小的异步类(curl
和max_clients
)玩,但我不了解哪种情况是我的最佳选择。
但是
答案 0 :(得分:0)
增加的时间可能是因为并发== 1,CPU利用率不足,而c == 8,则100%+利用率,并且无法满足所有请求。例如,抽象CPU可以每秒处理1000次操作,发送一个请求需要花费50个CPU操作数,读取一个请求结果也需要花费50个CPU操作数。当您有5 RPS时,您的CPU利用率为50%,平均请求时间为50毫秒(发送请求)+请求时间+ 50毫秒(读取请求)。但是,例如,当您有40 RPS(是5 RPS的8倍)时,您的CPU将被过度利用400%,并且某些完成的请求将等待解析,因此现在平均请求时间为50 ms +请求时间+ CPU等待时间 + 50毫秒。
总而言之,我的建议是检查两个负载的CPU利用率,并确定要分析发送请求和解析响应所花费的时间,CPU可能是您的瓶颈。