1、Apache Bench简介html
ApacheBench 是 Apache 服务器自带的一个web压力测试工具,简称ab。ab又是一个命令行工具,对发起负载的本机要求很低,根据ab命令能够建立不少的并发访问线程,模拟多个访问者同时对某一URL地址进行访问,所以能够用来测试目标服务器的负载压力。总的来讲ab工具小巧简单,上手学习较快,能够提供须要的基本性能指标,可是没有图形化结果,不能监控。web
2、Apache Bench安装apache
首先须要安装Apache服务器,下载地址:https://www.apachelounge.com/download/,服务器
建议下载最新版本的,由于旧版本的ab不支持-r参数。cookie
3、Apache Bench使用session
了解参数并发
参数说明: 格式:ab [options] [http://]hostname[:port]/path -n requests Number of requests to perform //本次测试发起的总请求数 -c concurrency Number of multiple requests to make //一次产生的请求数(或并发数) -t timelimit Seconds to max. wait for responses //测试所进行的最大秒数,默认没有时间限制。 -r Don't exit on socket receive errors. // 抛出异常继续执行测试任务 -p postfile File containing data to POST //包含了须要POST的数据的文件,文件格式如“p1=1&p2=2”.使用方法是 -p 111.txt -T content-type Content-type header for POSTing //POST数据所使用的Content-type头信息,如 -T “application/x-www-form-urlencoded” 。 (配合-p) -v verbosity How much troubleshooting info to print //设置显示信息的详细程度 – 4或更大值会显示头信息, 3或更大值能够显示响应代码(404, 200等), 2或更大值能够显示警告和其余信息。 -V 显示版本号并退出。 -C attribute Add cookie, eg. -C “c1=1234,c2=2,c3=3” (repeatable) //-C cookie-name=value 对请求附加一个Cookie:行。 其典型形式是name=value的一个参数对。此参数能够重复,用逗号分割。 提示:能够借助session实现原理传递 JSESSIONID参数, 实现保持会话的功能,如-C ” c1=1234,c2=2,c3=3, JSESSIONID=FF056CD16DA9D71CB131C1D56F0319F8″ 。 -w Print out results in HTML tables //以HTML表的格式输出结果。默认时,它是白色背景的两列宽度的一张表。 -i Use HEAD instead of GET -x attributes String to insert as table attributes -y attributes String to insert as tr attributes -z attributes String to insert as td or th attributes -H attribute Add Arbitrary header line, eg. ‘Accept-Encoding: gzip’ Inserted after all normal header lines. (repeatable) -A attribute Add Basic WWW Authentication, the attributes are a colon separated username and password. -P attribute Add Basic Proxy Authentication, the attributes are a colon separated username and password. -X proxy:port Proxyserver and port number to use -V Print version number and exit -k Use HTTP KeepAlive feature -d Do not show percentiles served table. -S Do not show confidence estimators and warnings. -g filename Output collected data to gnuplot format file. -e filename Output CSV file with percentages served -h Display usage information (this message)
参数不少,通常咱们用 -c 和 -n 参数就能够了。例如:app
# ab -c 10 -n 10 http://www.xiami.com/socket
结果参数分析:
ide
经过上图,测试结果也一目了然,apache测试出的吞吐率为:Requests per second: 204.89[#/sec](mean)。
除此以外还有其余一些信息,须要说明下,以下:
Server Software表示被测试的Web服务器软件名称。
Server Hostname表示请求的URL主机名。
Server Port表示被测试的Web服务器软件的监听端口。
Document Path表示请求的URL中的根绝对路径,经过该文件的后缀名,咱们通常能够了解该请求的类型。
Document Length表示HTTP响应数据的正文长度。
Concurrency Level表示并发用户数,这是咱们设置的参数之一。
Time taken for tests表示全部这些请求被处理完成所花费的总时间。
Complete requests表示总请求数量,这是咱们设置的参数之一。
Failed requests表示失败的请求数量,这里的失败是指请求在链接服务器、发送数据等环节发生异常,以及无响应后超时的状况。若是接收到的HTTP响应数据的头信息中含有2XX之外的状态码,则会在测试结果中显示另外一个名为“Non-2xx responses”的统计项,用于统计这部分请求数,这些请求并不算在失败的请求中。
Total transferred表示全部请求的响应数据长度总和,包括每一个HTTP响应数据的头信息和正文数据的长度。注意这里不包括HTTP请求数据的长度,仅仅为web服务器流向用户PC的应用层数据总长度。
HTML transferred表示全部请求的响应数据中正文数据的总和,也就是减去了Total transferred中HTTP响应数据中的头信息的长度。
Requests per second 每秒请求数(平均),吞吐率,计算公式:Complete requests/Time taken for tests
Time per request 每次并发请求时间(全部并发),用户平均请求等待时间,计算公式:Time token for tests/(Complete requests/Concurrency Level)。
Time per requet(across all concurrent request) 每一请求时间(并发平均) 服务器平均请求等待时间,计算公式:Time taken for tests/Complete requests,正好是吞吐率的倒数。也能够这么统计:Time per request/Concurrency Level。
Transfer rate表示这些请求在单位时间内从服务器获取的数据长度,计算公式:Total trnasferred/ Time taken for tests,这个统计很好的说明服务器的处理能力达到极限时,其出口宽带的需求量。
Percentage of requests served within a certain time(ms)这部分数据用于描述每一个请求处理时间的分布状况,好比以上测试,80%的请求处理时间都不超过52ms,这个处理时间是指前面的Time per request,即对于单个用户而言,平均每一个请求的处理时间。
调用示例:
参考:
https://www.cnblogs.com/Ryana/p/6279232.html https://blog.csdn.net/zzycgfans/article/details/6100755 https://blog.csdn.net/u014756827/article/details/52160689 https://www.cnblogs.com/xiaoyaowuming/p/5622660.html