Windows下的ab.exe一般都在Apache安装目录的bin目录下。这里举几个我实际中用过的例子:
对http://localhost/dz7/index.php产生400次请求
ab -n 400 http://localhost/dz7/index.php
测试完成后,ab会输出测试的结果:
Benchmarking localhost (be patient) Completed 100 requests Completed 200 requests Completed 300 requests Completed 400 requests Finished 400 requests Server Software: Apache/2.2.11 Server Hostname: localhost Server Port: 80 Document Path: /dz7/index.php Document Length: 7015 bytes Concurrency Level: 1 Time taken for tests: 21.623 seconds Complete requests: 400 Failed requests: 399 (Connect: 0, Receive: 0, Length: 399, Exceptions: 0) Write errors: 0 Total transferred: 2968811 bytes HTML transferred: 2805205 bytes Requests per second: 18.50 [#/sec] (mean) Time per request: 54.057 [ms] (mean) Time per request: 54.057 [ms] (mean, across all concurrent requests) Transfer rate: 134.08 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 1.3 0 16 Processing: 31 54 64.5 47 1265 Waiting: 16 51 64.3 47 1250 Total: 31 54 64.5 47 1265 Percentage of the requests served within a certain time (ms) 50% 47 66% 47 75% 47 80% 47 90% 47 95% 62 98% 141 99% 187 100% 1265 (longest request) |
参数-n设定请求的次数;另外,还可以用-c来设定并发访问数,例如要模拟50个并发访问,可以用
ab -n 400 -c 50 http://localhost/dz7/index.php
此外,还有一个实际的问题,就是如何用ab来模拟已登录的用户的访问。绝大部分程序的登录机制都是用Cookie+Session来实现的,浏览器在每次请求时都发送一个Cookie,里面记录了Session的SID。你可以先在利用Firefox的Firebug插件来确定Cookie里的内容是什么。
firebug_cookie
从中可以看到表示登录用户的内容是“QfI_sid=55ySsk”。ab可以用-C(大写的C)来设定每次请求用发送的Cookie。所以
ab -n 400 -c 50 -C "QfI_sid=55ySsk" http://localhost/dz7/index.php
就可以模拟已登录用户的400次访问,并发数为50的情况。