说明:用ab的好处,在处理多并发的情况下不用自己写线程模拟。
官网:
http://httpd.apache.org/(Apache服务器)
http://httpd.apache.org/docs/2.0/programs/(Apache工具文档大全)
http://httpd.apache.org/docs/2.0/programs/ab.html(文档教程)
http://httpd.apache.org/docs/current/programs/ab.html(文档教程)
教程:
要对Apache及其上的web程序进行压力测试其实非常简单,我们也不用再额外下载安装什么测试工具,因为Apache HTTP Server已经给我们准备了一个压力测试工具——ab。
ab,即Apache Benchmark,只要我们安装了Apache,就能够在Apache的安装目录中找到它。它的居住地址是Apache安装目录/bin/ab
。现在,我们就来看看如何使用ab.exe来进行压力测试。
在使用之前我们先来学习一下ab的用法。Apache安装目录/bin/
。然后键入帮助命令ab -help
(或者ab /?
、ab -h
),我们就可以看到如下的用法介绍界面。
ab的用法介绍
通过上面的用法介绍可以得知,ab的使用方法就是输入如下命令:
ab [可选的参数选项] 需要进行压力测试的url
此外,我们再根据上面的用法介绍界面来详细了解每个参数选项的作用。
-n:即requests,用于指定压力测试总共的执行次数。-c:即concurrency,用于指定压力测试的并发数。-t:即timelimit,等待响应的最大时间(单位:秒)。-b:即windowsize,TCP发送/接收的缓冲大小(单位:字节)。-p:即postfile,发送POST请求时需要上传的文件,此外还必须设置-T参数。-u:即putfile,发送PUT请求时需要上传的文件,此外还必须设置-T参数。-T:即content-type,用于设置Content-Type请求头信息,例如:application/x-www-form-urlencoded,默认值为text/plain。-v:即verbosity,指定打印帮助信息的冗余级别。-w:以HTML表格形式打印结果。-i:使用HEAD请求代替GET请求。-x:插入字符串作为table标签的属性。-y:插入字符串作为tr标签的属性。-z:插入字符串作为td标签的属性。-C:添加cookie信息,例如:"Apache=1234"(可以重复该参数选项以添加多个)。-H:添加任意的请求头,例如:"Accept-Encoding: gzip",请求头将会添加在现有的多个请求头之后(可以重复该参数选项以添加多个)。-A:添加一个基本的网络认证信息,用户名和密码之间用英文冒号隔开。-P:添加一个基本的代理认证信息,用户名和密码之间用英文冒号隔开。-X:指定使用的代理服务器和端口号,例如:"126.10.10.3:88"。-V:打印版本号并退出。-k:使用HTTP的KeepAlive特性。-k:使用HTTP的KeepAlive特性。-d:不显示百分比。-S:不显示预估和警告信息。-g:输出结果信息到gnuplot格式的文件中。-e:输出结果信息到CSV格式的文件中。-r:指定接收到错误信息时不退出程序。-h:显示用法信息,其实就是ab -help。
虽然ab可以配置的参数选项比较多,但是,一般情况下我们只需要使用形如ab -n 数字 -c 数字 url路径
的命令即可。譬如,我们对位于本地Apache服务器上、URL为localhost/index.php的页面进行压力测试。测试总次数为1000,并发数为100(相当于100个用户同时访问,他们总共访问1000次)。我们输入命令ab -n 1000 -c 100 localhost/index.php
,打印结果如下:
ab -n 1000 -c 100 localhost/index.phpThis is ApacheBench, Version 2.3 <$Revision: 655654 $>Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking localhost (be patient)Completed 100 requestsCompleted 200 requestsCompleted 300 requestsCompleted 400 requestsCompleted 500 requestsCompleted 600 requestsCompleted 700 requestsCompleted 800 requestsCompleted 900 requestsCompleted 1000 requestsFinished 1000 requestsServer Software: ???????Apache/2.2.25 (服务器软件名称及版本信息)Server Hostname: ???????localhost (服务器主机名)Server Port: ???????????80 (服务器端口)Document Path: ?????????/index.php (供测试的URL路径)Document Length: ???????10 bytes (供测试的URL返回的文档大小)Concurrency Level: ?????100 (并发数)Time taken for tests: ??0.247 seconds (压力测试消耗的总时间)Complete requests: ?????1000 (压力测试的总次数)Failed requests: ???????0 (失败的请求数)Write errors: ??????????0 (网络连接写入错误数)Total transferred: ?????198000 bytes (传输的总数据量)HTML transferred: ??????10000 bytes (HTML文档的总数据量)Requests per second: ???4048.34 [#/sec] (mean) (平均每秒的请求数)Time per request: ??????24.701 [ms] (mean) (所有并发用户(这里是100)都请求一次的平均时间)Time per request: ??????0.247 [ms] (mean, across all concurrent requests) (单个用户请求一次的平均时间)Transfer rate: ?????????782.78 [Kbytes/sec] received (传输速率,单位:KB/s)Connection Times (ms) ?????????????min ?mean[+/-sd] median ??maxConnect: ???????0 ???0 ??0.3 ?????0 ??????1Processing: ????6 ??23 ??4.2 ????24 ?????30Waiting: ???????5 ??20 ??5.3 ????21 ?????29Total: ?????????6 ??23 ??4.2 ????24 ?????30Percentage of the requests served within a certain time (ms) ?50% ????24 ?66% ????25 ?75% ????26 ?80% ????26 ?90% ????27 ?95% ????27 ?98% ????28 ?99% ????29 100% ????30 (longest request)
在上面的测试中,我们设置的压力测试总次数以及并发数并没有让服务器感觉到什么「压力」,现在我们再来看一个「压力山大」的执行命令:ab -n 100000 -c 1000 localhost/index.php
,这个时候apache就直接罢工——拒绝访问了:
ab -n 100000 -c 1000 localhost/index.phpThis is ApacheBench, Version 2.3 <$Revision: 655654 $>Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking localhost (be patient) ?Test aborted after 10 failuresapr_socket_connect(): 由于目标计算机积极拒绝,无法连接。 ??(730061)
注意:在上面的压力测试中,Apache使用的是默认配置,并没有经过任何优化措施处理。实际上,Apache在经过配置优化后,只要服务器硬件够用,Apache服务器是能够撑起1000的并发量的。
参考:
http://www.365mini.com/page/apache-benchmark.htm(以上内容转自此篇文章)
http://www.php.cn/php-weizijiaocheng-371644.html
http://www.jianshu.com/p/1f3acc45ca89
http://www.jb51.net/article/59469.htm
http://blog.51cto.com/510512/740219
http://blog.csdn.net/blueheart20/article/details/52170790
http://xuezaijiongtu.blog.163.com/blog/static/197576271201251101527325/
http://www.jianshu.com/p/43d04d8baaf7
http://www.jb51.net/article/56644.htm
Apache压力(并发)测试工具ab的使用教程收集
原文地址:http://www.cnblogs.com/EasonJim/p/8085363.html