Django/Apache/mod_wsgi 站点上的高 CPU 负载

lit*_*bug 10 django mod-wsgi apache-2.2

使用 Apache bench 在 AWS 小实例 (Ubuntu 10.04) 上对 django 1.21/Apache/mod_wsgi 配置进行负载测试时,在低并发请求下显示出极高的 CPU 负载(使用正常运行时间和 vmstat):

ab -c 5 -n 1000 "my_url"
Run Code Online (Sandbox Code Playgroud)

...导致此正常运行时间输出:

18:04:54 up 9 days, 16:54,  3 users,  load average: 5.33, 2.45, 1.91
Run Code Online (Sandbox Code Playgroud)

即使 Apache bench 并发值为 2,CPU 仍为 100%。我正在同一区域/区域中的不同 AWS 实例中运行 Apache bench。关于问题出在哪里的想法,或者我应该如何继续调试?

细节:

  • 出于绝望,我安装了一个带有简单“Hello World”视图(没有 DB 调用等)的 vanilla django 项目/应用程序。结果一样。所以我怀疑这是我的应用程序代码。
  • 在负载测试期间内存使用情况看起来不错。

这是负载测试之前/期间/之后的 vmstat 输出:

procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
0  0      0 1034484  94848 321320    0    0     0     0   13   29  0  0 100  0
6  0      0 1032916  94848 321328    0    0     0     0  262  720  4 32 12  0
6  0      0 1031684  94848 321336    0    0     0     0  312  796  7 33  0  0
8  0      0 1030892  94856 321344    0    0     0    12  302  763  4 36  0  0
...
6  0      0 1030268  94864 321376    0    0     0     0  302  843  3 39  0  0
0  0      0 1032452  94868 321380    0    0     0    12  183  516  3 22 34  0
1  0      0 1033988  94868 321388    0    0     0     0   24   38  1  2 92  0
0  0      0 1033996  94868 321388    0    0     0     0   17   28  0  0 100  0
Run Code Online (Sandbox Code Playgroud)
  • 我正在运行 apache2 的 prefork 版本,因为我也在运行 WordPress,它依赖于 PHP。(PHP 不能很好地与 Apache worker 版本配合使用)

这是我的虚拟主机文件:

WSGIPythonHome /home/xxx/webapps/ve/api
<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName  app.xxx.mobi

        WSGIDaemonProcess snaplive user=www-data group=www-data processes=10 threads=1 maximum-requests=10000
        WSGIProcessGroup snaplive
        WSGIScriptAlias / /home/xxx/webapps/api/settings/apache/prod.wsgi
        DocumentRoot /home/xxx/webapps/api/static
        ErrorLog /var/log/apache2/django-live/error.log
        CustomLog /var/log/apache2/django-live/access.log combined
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

这是我的 httpd.conf 文件:

Alias /media /home/xxx/Django-1.2.1/django/contrib/admin/media
LoadModule headers_module /usr/lib/apache2/modules/mod_headers.so

StartServers 2
MinSpareServers 2
MaxSpareServers 5
MaxClients 50
MaxRequestsPerChild 3000
ServerLimit 8
Keepalive off
HostnameLookups Off
Run Code Online (Sandbox Code Playgroud)

这是我的 wsgi 文件:

import os
import sys
sys.stdout = sys.stderr

from django.core.handlers.wsgi import WSGIHandler
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
application = WSGIHandler()

sys.path.append("/home/xxx/webapps/api")
Run Code Online (Sandbox Code Playgroud)

通过在负载测试期间从浏览器访问 django url,我已经定性地确认高 CPU 负载正在影响性能。

我读过这可能并不重要,但我在错误日志中看到了很多:

[Sun Sep 19 18:04:58 2010] [error] Exception KeyError: KeyError(-1218693376,) in <module 'threading' from '/usr/lib/python2.6/threading.pyc'> ignored
Run Code Online (Sandbox Code Playgroud)

这是我的 Apache 工作台结果,以防万一:

Server Software:        Apache/2.2.14
Server Hostname:        app.xxx.mobi
Server Port:            80

Document Path:          /plist_catalog/test_data
Document Length:        0 bytes

Concurrency Level:      5
Time taken for tests:   27.720 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Non-2xx responses:      1000
Total transferred:      269000 bytes
HTML transferred:       0 bytes
Requests per second:    36.08 [#/sec] (mean)
Time per request:       138.598 [ms] (mean)
Time per request:       27.720 [ms] (mean, across all concurrent requests)
Transfer rate:          9.48 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1    2   8.5      1      88
Processing:     9  136 176.9     81    1182
Waiting:        9  135 176.6     81    1182
Total:         10  138 176.7     83    1183

Percentage of the requests served within a certain time (ms)
  50%     83
  66%     98
  75%    128
  80%    140
  90%    423
  95%    576
  98%    727
  99%    819
 100%   1183 (longest request)
Run Code Online (Sandbox Code Playgroud)

lit*_*bug 2

问题是我安装了软件包 apache2-mpm-itk 而不是 apache2-mpm-prefork。apache2-mpm-itk 源自 apache2-mpm-prefork,但由于某种原因,与 mod_wsgi 一起使用时表现不佳。