我在 Linode 上运行了一台 VPS,内存为 1024 MB,Ubuntu 11.04,Nginx+PHP-FPM+APC Varnish+Memcached 处于活动状态。我使用 WordPress 3.2.1 设置了几个测试博客,一切正常,性能测试似乎很有希望:
# ab -n 1000 -c 100 http://mydomain.com/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking mydomain.com (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
Server Software: nginx
Server Hostname: mydomain.com
Server Port: 80
Document Path: /
Document Length: 5677 bytes
Concurrency Level: 100
Time taken for tests: 0.186 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 5989000 bytes
HTML transferred: 5677000 bytes
Requests per second: 5376.14 [#/sec] (mean)
Time per request: 18.601 [ms] (mean)
Time per request: 0.186 [ms] (mean, across all concurrent requests)
Transfer rate: 31443.08 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 6 9 0.9 8 13
Processing: 4 9 1.3 9 14
Waiting: 1 5 2.5 5 13
Total: 12 18 1.6 18 22
WARNING: The median and mean for the initial connection time are not within a normal deviation
These results are probably not that reliable.
Percentage of the requests served within a certain time (ms)
50% 18
66% 19
75% 19
80% 19
90% 20
95% 21
98% 22
99% 22
100% 22 (longest request)
安装 Memcached 和 php5-memcache 包并启用 WP Memcache 插件后,我可以看到 memcache 服务器已启动并运行,命中率约为 50%。memcache 服务正在使用 64 MB 内存:
# ps xa |grep memcached
12259 ? Sl 0:01 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1
但是,我看到的是主内存和交换的消耗很高,我想知道这是否正常。服务器大部分时间处于空闲状态(测试博客正在运行,但没有人知道它们,所以我是唯一的访问者),所以我不明白为什么内存和交换如此之高。
#free -m
total used free shared buffers cached
Mem: 927 906 21 0 6 58
-/+ buffers/cache: 840 87
Swap: 255 255 0
有任何想法吗?
更新:看来 Memcached 不是原因。我已停止服务器,网站当然仍在运行,但内存/交换消耗仍接近 100%。所以应该存在问题,可能是 PHP-FPM 或 Varnish 的问题?
更新 2:看来 PHP-FPM 是问题所在,看看这个(感谢 Tomtezky 提供的命令)
# ps aux --sort=-rss | head -15
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
www-data 12414 0.0 9.6 247840 91772 ? S 01:25 0:06 php-fpm: pool www
www-data 12405 0.0 9.5 244012 91212 ? S 01:25 0:06 php-fpm: pool www
www-data 12418 0.0 9.0 241820 86132 ? S 01:25 0:05 php-fpm: pool www
www-data 12417 0.0 8.5 236724 81500 ? S 01:25 0:07 php-fpm: pool www
www-data 12409 0.0 8.2 241980 78784 ? S 01:25 0:05 php-fpm: pool www
www-data 12407 0.0 8.1 236968 77880 ? S 01:25 0:05 php-fpm: pool www
www-data 12400 0.0 8.1 234344 77860 ? S 01:25 0:06 php-fpm: pool www
www-data 12408 0.0 8.1 237120 77700 ? S 01:25 0:06 php-fpm: pool www
www-data 12410 0.0 8.0 232248 76224 ? S 01:25 0:06 php-fpm: pool www
www-data 12413 0.0 7.6 242092 72452 ? S 01:25 0:07 php-fpm: pool www
www-data 12416 0.0 7.1 241812 68148 ? S 01:25 0:05 php-fpm: pool www
www-data 12412 0.0 7.0 220684 66644 ? S 01:25 0:04 php-fpm: pool www
www-data 12402 0.0 6.9 241556 66156 ? S 01:25 0:05 php-fpm: pool www
www-data 12419 0.0 6.2 238016 59304 ? S 01:25 0:04 php-fpm: pool www
看到了吗...如何减少 PHP-FPM 的内存使用?
答案1
这不是 PHP-FPM,而是在 PHP-FPM 中加载的 Wordpress 实例。限制 php-fpm 工作者的数量,或减少 wordpress 安装的内存占用。