該工具是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次。會出現以下結果。
- Server Software: Apache/2.4.4 #apache版本號
- Server Hostname: localhost
- Server Port: 80
- Document Path: /test/index.php
- Document Length: 5 bytes
- ConcurrencyLevel: 100
- Time taken fortests: 54.111 seconds #訪問的總時間(秒)
- Completerequests: 10000 #訪問的總次數
- Failed requests: 0
- Write errors: 0
- Totaltransferred: 2060000 bytes
- HTMLtransferred: 50000 bytes
- Requests persecond: 184.80 [#/sec] (mean) #每秒訪問多少次
- Time perrequest: 541.111 [ms] (mean) #這么多人(100)訪問一次的時間
- Time perrequest: 5.411 [ms] (mean, acrossall concur
另外,如果我們把並發數增加到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
3、 修改httpd-mpm.conf文件,因為從上面可以看到,我的apache用的是winnt模式,所以在該文件下找到對應的winnt_module模塊,修改參數,原先為150,我們把它修改成1000
- <IfModulempm_winnt_module>
- ThreadsPerChild 1000
- MaxConnectionsPerChild 0
- </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>