from sanic import Sanic
from sanic import response
app = Sanic(__name__)
@app.route('/v1/ok', methods=['GET'])
async def post_handler(request):
return response.text("hey all good")
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8001, debug=True)
我正在尝试使用sanic在 python 中编写 REST API
以下是我的结论:
我尝试使用wrk对这个 GET API 进行基准测试,其中 50 个线程运行 30 秒测试。机器使用 AWS EC2 t2.medium,它有 4GB RAM 和 2 个 CPU 命令使用
wrk -t50 -c4000 -d30s http://XXX.XX.XXX.XXX:8001/v1/ok
基准测试结果
Running 30s test @ http://XXX.XX.XXX.XXX:8001/v1/ok
50 threads and 4000 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 559.30ms 117.86ms 1.99s 94.47%
Req/Sec 41.92 44.33 361.00 86.14%
53260 requests in 30.10s, 6.70MB read
Socket errors: connect 1493, read 15631, write 0, timeout 4
Requests/sec: 1769.21
Transfer/sec: 228.06KB
我的疑问是,我该如何改进
- 超时的请求数。目前它是 4。它应该是零。
- 平均延迟,约为 550 毫秒(太多)
在 POST 请求的情况下,它非常非常糟糕,我正在尝试加载 keras 模型并进行预测。
代码编写方式有问题吗?
或者
这是 Sanic 的限制吗?
我应该尝试另一个 REST 框架吗?
PS:在延迟和超时请求方面,我使用烧瓶的经验更糟。
import sys
import os
import json
import pandas
import numpy
import optparse
from keras.models import Sequential, load_model
from keras.preprocessing import sequence
from keras.preprocessing.text import Tokenizer
from collections import OrderedDict
from sanic import Sanic
from sanic import response
import time
app = Sanic(__name__)
@app.route('/v1/mal/prediction', methods=['POST'])
async def post_handler(request):
csv_file = 'alerts.csv'
log_entry = request.json
dataframe = pandas.read_csv(csv_file, engine='python', quotechar='|', header=None)
dataset = dataframe.values
X = dataset[:,0]
for index, item in enumerate(X):
reqJson = json.loads(item, object_pairs_hook=OrderedDict)
del reqJson['timestamp']
del reqJson['headers']
del reqJson['source']
del reqJson['route']
del reqJson['responsePayload']
X[index] = json.dumps(reqJson, separators=(',', ':'))
tokenizer = Tokenizer(filters='\t\n', char_level=True)
tokenizer.fit_on_texts(X)
seq = tokenizer.texts_to_sequences([log_entry])
max_log_length = 1024
log_entry_processed = sequence.pad_sequences(seq, maxlen=max_log_length)
model = load_model('model.h5')
model.load_weights('weights.h5')
model.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
prediction = model.predict(log_entry_processed)
return response.text(prediction[0])
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8000, debug=True)
请提出更好的方法来改善 API 响应时间并减少超时请求?