Azure"App Service" - Django和SQLite

bei*_*ler 6 python sqlite django azure azure-web-app-service

我有一个django应用程序(特别是django-rest).当我运行本网站的本地副本时,我的请求可以在50-400ms内处理.

接下来,我设法部署到Microsoft Azure App Service.现在,在我可以购买的最昂贵的层级下,响应将在800-2000ms范围内回归.

该应用程序对sqlite数据库执行简单查询.此数据库文件大约为30兆字节,最大的表为12000行.

我应该指出对数据库的所有访问权限都是只读的,因此没有争用问题.

组态:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(PROJECT_ROOT, 'mydatabase.db'),
    }
}
Run Code Online (Sandbox Code Playgroud)

web.config中:

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="WSGI_ALT_VIRTUALENV_HANDLER" value="django.core.wsgi.get_wsgi_application()" />
    <add key="WSGI_ALT_VIRTUALENV_ACTIVATE_THIS" value="D:\home\site\wwwroot\env\Scripts\activate_this.py" />
    <add key="WSGI_HANDLER" value="ptvs_virtualenv_proxy.get_virtualenv_handler()" />
    <add key="PYTHONPATH" value="D:\home\site\wwwroot" />
    <add key="DJANGO_SETTINGS_MODULE" value="myapp.settings" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <!-- Required for websockets. -->
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <handlers>
      <remove name="Python273_via_FastCGI" />
      <add name="Python FastCGI" path="handler.fcgi" verb="*" modules="FastCgiModule" scriptProcessor="D:\Python27\python.exe|D:\Python27\Scripts\wfastcgi.py" resourceType="Unspecified" requireAccess="Script" />
    </handlers>
    <rewrite>
      <rules>
        <rule name="Static Files" stopProcessing="true">
          <conditions>
            <add input="true" pattern="false" />
          </conditions>
        </rule>
        <rule name="Configure Python" stopProcessing="true">
          <match url="(.*)" ignoreCase="false" />
          <conditions>
            <add input="{REQUEST_URI}" pattern="^/static/.*" ignoreCase="true" negate="true" />
          </conditions>
          <action type="Rewrite" url="handler.fcgi/{R:1}" appendQueryString="true" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)

Python版本2.7.

我已将其缩小到SQLite性能.静态文件和API索引页面会在~60ms内返回,而最重的查询会在~2000ms内返回.这是Time Till First Byte,而不是整体响应时间,我已经排除了网络延迟(由于地理位置接近而导致网络延迟非常低).这是在P3(Premium Tier)4核,7GB内存(Azure称之为).

在localhost上运行,索引页的响应时间约为15ms,而我的Macbook 2.2 GHz Intel Core i7 16 GB 1600 MHz DDR3上的响应时间约为380ms.

应用程序"热身"不是问题,因为这是在它已经"热"之后(时间基于一些刷新)

更新:我安装了Django Rest Toolbar以获取更多信息.

在macbook django DEV服务器(纯python?):

SQL time ~217ms
Total CPU time ~681ms

Resource    Value
User CPU time   662.771 msec
System CPU time 18.415 msec
Total CPU time  681.186 msec
Elapsed time    681.326 msec
Context switches    1 voluntary, 95 involuntary
Run Code Online (Sandbox Code Playgroud)

在Azure应用服务IIS和FastCGI上(参见上面的配置):

SQL time ~854ms
Total CPU time ~2282ms
No CPU extended breakdown available.
Run Code Online (Sandbox Code Playgroud)

欣赏任何见解!

Pet*_*ain 0

鉴于您的本地和 Azure 测试运行显示从最好到最坏情况的相似倍数,并且您只有 30MB 的数据库文件,我的猜测是您的 Azure 主机的 CPU 速度要慢得多。

他们对某些规格虚拟机进行限制这一事实证明了这一点。与AWS相比,这也是值得注意的。我想您的应用服务平台也是如此。