Django Tastypie Postgres缓慢的服务器响应

时间:2015-11-16 22:02:41

标签: python django performance apache postgresql

问题:

  • 服务器对呼叫的响应(由opbeat,DHC chrome客户端测量)是 大约500ms到5000ms。 Postgres查询(总计,每次通话) 比相应的响应时间快约20x-50x。为什么这么慢?!?

背景信息:

  • 我在webfaction上运行了一个实时tastypie API(共享实例,1 gig ram :()用于我们的移动应用.Django 1.8,Python 2.7,0.12.2.dev0 tastypie,PostgreSQL 9.4。,CentOS7
  • db有大约40个表,~2gigs,6k用户(可能有1/3是'活动'),db位于dbs的单独共享webfaction框中。
  • 虽然我们在api中确实有一些过程代码,但许多慢速调用只是对用户资源的GET - 最大/最慢是〜50kb记录,JSON响应中约350个对象。这次调用的postgres时间约为20-50ms,快速专用开发服务器上DHC的最快时间约为2秒 - 2gig ram,2 procs--现场约4秒,最慢约为10-16秒框。
  • 在Apache / mod_wsgi上运行,HTTPS,(apache的基准很快),没有gzip。 httpd.conf设置似乎很好。

简而言之:

  • 99%的时间花在了tastypie.resources.wrapper上 - 我还没有搞乱这段代码。这些调用只是调用返回资源。
  • 数据库查询很快
  • 网络服务器似乎很快。

诊断:

Django调试工具栏(用于在dev专用框上最慢的最慢操作调用)

用户CPU时间1926.979毫秒

系统CPU时间27.074毫秒

总CPU时间1954.053毫秒

经历的时间1980.884毫秒

上下文切换71自愿,44非自愿

的httpd.conf

LoadModule authz_core_module modules/mod_authz_core.so

LoadModule dir_module        modules/mod_dir.so

LoadModule env_module        modules/mod_env.so

LoadModule log_config_module modules/mod_log_config.so

LoadModule mime_module       modules/mod_mime.so

LoadModule rewrite_module    modules/mod_rewrite.so

LoadModule setenvif_module   modules/mod_setenvif.so

LoadModule wsgi_module       modules/mod_wsgi.so

LoadModule unixd_module      modules/mod_unixd.so

...

KeepAlive Off

SetEnvIf X-Forwarded-SSL on HTTPS=1

ServerLimit 1

StartServers 1

MaxRequestWorkers 5

MinSpareThreads 1

MaxSpareThreads 3

ThreadsPerChild 5

...

WSGIApplicationGroup %{GLOBAL}

WSGIDaemonProcess djangoapp processes=2 threads=8 python-path=...

WSGIProcessGroup djangoapp

WSGIRestrictEmbedded On

WSGILazyInitialization On

活动服务器上的Opbeat (这个抓取有更多/更长的数据库查询,我正在测试删除selecte_related()/ prefetch_related() - 它们帮助了数据库查询时间,但没有多少关闭总时间):

Wtf,10张发布图片的声誉?

opbeat performance breakdown chart/graph posted by a n00b

最后的想法:

  • tastypie这么慢吗?当然不是。虽然更好的盒子运行它〜更快,但是花了2秒才能完成db 20ms的操作是否现实?
  • 是的,当我把它放在一个更快,专用的开发盒上时,时间会有所下降,但它们仍然是执行SQL调用所需时间的10-50倍,例如:对于资源GET来说,它们仍然是2秒,这对于db< 50毫秒。所以,它似乎不仅仅是资源问题,AFAIK。
  • 我尝试链接到亚马逊RDS postgres db,但它的时间比db呼叫慢20倍(微/免费等级),而且总往返时间也慢一些。
  • 感谢您的帮助和兴趣 -

1 个答案:

答案 0 :(得分:0)

在我的调查中,var leaseList = (from l in leases.tblfLeaseDetails join a in leases.tblfAuthorizations on l.Lease_Detail_ID equals a.Lease_Detail_ID into la from jla in (from aj in leases.tblfAuthorizations where aj.Lease_Detail_ID == l.Lease_Detail_ID orderby aj.Authorized_Date descending select aj).Take(1).DefaultIfEmpty() join p in leases.tblfPayments on l.Lease_Detail_ID equals p.Lease_Detail_ID into lp from jlp in (from pj in leases.tblfPayments where pj.Lease_Detail_ID == l.Lease_Detail_ID orderby pj.Payment_Date descending select pj).Take(1).DefaultIfEmpty() join v in leases.tblvVendors on l.Vendor_ID equals v.Vendor_ID into lv from jlv in lv.DefaultIfEmpty() join c in leases.tblvCounties on l.County_ID equals c.County_ID into lc from jlc in lc.DefaultIfEmpty() select new LeaseViewModel() { Lease_Detail_ID = l.Lease_Detail_ID, Vendor_Name = jlv.Vendor_Name, County = jlc.County, Authorization = jla.Authorized, Payment_Date = jlp.Payment_Date }).Distinct() 速度很慢,因此功能Tastypie很慢。

我以这种方式从django.core.urlresolvers.reverse辞职并修补resource_uri

django_app /猴/ tastypie.py

Tastypie

django_app /猴/的初始化的.py

from __future__ import absolute_import

from django.core.exceptions import ObjectDoesNotExist
from tastypie.bundle import Bundle
from tastypie.exceptions import ApiFieldError


def dehydrate(self, bundle, for_list=True):
    if not bundle.obj or not bundle.obj.pk:
        if not self.null:
            raise ApiFieldError(
                "The model '%r' does not have a primary key and can not be used in a ToMany context." % bundle.obj)

        return []

    the_m2ms = None
    previous_obj = bundle.obj
    attr = self.attribute

    if isinstance(self.attribute, basestring):
        attrs = self.attribute.split('__')
        the_m2ms = bundle.obj

        for attr in attrs:
            previous_obj = the_m2ms
            try:
                the_m2ms = getattr(the_m2ms, attr, None)
            except ObjectDoesNotExist:
                the_m2ms = None

            if not the_m2ms:
                break

    elif callable(self.attribute):
        the_m2ms = self.attribute(bundle)

    if not the_m2ms:
        if not self.null:
            raise ApiFieldError(
                "The model '%r' has an empty attribute '%s' and doesn't allow a null value." % (previous_obj, attr))

        return []

    self.m2m_resources = []
    m2m_dehydrated = []

    # TODO: Also model-specific and leaky. Relies on there being a
    # ``Manager`` there.
    m2ms = the_m2ms.all() if for_list else the_m2ms.get_query_set().all()
    for m2m in m2ms:
        m2m_resource = self.get_related_resource(m2m)
        m2m_bundle = Bundle(obj=m2m, request=bundle.request)
        self.m2m_resources.append(m2m_resource)
        m2m_dehydrated.append(self.dehydrate_related(m2m_bundle, m2m_resource, for_list=for_list))

    return m2m_dehydrated


def _build_reverse_url(self, name, args=None, kwargs=None):
    return kwargs.get('pk')


def get_via_uri(self, uri, request=None):
    bundle = self.build_bundle(request=request)
    return self.obj_get(bundle=bundle, pk=uri)


def build_related_resource(self, value, request=None, related_obj=None, related_name=None):
    """
    Returns a bundle of data built by the related resource, usually via
    ``hydrate`` with the data provided.

    Accepts either a URI, a data dictionary (or dictionary-like structure)
    or an object with a ``pk``.
    """
    self.fk_resource = self.to_class()
    kwargs = {
        'request': request,
        'related_obj': related_obj,
        'related_name': related_name,
    }

    if isinstance(value, Bundle):
        # Already hydrated, probably nested bundles. Just return.
        return value
    elif isinstance(value, (basestring, int)):
        # We got a URI. Load the object and assign it.
        return self.resource_from_uri(self.fk_resource, value, **kwargs)
    elif isinstance(value, Bundle):
        # We got a valid bundle object, the RelatedField had full=True
        return value
    elif hasattr(value, 'items'):
        # We've got a data dictionary.
        # Since this leads to creation, this is the only one of these
        # methods that might care about "parent" data.
        return self.resource_from_data(self.fk_resource, value, **kwargs)
    elif hasattr(value, 'pk'):
        # We've got an object with a primary key.
        return self.resource_from_pk(self.fk_resource, value, **kwargs)
    else:
        raise ApiFieldError("The '%s' field was given data that was not a URI, not a dictionary-alike and does not have a 'pk' attribute: %s." % (self.instance_name, value))

django_app /的初始化的.py

def patch_tastypie():
    from tastypie.fields import ToManyField, RelatedField
    from tastypie.resources import Resource, ResourceOptions
    from monkey.tastypie import dehydrate, _build_reverse_url, get_via_uri, build_related_resource

    setattr(ToManyField, 'dehydrate', dehydrate)
    setattr(Resource, '_build_reverse_url', _build_reverse_url)
    setattr(Resource, 'get_via_uri', get_via_uri)
    setattr(ResourceOptions, 'include_resource_uri', False)
    setattr(RelatedField, 'build_related_resource', build_related_resource)

它并不完美,但加速from __future__ import absolute_import from .monkey import patch_tastypie patch_tastypie() 10-20%