一个计算机技术爱好者与学习者

0%

ApacheBenchmark和gnuplot

1. 前言

《虚拟机在线迁移的性能统计》一文中,提到了虚拟机迁移的四个性能指标。但是文中只记录了迁移时间、停机时间、迁移数据量的测量方法,对于应用程序的性能,由于没有想到好的测量方法,最终搁置。

又参考了一些论文,发现他们在考虑Web应用程序性能时,主要考虑吞吐量。本文,就来研究一下ApacheBench测量吞吐量的方法,并且使用gnuplot绘制出吞吐量与时间的关系。

2. ApacheBench

ApacheBench(ab)是Apache自带的一个Web压力测试工具,也可以单独下载使用。ab非常实用,它不仅可以对apache服务器进行网站访问压力测试,也可以对或其它类型的服务器进行压力测试。比如nginx、tomcat、IIS等。

2.1. 安装

1、安装apache
apt install apache2

2、测试

1
2
curl localhost
ab -V

单独下载ab的话,可以使用apt install apache2-utils

2.2. 使用

ab命令使用说明:
ab [options] [http[s]://]hostname[:port]/path

可选参数如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
-n requests     Number of requests to perform
-c concurrency Number of multiple requests to make at a time
-t timelimit Seconds to max. to spend on benchmarking
This implies -n 50000
-s timeout Seconds to max. wait for each response
Default is 30 seconds
-b windowsize Size of TCP send/receive buffer, in bytes
-B address Address to bind to when making outgoing connections
-p postfile File containing data to POST. Remember also to set -T
-u putfile File containing data to PUT. Remember also to set -T
-T content-type Content-type header to use for POST/PUT data, eg.
'application/x-www-form-urlencoded'
Default is 'text/plain'
-v verbosity How much troubleshooting info to print
-w Print out results in HTML tables
-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
-C attribute Add cookie, eg. 'Apache=1234'. (repeatable)
-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.
-q Do not show progress when doing more than 150 requests
-l Accept variable document length (use this for dynamic pages)
-g filename Output collected data to gnuplot format file.
-e filename Output CSV file with percentages served
-r Don't exit on socket receive errors.
-m method Method name
-h Display usage information (this message)
-Z ciphersuite Specify SSL/TLS cipher suite (See openssl ciphers)
-f protocol Specify SSL/TLS protocol
(TLS1, TLS1.1, TLS1.2 or ALL)

1、模拟10个client,总共发送100个请求(每个client发送10个请求)
ab -n 100 -c 10 http://localhost/
注意网址后面要加”/“或者明确的path。
运行结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
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).....done

Server Software: Apache/2.4.18
Server Hostname: localhost
Server Port: 80

Document Path: /
Document Length: 11321 bytes

