笺注:这是在 CentOS8_lnmp1.7_LAMP 的基础上进行的。
查看Apache的版本:
[root@centos8 ~]# httpd -v
-bash: httpd: 未找到命令
[root@centos8 ~]# find / -name httpd
/etc/rc.d/init.d/httpd
/usr/local/apache/bin/httpd
做个软链接:
[root@centos8 ~]# ln -s /usr/local/apache/bin/httpd /usr/sbin
[root@centos8 ~]#
[root@centos8 ~]# httpd -v
Server version: Apache/2.4.46 (Unix)
Server built: Feb 27 2021 18:34:26
操作系统的“进程最大可打开文件数”的查询方法:
[root@centos8 ~]# ulimit -n
65535
查看Apache当前使用的MPM模式:(默认就是使用event)
[root@centos8 ~]# httpd -V
Server version: Apache/2.4.46 (Unix)
Server built: Feb 27 2021 18:34:26
Server's Module Magic Number: 20120211:93
Server loaded: APR 1.7.0, APR-UTIL 1.6.1
Compiled using: APR 1.7.0, APR-UTIL 1.6.1
Architecture: 64-bit
Server MPM: event
threaded: yes (fixed thread count)
forked: yes (variable process count)
Server compiled with....
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_PROC_PTHREAD_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=256
-D HTTPD_ROOT="/usr/local/apache"
-D SUEXEC_BIN="/usr/local/apache/bin/suexec"
-D DEFAULT_PIDLOG="logs/httpd.pid"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="conf/mime.types"
-D SERVER_CONFIG_FILE="conf/httpd.conf"
Apache的主配置文件:
[root@centos8 ~]# find / -name "httpd.conf"
/usr/local/apache/conf/original/httpd.conf
/usr/local/apache/conf/httpd.conf
默认就是使用event的so文件:
[root@centos8 ~]# cat /usr/local/apache/conf/httpd.conf |grep 'mpm_'
#LoadModule mpm_worker_module modules/mod_mpm_worker.so
LoadModule mpm_event_module modules/mod_mpm_event.so
#LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
查找MPM模式的配置文件:
[root@centos8 ~]# cat /usr/local/apache/conf/httpd.conf |grep "httpd-mpm.conf"
Include conf/extra/httpd-mpm.conf
MPM模式的配置文件:
[root@centos8 ~]# vi /usr/local/apache/conf/extra/httpd-mpm.conf
event MPM:
测试:
[root@centos8 ~]# ab
-bash: ab: 未找到命令
[root@centos8 ~]# yum -y install httpd-tools
[root@centos8 ~]# ab -n 1000 -c 400 http://127.0.0.1/p.php #指定并发请求数为400,总请求数为1000,对http://127.0.0.1/p.php进行压力测试
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (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: Apache
Server Hostname: 127.0.0.1
Server Port: 80
Document Path: /p.php
Document Length: 21174 bytes
Concurrency Level: 400
Time taken for tests: 1.970 seconds
Complete requests: 1000
Failed requests: 999
(Connect: 0, Receive: 0, Length: 999, Exceptions: 0)
Total transferred: 21392939 bytes
HTML transferred: 21186939 bytes
Requests per second: 507.66 [#/sec] (mean)
Time per request: 787.925 [ms] (mean)
Time per request: 1.970 [ms] (mean, across all concurrent requests)
Transfer rate: 10605.86 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 185 364.6 11 1077
Processing: 88 373 227.9 307 1363
Waiting: 2 371 228.6 304 1362
Total: 95 558 492.2 387 1761
Percentage of the requests served within a certain time (ms)
50% 387
66% 454
75% 551
80% 725
90% 1670
95% 1708
98% 1742
99% 1758
100% 1761 (longest request)
笺注:可以看到每秒最大处理请求次数为507.66次,每个请求响应时间是1.970毫秒。
###
加大并发请求数、总请求数进行测试:(并发请求数为1000,总请求数为3000)
[root@centos8 ~]# ab -n 3000 -c 1000 http://127.0.0.1/p.php
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient)
Completed 300 requests
Completed 600 requests
Completed 900 requests
Completed 1200 requests
Completed 1500 requests
Completed 1800 requests
Completed 2100 requests
Completed 2400 requests
Completed 2700 requests
Completed 3000 requests
Finished 3000 requests
Server Software: Apache
Server Hostname: 127.0.0.1
Server Port: 80
Document Path: /p.php
Document Length: 21187 bytes
Concurrency Level: 1000
Time taken for tests: 19.255 seconds
Complete requests: 3000
Failed requests: 2443
(Connect: 0, Receive: 0, Length: 2443, Exceptions: 0)
Total transferred: 64182690 bytes
HTML transferred: 63564690 bytes
Requests per second: 155.80 [#/sec] (mean)
Time per request: 6418.342 [ms] (mean)
Time per request: 6.418 [ms] (mean, across all concurrent requests)
Transfer rate: 3255.17 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 418 939.8 109 3738
Processing: 45 1549 3471.4 452 19213
Waiting: 2 1545 3472.2 448 19213
Total: 45 1968 3482.6 608 19229
Percentage of the requests served within a certain time (ms)
50% 608
66% 722
75% 1355
80% 1893
90% 4623
95% 9580
98% 19217
99% 19217
100% 19229 (longest request)
笺注:可以看到每秒最大处理请求次数为155.80次,每个请求响应时间是6.418毫秒。
###
加大并发请求数、总请求数进行测试:(并发请求数为2000,总请求数为3000)
[root@centos8 ~]# ab -n 3000 -c 2000 http://127.0.0.1/p.php
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient)
Completed 300 requests
Completed 600 requests
Completed 900 requests
Completed 1200 requests
Completed 1500 requests
Completed 1800 requests
Completed 2100 requests
Completed 2400 requests
Completed 2700 requests
apr_socket_recv: Connection reset by peer (104)
Total of 2865 requests completed
修改event MPM的参数值:
[root@centos8 ~]# vi /usr/local/apache/conf/extra/httpd-mpm.conf
<IfModule mpm_event_module>
StartServers 6
MinSpareThreads 16
MaxSpareThreads 1024
ThreadsPerChild 128
ServerLimit 128
MaxRequestWorkers 16384
MaxConnectionsPerChild 10000
</IfModule>
如下图:
注释:
StartServers #默认启动的进程数
MinSpareThreads #最小线程数
MaxSpareThreads #最大线程数
ThreadsPerChild #最大子线程数
ServerLimit #最大进程数
MaxRequestWorkers #最大子进程数,即最大并发连接数,超过的连接请求将排队等候处理;当并发连接数超过了系统的承受能力时,系统会卡死。
MaxConnectionsPerChild #进程生命周期内,处理的最大请求数目。达到该数目后,进程将死掉。如果设置为0,表示没有限制。该参数的意义在于,避免了可能存在的内存泄露问题。
优化的计算公式:
MaxSpareThreads > StartServers * ThreadsPerChild
MaxRequestWorkers = ServerLimit * ThreadsPerChild
重启服务器:
[root@centos8 ~]# reboot
###
加大并发请求数、总请求数进行测试:(并发请求数为2000,总请求数为3000)
[root@centos8 ~]# ab -n 3000 -c 2000 http://127.0.0.1/p.php
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient)
Completed 300 requests
Completed 600 requests
Completed 900 requests
Completed 1200 requests
Completed 1500 requests
Completed 1800 requests
Completed 2100 requests
Completed 2400 requests
Completed 2700 requests
Completed 3000 requests
Finished 3000 requests
Server Software: Apache
Server Hostname: 127.0.0.1
Server Port: 80
Document Path: /p.php
Document Length: 21173 bytes
Concurrency Level: 2000
Time taken for tests: 26.959 seconds
Complete requests: 3000
Failed requests: 2999
(Connect: 0, Receive: 0, Length: 2999, Exceptions: 0)
Total transferred: 64179692 bytes
HTML transferred: 63561692 bytes
Requests per second: 111.28 [#/sec] (mean)
Time per request: 17972.804 [ms] (mean)
Time per request: 8.986 [ms] (mean, across all concurrent requests)
Transfer rate: 2324.83 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1100 1406.6 225 3909
Processing: 23 2534 3999.8 1258 26881
Waiting: 7 2382 4024.4 1254 26881
Total: 79 3634 3876.9 2419 26922
Percentage of the requests served within a certain time (ms)
50% 2419
66% 3951
75% 4698
80% 5169
90% 7069
95% 13770
98% 14128
99% 22274
100% 26922 (longest request)
笺注:可以看到每秒最大处理请求次数为111.28次,每个请求响应时间是8.986毫秒。
###
加大并发请求数、总请求数进行测试:(并发请求数为3001,总请求数为4000)
[root@centos8 ~]# ab -n 4000 -c 3001 http://127.0.0.1/p.php
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient)
Completed 400 requests
Completed 800 requests
Completed 1200 requests
Completed 1600 requests
Completed 2000 requests
Completed 2400 requests
Completed 2800 requests
Completed 3200 requests
Completed 3600 requests
Completed 4000 requests
Finished 4000 requests
Server Software: Apache
Server Hostname: 127.0.0.1
Server Port: 80
Document Path: /p.php
Document Length: 21177 bytes
Concurrency Level: 3001
Time taken for tests: 40.682 seconds
Complete requests: 4000
Failed requests: 3999
(Connect: 0, Receive: 0, Length: 3999, Exceptions: 0)
Total transferred: 85583662 bytes
HTML transferred: 84759662 bytes
Requests per second: 98.32 [#/sec] (mean)
Time per request: 30521.983 [ms] (mean)
Time per request: 10.171 [ms] (mean, across all concurrent requests)
Transfer rate: 2054.40 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1364 1366.1 1450 3525
Processing: 233 5006 7363.1 2196 39136
Waiting: 6 4853 7414.4 1964 39136
Total: 243 6371 7623.4 4199 40597
Percentage of the requests served within a certain time (ms)
50% 4199
66% 6625
75% 7486
80% 8589
90% 14085
95% 21252
98% 40517
99% 40587
100% 40597 (longest request)
笺注:可以看到每秒最大处理请求次数为98.32次,每个请求响应时间是10.171毫秒。
###
加大并发请求数、总请求数进行测试:(并发请求数为5001,总请求数为8000)
[root@centos8 ~]# ab -n 8000 -c 5001 http://127.0.0.1/p.php
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient)
Completed 800 requests
Completed 1600 requests
Completed 2400 requests
Completed 3200 requests
Completed 4000 requests
Completed 4800 requests
Completed 5600 requests
Completed 6400 requests
Completed 7200 requests
apr_pollset_poll: The timeout specified has expired (70007)
Total of 7655 requests completed
笺注:Apache的最大并发连接数也受服务器性能的限制。
############
修改Apache的主配置文件,使用prefork的so文件:
[root@centos8 ~]# cat /usr/local/apache/conf/httpd.conf |grep 'mpm_'
#LoadModule mpm_worker_module modules/mod_mpm_worker.so
#LoadModule mpm_event_module modules/mod_mpm_event.so
LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
重启Apache:
[root@centos8 ~]# systemctl restart httpd
查看Apache当前使用的MPM模式:(现在使用prefork)
[root@centos8 ~]# httpd -V
Server version: Apache/2.4.46 (Unix)
Server built: Feb 27 2021 18:34:26
Server's Module Magic Number: 20120211:93
Server loaded: APR 1.7.0, APR-UTIL 1.6.1
Compiled using: APR 1.7.0, APR-UTIL 1.6.1
Architecture: 64-bit
Server MPM: prefork
threaded: no
forked: yes (variable process count)
Server compiled with....
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_PROC_PTHREAD_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=256
-D HTTPD_ROOT="/usr/local/apache"
-D SUEXEC_BIN="/usr/local/apache/bin/suexec"
-D DEFAULT_PIDLOG="logs/httpd.pid"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="conf/mime.types"
-D SERVER_CONFIG_FILE="conf/httpd.conf"
MPM模式的配置文件:
[root@centos8 ~]# vi /usr/local/apache/conf/extra/httpd-mpm.conf
prefork MPM:
测试:(并发请求数为200,总请求数为1000)
[root@centos8 ~]# ab -n 1000 -c 200 http://127.0.0.1/p.php
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (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: Apache
Server Hostname: 127.0.0.1
Server Port: 80
Document Path: /p.php
Document Length: 21196 bytes
Concurrency Level: 200
Time taken for tests: 13.495 seconds
Complete requests: 1000
Failed requests: 618
(Connect: 0, Receive: 0, Length: 618, Exceptions: 0)
Total transferred: 21400808 bytes
HTML transferred: 21194808 bytes
Requests per second: 74.10 [#/sec] (mean)
Time per request: 2698.912 [ms] (mean)
Time per request: 13.495 [ms] (mean, across all concurrent requests)
Transfer rate: 1548.72 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 3 47.1 0 1060
Processing: 14 1109 3355.4 209 13480
Waiting: 4 1109 3355.4 209 13480
Total: 15 1112 3356.5 209 13485
Percentage of the requests served within a certain time (ms)
50% 209
66% 210
75% 212
80% 213
90% 215
95% 13410
98% 13456
99% 13471
100% 13485 (longest request)
笺注:可以看到每秒最大处理请求次数为74.10次,每个请求响应时间是13.495毫秒。
###
加大并发请求数、总请求数进行测试:(并发请求数为1001,总请求数为2000)
[root@centos8 ~]# ab -n 2000 -c 1001 http://127.0.0.1/p.php
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient)
Completed 200 requests
Completed 400 requests
Completed 600 requests
Completed 800 requests
Completed 1000 requests
Completed 1200 requests
Completed 1400 requests
Completed 1600 requests
Completed 1800 requests
Completed 2000 requests
Finished 2000 requests
Server Software: Apache
Server Hostname: 127.0.0.1
Server Port: 80
Document Path: /p.php
Document Length: 21195 bytes
Concurrency Level: 1001
Time taken for tests: 30.733 seconds
Complete requests: 2000
Failed requests: 1572
(Connect: 0, Receive: 0, Length: 1572, Exceptions: 0)
Total transferred: 42803570 bytes
HTML transferred: 42391570 bytes
Requests per second: 65.08 [#/sec] (mean)
Time per request: 15381.783 [ms] (mean)
Time per request: 15.366 [ms] (mean, across all concurrent requests)
Transfer rate: 1360.12 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 398 511.0 17 1072
Processing: 12 4988 9080.3 224 30707
Waiting: 2 4987 9080.3 224 30707
Total: 28 5385 9221.3 225 30726
Percentage of the requests served within a certain time (ms)
50% 225
66% 2902
75% 4659
80% 7922
90% 27967
95% 30577
98% 30667
99% 30697
100% 30726 (longest request)
笺注:可以看到每秒最大处理请求次数为65.08次,每个请求响应时间是15.366毫秒。
###
加大并发请求数、总请求数进行测试:(并发请求数为2001,总请求数为3000)
[root@centos8 ~]# ab -n 3000 -c 2001 http://127.0.0.1/p.php
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient)
Completed 300 requests
Completed 600 requests
Completed 900 requests
Completed 1200 requests
Completed 1500 requests
Completed 1800 requests
Completed 2100 requests
Completed 2400 requests
apr_pollset_poll: The timeout specified has expired (70007)
Total of 2416 requests completed
结论:
与Apache的MPM模式prefork相比,Apache的MPM模式event对高并发的支持更好。
相关文章:
Apache2.4的MPM模式(event)
Apache2.4的MPM模式(prefork) |