我查看了Google Chrome控制台的ajax请求时间。 我在后端测量,mysql查询执行5毫秒。 在Chrome控制台,我看到this picture
TTFB时间333.07毫秒。 我有9个gunicorn worker,Django框架和REST框架。什么需要这么多时间?
例如, 我的观点:
@api_view(['GET'])
def get_gallery(request, slug):
query = Gallery.objects.filter(route__slug=slug, route__is_active=True)
return JSONResponse(GallerySerializer(query, many=True).data)
class JSONResponse(HttpResponse):
"""
An HttpResponse that renders its content into JSON.
"""
def __init__(self, data, **kwargs):
content = JSONRenderer().render(data)
kwargs['content_type'] = 'application/json; charset=utf-8'
super(JSONResponse, self).__init__(content, **kwargs)
我的序列化器:
class GallerySerializer(ModelSerializerWithAuth):
image = serializers.ImageField(use_url=False)
thumb = serializers.ImageField(use_url=False)
class Meta:
model = Gallery
fields = ('id', 'image', 'thumb')
gunicorn配置:
bind = '127.0.0.1:9090'
errorlog = '/path/to/log'
timeout=120
user = 'user'
import multiprocessing
workers = multiprocessing.cpu_count() * 2 + 1