worker的工作原理及配置
worker是2.0 版中全新的支持多线程和多进程混合模型的MPM。由于使用线程来处理,所以可以处理相对海量的请求,而系统资源的开销要小于基于进程的服务器,worker也使用了多进程,每个进程又生成多个线程,以获得基于进程服务器的稳定性
<IfModule mpm_worker_module>
# StartServers 2 //初始化建立的进程数
# MaxClients 150 //所有子进程中的线程总数
# MinSpareThreads 25 //最少空闲线程数(为了不在请求到来时再生成线程)
# MaxSpareThreads 75 //最大空闲线程数
# ThreadsPerChild 25 //每个子进程数包含的线程数
# MaxRequestsPerChild 0 //控制服务器建立新进程和结束旧进程的频率
StartServers 60
MaxClients 3200
ServerLimit 100
MinSpareThreads 1920
MaxSpareThreads 3200
#ThreadLimit 512
ThreadsPerChild 32
MaxRequestsPerChild 300
</IfModule>
公式:活动子进程的最大数量=MaxClients/ThreadsPerChild
MaxChients=ThreadsPerChild的整数倍
ServerLimit(活动子进程数量的硬限制)>= 活动子进程的最大数量
ThreadLimit(所有服务线程总数的硬限制)>=ThreadsPerChild
ServerLimit*ThreadsPerChild >= MaxClient
ServerLimit没写就是16,它决定系统最多启动几个httpd进程。
ThreadLimit 没写默认是64,
ThreadsPerChild* ServerLimit=25*16=400,
400就是系统理论支持的最大并发。
MaxClients<ThreadsPerChild* ServerLimit,
MaxClients如果大于400将被限制在400.
400只是理论最大并发,实际并发就是MaxClients的值。
我做了如下试验:
Time taken for tests: 127.952487 seconds
Requests per second: 78.15 [#/sec] (mean)
Time per request: 63976.242 [ms] (mean)
Time per request: 12.795 [ms] (mean, across all concurrent requests)
Transfer rate: 22.89 [Kbytes/sec] received
第二次,ab -n 10000 -c 400 http://192.168.1.101/
Time taken for tests: 138.340074 seconds
Requests per second: 72.29 [#/sec] (mean)
Time per request: 5533.603 [ms] (mean)
Time per request: 13.834 [ms] (mean, across all concurrent requests)
Transfer rate: 21.17 [Kbytes/sec] received
第三次,ab -n 10000 -c 150 http://192.168.1.101/
Time taken for tests: 6.727553 seconds
Requests per second: 1486.42 [#/sec] (mean)
Time per request: 100.913 [ms] (mean)
Time per request: 0.673 [ms] (mean, across all concurrent requests)
Transfer rate: 435.37 [Kbytes/sec] received
结论是:并发如果大于MaxClients,服务器的效率会很低,如果并发等于小于MaxClients,10000个请求将迅速被处理完毕,效率很高