Concurrency Level: 10
Time taken for tests: 0.013 seconds
Complete requests: 100
Failed requests: 0
Total transferred: 1159500 bytes
HTML transferred: 1132100 bytes
# 相当于LoadRunner中的每秒事务数
Requests per second: 7841.29 [#/sec] (mean)
# 相当于LoadRunner中的平均事务响应时间
Time per request: 1.275 [ms] (mean)
# 每个连接请求实际运行时间的平均值
Time per request: 0.128 [ms] (mean, across all concurrent requests)
Transfer rate: 88788.85 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.1 0 1
Processing: 0 1 0.3 1 1
Waiting: 0 0 0.2 0 1
Total: 1 1 0.3 1 2

Percentage of the requests served within a certain time (ms)
50% 1
66% 1
75% 1
80% 1
90% 2
95% 2
98% 2
99% 2
100% 2 (longest request)

性能测试得到的最重要的指标是QPS(Requests per second),反映了接口的并发承受能力,也就是系统的峰值性能。如果对接口的调用超过了这一限制,就要考虑提升硬件或者做一些优化了。更多结果说明参考Web性能压力测试工具之ApacheBench详解

2、指定测试的最大时间为10秒
ab -t 10 -n 1000000 http://localhost/
“-t”可以指定测试的最大时间,如果还不到此数请求已经发完,那么测试也会结束。当使用”-t”参数时,ab内部默认最大的请求数为50000,为了同时使用”-n”指定的参数,可以将”-t”参数放在”-n”参数之前。更多内容参考按时间进行压力测试

3、测试结果存入gnuplot格式的文件
ab -g t10.dat -t 10 -n 1000000 http://localhost/

3. gnuplot

gnuplot是一个小巧实用的数据处理工具,主要用来绘制2D/3D的数据或者函数图像,但是也包含数学计算、拟合等功能。虽然它的名字里有个“gnu”,但是它和大名鼎鼎的GNU没什么关系,使用的授权协议也不是GNU GPL,所以这里的“gnu”是小写,全名应该读作“new plot”。

3.1. 安装

1、终端输入gnuplot,提示:

1
2
3
4
5
6
7
8
The program 'gnuplot' can be found in the following packages:
* gnuplot-nox
* gnuplot-qt
* gnuplot-x11
* gnuplot5-nox
* gnuplot5-qt
* gnuplot5-x11
Try: sudo apt install <selected package>

可见gnuplot在多个软件包中都包含,这里我们选择安装gnuplot-nox或者gnuplot-qt。

2、安装gnuplot-nox
apt install gnuplot-nox

3、测试
gnuplot -V

其他平台的安装,参考谈谈gnuplot(一):安装

3.2. 使用

1、启动
输入gnuplot,进入gnuplotshell。

这里包含gnuplot的版本、系统、版权等信息。最关键的是最后一条:
Terminal type set to ‘unknown’
什么是terminal?在gnuplot中,terminal就是说你打算用什么方式输出图片。这里默认的terminal是unknown,因为我们安装了gnuplot-nox。一般需要修改为wxt,也就是直接输出到电脑屏幕上。

2、设置terminal

1
2
set terminal dumb
plot sin(x)


以上,设置terminal为dumb,也就是直接在shell中显示绘图结果。

3、图形界面显示
在shell中绘图,太粗糙,我们想要在图形界面中显示。
(1)使用vncserver连接服务器,参考Linux下使用VirtualBox
(2)启动gnuplot,设置terminal
set terminal wxt或者set terminal x11无法成功,因为没有安装图形界面支持。
(3)安装图形界面支持
apt install gnuplot-x11
之后再启动gnuplot,默认terminal为x11。
(4)尝试绘图

更多绘制方法,参考程序员的绘图利器 — Gnuplot

4、gnuplot脚本
(1)新建sin.plt脚本,内容为:

1
2
3
4
# 绘制sin
set xlabel 'x'
set ylabel 'y'
plot sin(x)

(2)运行脚本
gnuplot sin.plt或者在gnushell中执行load 'sin.plt',值得一提的是,前一种方法绘制的图像会一闪而过。
(3)保存脚本
如果在gnushell执行了一些set命令,然后plot进行绘图,想要保存这些操作,方便下次使用,那么可以使用save 'filename.plt'命令保存脚本。

4. 绘制ab数据

在ab使用一节,已经保存两个文件,n10000.dat和t10.dat,接下来我们把该文件中的数据绘制一下。主要参考使用Apache Bench 和 Gnuplot产生性能测试图

4.1. 响应时间

1、查看t10.dat文件
tail t10.dat

  • starttime:人类可读的开始时间
  • seconds:开始时间的unix时间戳值
  • ctime:对应的ab输出中的Connection Times(ms)中的Connect
  • dtime:对应的ab输出中的Connection Times(ms)中的Processing
  • ttime:对应的ab输出中的Connection Times(ms)中的Total
  • wait:对应的ab输出中的Connection Times(ms)中的Waiting

2、新建t10.plt脚本,绘制request与response time的关系图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# output as png image
set terminal png

# save file to "t10.png"
set output "t10.png"

# graph title
set title "ab -g t10.dat -t 10 -n 1000000 http://localhost/"

# nicer aspect ratio for image size
set size 1,0.7

# y-axis grid
set grid y

#x-axis label
set xlabel "request"

#y-axis label
set ylabel "response time (ms)"

#plot data from "t10.dat" using column 9 with smooth sbezier lines
plot "t10.dat" using 9 smooth sbezier with lines title "t10"

3、执行t10.plt脚本
gnuplot t10.plt
生成的t10.png图像如下:

这张图,和教程中给的结果差别很大,看不出逐渐增长的趋势。从上图可以看出,10s内发送了接近6万个请求,response time最大才12ms,应该是因为本机性能太好导致的。所以,郝同学决定换一个主机测试。

4、测试个人博客
ab -g t10.dat -t 10 -n 1000000 http://www.voidking.com/

5、再次执行t10.plt脚本
生成的图像如下:

由上图可以看出,10s内发送了接近120个请求,response time逐渐增长,最大为99ms。

4.2. 吞吐量

上面的效果并不是我们最终想要的,我们需要的,是response per second与time的关系,也就是吞吐量。

1、新建pt10.sh脚本,处理t10.dat

1
2
3
#!/bin/bash
start_time=`awk '{print $6}' t10.dat | grep -v 'wait' | sort | uniq -c|head -1|awk '{print $2}'`
awk '{print $6}' t10.dat | grep -v 'wait' | sort | uniq -c|awk -v t=$start_time '{print $2-t,$1}' > epochtime.dat

2、执行pt10.sh

1
2
chmod a+x pt10.sh
./pt10.sh

3、新建throughput.plt脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# output as png image
set terminal png size 1000,560
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 "epochtime.dat" using 1:2 with lines title "t10"

生成的图像如下:

5. 后记

以上,完成了ApacheBenchmark和gnuplot的基础学习。gnuplot确实是一个很棒的绘图工具,绘出的图看起来就专业。以后就不用excel绘图了,安装一个windows版gnuplot,完美。

  • 本文作者: 好好学习的郝
  • 原文链接: https://www.voidking.com/dev-ab-and-gnuplot/
  • 版权声明: 本文采用 BY-NC-SA 许可协议,转载请注明出处!源站会即时更新知识点并修正错误,欢迎访问~
  • 微信公众号同步更新,欢迎关注~