我对我的 Linux Centos Apache 服务器的性能有疑问。我有一个程序(用 c 编写)可以同时执行许多 http 请求。这个过程本身似乎非常有效,就好像我可以同时向外部服务器发出 500 个请求,而与只有 1 个请求相比,没有任何明显的时间差异。
但是,我在同一台服务器上有许多脚本,我使用同一个程序同时运行这些脚本。要运行的脚本数量各不相同,但一次搜索大约为 100 个。
每个脚本的任务是调用 API(在外部服务器上)解析所需的数据并将其插入数据库。
我测量了每个脚本的开始时间,我注意到每个脚本的开始都有很大的延迟。第一个脚本的开始和最后一个脚本的开始之间最多有 10 秒的差异。这种大的时间延迟使我网站上的搜索变慢。
我在我的 linux centos 机器上使用了 top 命令,在搜索过程中查看下面 2 个不同实例的 2 个示例。
顶级命令示例 1:
top - 18:51:18 up 36 days, 3:35, 1 user, load average: 0.02, 0.07, 0.08
Tasks: 182 total, 2 running, 180 sleeping, 0 stopped, 0 zombie
Cpu(s): 3.7%us, 1.3%sy, 0.0%ni, 94.9%id, 0.0%wa, 0.0%hi, 0.1%si, 0.0%st
Mem: 4194304k total, 3941184k used, 253120k free, 26820k buffers
Swap: 4194296k total, 76k used, 4194220k free, 2069456k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
691 apache 15 0 190m 18m 5052 S 4.0 0.5 0:00.19 httpd
959 apache 17 0 189m 15m 3196 R 3.0 0.4 0:00.09 httpd
702 apache 15 0 185m 101m 5036 S 1.7 2.5 0:00.86 httpd
732 apache 15 0 184m 12m 5036 S 1.7 0.3 0:00.15 httpd
689 apache 15 0 184m 14m 5144 S 0.7 0.3 0:00.87 httpd
734 apache 15 0 184m 100m 4740 S 0.7 2.4 0:00.21 httpd
670 apache 15 0 205m 99m 4992 S 0.3 2.4 0:00.39 httpd
678 apache 15 0 184m 13m 5032 S 0.3 0.3 0:01.05 httpd
795 root 15 0 12764 1356 956 R 0.3 0.0 0:00.03 top
949 apache 15 0 181m 9616 2928 S 0.3 0.2 0:00.01 httpd
951 apache 20 0 180m 8748 2640 S 0.3 0.2 0:00.01 httpd
1 root 15 0 10372 792 664 S 0.0 0.0 0:00.20 init
2 root RT -5 0 0 0 S 0.0 0.0 0:00.14 migration/0
3 root 34 19 0 0 0 S 0.0 0.0 0:00.04 ksoftirqd/0
4 root RT -5 0 0 0 S 0.0 0.0 0:00.00 watchdog/0
5 root 10 -5 0 0 0 S 0.0 0.0 0:00.04 events/0
6 root 10 -5 0 0 0 S 0.0 0.0 0:00.00 khelper
7 root 10 -5 0 0 0 S 0.0 0.0 0:00.00 kthread
9 root 10 -5 0 0 0 S 0.0 0.0 0:00.00 xenwatch
10 root 10 -5 0 0 0 S 0.0 0.0 0:00.00 xenbus
37 root 10 -5 0 0 0 S 0.0 0.0 0:00.03 kblockd/0
42 root 20 -5 0 0 0 S 0.0 0.0 0:00.00 cqueue/0
50 root 20 -5 0 0 0 S 0.0 0.0 0:00.00 khubd
52 root 10 -5 0 0 0 S 0.0 0.0 0:00.00 kseriod
137 root 15 0 0 0 0 S 0.0 0.0 0:00.00 khungtas
顶级命令示例 2:
top - 18:52:49 up 36 days, 3:36, 1 user, load average: 0.53, 0.21, 0.12
Tasks: 240 total, 8 running, 231 sleeping, 0 stopped, 1 zombie
Cpu(s): 50.4%us, 4.8%sy, 0.0%ni, 43.5%id, 0.5%wa, 0.0%hi, 0.5%si, 0.2%st
Mem: 4194304k total, 4097104k used, 97200k free, 27148k buffers
Swap: 4194296k total, 76k used, 4194220k free, 1965428k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
949 apache 16 0 185m 101m 5328 S 32.1 2.5 0:01.19 httpd
1229 apache 16 0 184m 12m 4580 S 32.1 0.3 0:00.98 httpd
968 apache 17 0 188m 17m 4732 S 30.4 0.4 0:01.92 httpd
1244 apache 17 0 184m 12m 4580 S 27.8 0.3 0:00.86 httpd
994 apache 16 0 190m 19m 5060 S 27.5 0.5 0:01.69 httpd
1222 apache 16 0 218m 44m 4676 R 26.5 1.1 0:00.82 httpd
1657 mysql 15 0 627m 223m 5664 S 23.5 5.5 65:16.63 mysqld
1256 apache 16 0 184m 12m 4580 S 21.2 0.3 0:00.81 httpd
1245 apache 16 0 210m 37m 4084 R 14.6 0.9 0:00.47 httpd
1005 apache 16 0 213m 42m 5308 R 13.6 1.0 0:00.67 httpd
1246 apache 17 0 184m 12m 4580 S 11.3 0.3 0:00.74 httpd
1214 apache 16 0 182m 10m 4060 S 3.3 0.3 0:00.23 httpd
1253 apache 16 0 184m 12m 4580 S 2.3 0.3 0:00.67 httpd
1233 apache 15 0 196m 22m 3696 R 2.0 0.6 0:00.17 httpd
1215 apache 15 0 183m 11m 4060 S 1.7 0.3 0:00.18 httpd
1265 apache 15 0 182m 11m 3444 S 1.7 0.3 0:00.05 httpd
1230 apache 16 0 180m 9644 3436 S 1.3 0.2 0:00.04 httpd
1210 apache 15 0 192m 19m 3620 S 1.0 0.5 0:00.14 httpd
1011 apache 15 0 193m 22m 5356 R 0.7 0.5 0:00.86 httpd
1016 apache 15 0 192m 19m 4092 S 0.7 0.5 0:00.20 httpd
1019 apache 15 0 192m 21m 4972 S 0.7 0.5 0:01.27 httpd
1051 root 15 0 12896 1424 956 R 0.7 0.0 0:00.10 top
1221 apache 15 0 180m 9820 3436 S 0.7 0.2 0:00.03 httpd
989 apache 15 0 193m 21m 5332 R 0.3 0.5 0:01.06 httpd
1000 apache 15 0 208m 102m 5424 S 0.3 2.5 0:00.97 httpd
1032 apache 15 0 190m 18m 4748 S 0.3 0.4 0:00.39 httpd
1213 apache 15 0 0 0 0 Z 0.3 0.0 0:00.15 httpd <defunct>
1251 apache 15 0 184m 11m 3700 S 0.3 0.3 0:00.02 httpd
1 root 15 0 10372 792 664 S 0.0 0.0 0:00.20 init
2 root RT -5 0 0 0 S 0.0 0.0 0:00.14 migration/0
3 root 34 19 0 0 0 S 0.0 0.0 0:00.04 ksoftirqd/0
4 root RT -5 0 0 0 S 0.0 0.0 0:00.00 watchdog/0
5 root 10 -5 0 0 0 S 0.0 0.0 0:00.04 events/0
我还看了一下磁盘IO,有很多进程,但加起来IO并不多:
Total DISK READ: 0.00 B/s | Total DISK WRITE: 0.00 B/s
TID PRIO USER DISK READ DISK WRITE SWAPIN IO> COMMAND
2804 be/3 root 0.00 B/s 0.00 B/s 0.00 % 1.13 % [ib_cm/1]
32387 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.75 % httpd -k start -DSSL
32344 be/4 apache 0.00 B/s 3.77 K/s 0.00 % 0.75 % httpd -k start -DSSL
32465 be/4 apache 0.00 B/s 0.00 B/s -0.75 % 0.75 % httpd -k start -DSSL
32487 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.75 % httpd -k start -DSSL
32377 be/4 apache 0.00 B/s 3.77 K/s 0.00 % 0.38 % httpd -k start -DSSL
32462 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.38 % httpd -k start -DSSL
32469 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.38 % httpd -k start -DSSL
32445 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.38 % httpd -k start -DSSL
32349 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.00 % httpd -k start -DSSL
32436 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % python /usr/bin/iotop
32385 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.00 % httpd -k start -DSSL
32382 be/4 apache 0.00 B/s 3.77 K/s 0.38 % 0.00 % httpd -k start -DSSL
32446 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.00 % httpd -k start -DSSL
32381 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.00 % httpd -k start -DSSL
32375 be/4 apache 0.00 B/s 0.00 B/s 0.75 % 0.00 % httpd -k start -DSSL
32312 be/4 apache 0.00 B/s 3.77 K/s 0.00 % 0.00 % httpd -k start -DSSL
32342 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.00 % httpd -k start -DSSL
1407 be/4 dbus 0.00 B/s 0.00 B/s 0.00 % 0.00 % dbus-daemon --system
32455 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.00 % httpd -k start -DSSL
32466 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.00 % httpd -k start -DSSL
32470 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.00 % httpd -k start -DSSL
32488 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.00 % httpd -k start -DSSL
25045 be/4 dovecot 0.00 B/s 0.00 B/s 0.00 % 0.00 % dovecot/pop3-login
4 rt/3 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [watchdog/0]
32514 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.00 % httpd -k start -DSSL
32581 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.00 % httpd -k start -DSSL
32531 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.00 % httpd -k start -DSSL
32471 be/4 apache 0.00 B/s 0.00 B/s 0.38 % 0.00 % httpd -k start -DSSL
32546 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.00 % httpd -k start -DSSL
32519 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.00 % httpd -k start -DSSL
32521 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.00 % httpd -k start -DSSL
32315 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.00 % httpd -k start -DSSL
32523 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.00 % httpd -k start -DSSL
32524 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.00 % httpd -k start -DSSL
32335 be/4 apache 0.00 B/s 3.77 K/s 0.00 % 0.00 % httpd -k start -DSSL
32583 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.00 % httpd -k start -DSSL
32566 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.00 % httpd -k start -DSSL
32526 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.00 % httpd -k start -DSSL
32481 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.00 % httpd -k start -DSSL
32557 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.00 % httpd -k start -DSSL
32529 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.00 % httpd -k start -DSSL
32530 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.00 % httpd -k start -DSSL
32541 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.00 % httpd -k start -DSSL
32507 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.00 % httpd -k start -DSSL
32570 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.00 % httpd -k start -DSSL
32504 be/4 apache 0.00 B/s 0.00 B/s 0.00 % 0.00 % httpd -k start -DSSL
我还测试了我的 httpd.conf 的不同设置:
测试 HTTPD.CONF 1:
<IfModule mpm_prefork_module>
StartServers 2
MinSpareServers 2
MaxSpareServers 5
ServerLimit 200
MaxClients 200
MaxRequestsPerChild 100
</IfModule>
测试 HTTPD.CONF 2:
<IfModule mpm_prefork_module>
StartServers 2
MinSpareServers 2
MaxSpareServers 5
ServerLimit 2
MaxClients 2
MaxRequestsPerChild 1
</IfModule>
以上似乎对性能没有任何影响。
我还检查了是否安装了 mpm 模块(这似乎是这种情况),见下文。但是我真的不明白上述设置如何对我的服务器性能没有任何影响。
-bash-3.2# httpd -M
[Wed Mar 11 18:59:42 2015] [warn] module php5_module is already loaded, skipping
Loaded Modules:
core_module (static)
authn_file_module (static)
authn_default_module (static)
authz_host_module (static)
authz_groupfile_module (static)
authz_user_module (static)
authz_default_module (static)
auth_basic_module (static)
reqtimeout_module (static)
include_module (static)
filter_module (static)
deflate_module (static)
log_config_module (static)
logio_module (static)
env_module (static)
expires_module (static)
headers_module (static)
unique_id_module (static)
setenvif_module (static)
version_module (static)
proxy_module (static)
proxy_connect_module (static)
proxy_ftp_module (static)
proxy_http_module (static)
proxy_scgi_module (static)
proxy_ajp_module (static)
proxy_balancer_module (static)
ssl_module (static)
mpm_prefork_module (static)
http_module (static)
mime_module (static)
dav_module (static)
status_module (static)
autoindex_module (static)
asis_module (static)
suexec_module (static)
cgi_module (static)
dav_fs_module (static)
dav_lock_module (static)
negotiation_module (static)
dir_module (static)
actions_module (static)
userdir_module (static)
alias_module (static)
rewrite_module (static)
so_module (static)
php5_module (shared)
Syntax OK
我真的迷失在寻找可能成为这种性能瓶颈的地方。重要的是,当我将 exit 放在所有 100 个脚本的顶部时,没有时间延迟,但是当脚本执行所有 curl 请求和数据解析时,会有很大的时间延迟。
非常欢迎所有帮助!