Apache Beach (ab)是Apache自带的一个性能测试工具,专门用来测试网站的性能, 不仅限于Apache web服务器。 它可以同时模拟多个并发请求,测试Web服务器的最大承载压力,同时也可以根据Apache Bench提供的测试结果对服务器性能参数进行调整。它可以记录测试数据,其它工具比如Gnuplot可以利用测试数据进行分析。它也可以提供一个summary,可以直观显示当前测试的web服务器的性能。
1.1、初始运行及简单参数
启动IDEA中的springboot工程,listCategory功能为读取数据库表的信息列表显示在页面上,在ab下运行命令:
ab -n 100000 -c 10 http://localhost:8080/listCategory
注意网址后面要加”/“或者明确的path如”https://www.google.com/?gfe_rd=cr&ei=_BvfU77ZGMeL8QfugIHAAw“.
“-c”是并发数,可以模拟同时有多少个clients并发访问。 “-n”表示总的请求数。每个client发送的请求数为此数字除以client数(上面的数字)。 “-t”可以指定测试的最大时间,如果还不到此数请求已经发完,那么测试也会结束。当使用-t参数时,ab内部默认最大的请求数为50000,为了同时使用”-n”指定的参数,可以将”-t”参数放在”-n”参数之前
1.2、测试结果分析
Server Software: Server Hostname: localhost //服务器主机名 Server Port: 8080 //服务器端口 Document Path: /listCategory //测试的页面文档 Document Length: 2501 bytes //文档大小 Concurrency Level: 500 //并发量 Time taken for tests: 34.608 seconds //整个测试持续的时间 Complete requests: 100000 //完成的请求数量 Failed requests: 0 //失败的请求数量 Total transferred: 273500000 bytes //整个场景中的网络传输量 HTML transferred: 250100000 bytes //整个场景中的HTML内容传输量 Requests per second: 2889.48 [#/sec](mean) //每秒事务数 后面括号中的mean表示这是一个平均值 Time per request: 173.041 [ms] (mean) //平均事务响应时间 后面括号中的mean表示这是一个平均值 Time per request: 0.346 [ms] (mean, across all concurrent requests) //每个请求实际运行时间的平均值 Transfer rate: 7717.51 [Kbytes/sec] received //平均每秒网络上的流量,可以帮助排除是否存在网络流量过大导致响应时间延长的问题 Connection Times (ms) //网络上消耗的时间的分解 min mean[+/-sd] median max Connect: 0 0 5.9 0 596 Processing: 24 172 337.5 112 9579 Waiting: 8 127 333.2 84 9531 Total: 24 172 337.6 113 9579 //以下是整个场景中所有请求的响应情况。在场景中每个请求都有一个响应时间,其中50%的用户响应时间小于113毫秒,66% 的用户响应时间小于119毫秒,最大的响应时间小于9579 毫秒。由于对于并发请求,cpu实际上并不是同时处理的,而是按照每个请求获得的时间片逐个轮转处理的,所以基本上第一个Time per request时间约等于第二个Time per request时间乘以并发请求数。 Percentage of the requests served within a certain time (ms) 50% 113 66% 119 75% 121 80% 124 90% 140 95% 639 98% 656 99% 700 100% 9579 (longest request)2.1、在ab下运行命令:
ab -n 100000 -c 500 -g F:/ab-gnuplot/50/n100000c500.dat http://localhost:8080/listCategory
-g:把所有测试结果写入一个'gnuplot'或者TSV(以Tab分隔的)文件。此文件可以方便地导入到Gnuplot,IDL,Mathematica,Igor甚至Excel中。其中的第一行为标题。
释义:并发量为500,请求数10w,把测试结果写入“F:/ab-gnuplot/50”目录下,命名为“n100000c500.dat”。
2.2、使用Gnuplot将测试结果生成图形,便于比较
在“ab-gnuplot”文件夹下新建一个“ab10vs20.plt”的文件,比较请求数10w,“最大线程数”不同的情况下的性能(并发量相同)
比较请求数10w并发量3,最大线程数(server.tomcat.max-threads=10/20)为10/20的性能:
#output as png image set terminal png size 1920,1280 #save file to "ab20vs32.png" set output "ab20vs32.png" #graph title set title "Biz Performance" set key invert reverse Left outside #nicer aspect ratio for image size #set size 1,0.7 # y-axis grid set grid y #x-axis label set xlabel "requests" #y-axis label set ylabel "response time (ms)" #plot data from "biz.dat" using column 9 with smooth sbezier lines #and title of "Biz Performance" for the given data plot "./20/n100000c3.dat" using 9 smooth sbezier with lines title "t20n100000c3", \ "./10/n100000c3.dat" using 9 smooth sbezier with lines title "t10n100000c3"然后使用gnuplot左上角的“file-open”打开这个文件,即可生成测试图如下(建议新建标签页打开图片):
发现性能波动在95000后,可通过设置gnuplot的xrange值,之查看95000后面的图像,命令如下:
set xrange[95000:100000]
replot
图表如下图所示:
2.3、吞吐量
为了得到按时间序列显示的吞吐率图表,我们可以处理一下得到的测试数据,写个脚本:
首先,转变测试数据,在Git Bash中跑一遍:
for var in {1,2,3,4,5,6,7,8,9,10}; do start_time=`awk '{print $6}' 10/n100000c$var.dat | grep -v 'wait' | sort | uniq -c|head -1|awk '{print $2}'` awk '{print $6}' 10/n100000c$var.dat | grep -v 'wait' | sort | uniq -c|awk -v t=$start_time '{print $2-t,$1}' > 10/epochtime$var.dat done然后,将转换后的数据进行画图,新建“throughput.plt”文件,在gnuplot中执行
#output as png image set terminal png size 1920,1080 set output "throughput.png" #graph title set title "Throughput" set key invert reverse Left outside #nicer aspect ratio for image size #set size 1,0.6 # y-axis grid set grid y #x-axis label set xlabel "time" #y-axis label set ylabel "responses per second" plot "epochtime1.dat" using 1:2 with lines title "concurrency 1", \ "epochtime2.dat" using 1:2 with lines title "concurrency 2", \ "epochtime3.dat" using 1:2 with lines title "concurrency 3", \ "epochtime4.dat" using 1:2 with lines title "concurrency 4", \ "epochtime5.dat" using 1:2 with lines title "concurrency 5", \ "epochtime6.dat" using 1:2 with lines title "concurrency 6", \ "epochtime7.dat" using 1:2 with lines title "concurrency 7", \ "epochtime8.dat" using 1:2 with lines title "concurrency 8", \ "epochtime9.dat" using 1:2 with lines title "concurrency 9", \ "epochtime10.dat" using 1:2 with lines title "concurrency 10"分析:
我的电脑配置为四核,内存12G;
当并发量为1时,吞吐量最少,耗时70ms;
当并发量为2时,耗时约34ms,耗时大概为并发量为1的情况下的一半;
当并发量为3时,耗时约23ms,耗时大概为并发量为1的情况下的三分之一;
当并发量为4时,耗时约22ms,耗时大概为并发量为1的情况下的三分之一;
之后的吞吐量大抵相差不多,一直波动着,是因为我的电脑是四核的缘故吗?