PHP apache测试工具ab.exe 和mpm介绍提高并发数

该工具是apache自带的,可以用它来测试网站的并发量有多大和某个页面的访问时间。

基本用法:

1、 进入CMD,转到apache的bin目录下。

2、 执行命令ab.exe -n 访问的问次数–c 多少人访问(并发量) 访问的地址如:ab.exe –n 1000 –c 100 http://localhost/index.PHP;

如输入以下命令ab.exe-n 10000 -c 100 http://localhost/test/index.php,

Index.php的内容为

<?php

for($i=0;$i<100;$i++){

echo$i.'|';

}

该命令的意思为100个人访问该地址1W次。会出现以下结果。

 1     Server Software:        Apache/2.4.4                                   #apache版本号  
 2     Server Hostname:        localhost  
 3     Server Port:            80  
 4        
 5     Document Path:          /test/index.php                       
 6     Document Length:        5 bytes  
 7        
 8     ConcurrencyLevel:      100  
 9     Time taken fortests:   54.111 seconds                                 #访问的总时间(秒)  
10     Completerequests:      10000                                          #访问的总次数  
11     Failed requests:        0  
12     Write errors:           0  
13     Totaltransferred:      2060000 bytes  
14     HTMLtransferred:       50000 bytes  
15     Requests persecond:    184.80 [#/sec] (mean)                          #每秒访问多少次  
16     Time perrequest:       541.111 [ms] (mean)                            #这么多人(100)访问一次的时间  
17     Time perrequest:       5.411 [ms] (mean, acrossall concurrent requests)         #一个人访问一次花费的时间  
18     Transfer rate:          37.18 [Kbytes/sec] received  

另外,如果我们把并发数增加到500,即把命令调整成ab.exe -n 10000 -c 500 http://localhost/test/index.php它就会出现以下结果。

    apr_socket_connect():由于目标计算机积极拒绝,无法连接。   (730061)  
    Total of 902 requestscompleted  

原因是因为apache在windows下默认的最大并发访问量为150。我们可以设置conf\extra下的httpd-mpm.conf文件来修改它的最大并发数。在修改之前我们要说明一下,mpm是个什么东西

Mpm为多路处理模块,即apache采用怎么样的方式来处理并发,主要有三种方式

1、 perfork 预处理进程方式(用进程服务)

2、 worker 工作模式(用进程下的线程服务)

3、 winnt这个一般是windos 下采用的。(针对windows)

说完这个我们就可以动手修改配置文件了。步骤如下:

1、 打开httpd.conf配置文件,打开下面的配置

# Server-poolmanagement (MPM specific)

Include conf/extra/httpd-mpm.conf

2、 确定当前 apache是mpm模式,CMD下进放到apache的bin目录输入指令httpd.exe –l

会出现以下结果,就可知道它用的是什么模式

    Compiledin modules:  
      core.c  
      mod_win32.c  
      mpm_winnt.c 这是为winnt模式  
      http_core.c  
      mod_so.c  

修改httpd-mpm.conf文件,因为从上面可以看到,我的apache用的是winnt模式,所以在该文件下找到对应的winnt_module模块,修改参数,原先为150,我们把它修改成1000

1     <IfModulempm_winnt_module>  
2         ThreadsPerChild        1000  
3         MaxConnectionsPerChild   0  
4     </IfModule>  

4、 重启服务器

修改完之后我们重新运行上面的命令ab.exe-n 10000 -c 500 http://localhost/test/index.php

它就会运行成功了,出现与ab.exe -n10000 -c 100 http://localhost/test/index.php时类似的效果。

同理,如果是其它模式,则在httpd-mpm.conf中修改对应的地方即可。如下

<IfModule mpm_prefork_module>  
   StartServers             5                  #开始启动的进程  
   MinSpareServers          5                 #最小准备进程  
   MaxSpareServers         10                #最大空闲进程  
   MaxRequestWorkers      1000            #最大并发数  
   MaxConnectionsPerChild   0  
</IfModule>