ApacheBenchmark和gnuplot

栏目: 服务器 · Apache · 发布时间: 5年前

内容简介:又参考了一些论文,发现他们在考虑Web应用程序性能时,主要考虑吞吐量。本文,就来研究一下ApacheBench测量吞吐量的方法,并且使用gnuplot绘制出吞吐量与时间的关系。ApacheBench(ab)是Apache自带的一个Web压力测试工具,也可以单独下载使用。ab非常实用,它不仅可以对apache服务器进行网站访问压力测试,也可以对或其它类型的服务器进行压力测试。比如nginx、tomcat、IIS等。

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

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

ApacheBench

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

安装

1、安装apache

apt install apache2

2、测试

curl localhost
ab -V

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

使用

ab命令使用说明:

ab [options] [http[s]://]hostname[:port]/path

可选参数如下:

-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。

运行结果如下:

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/

gnuplot

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

安装

1、终端输入 gnuplot ,提示:

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(一):安装

使用

1、启动

输入 gnuplot ,进入gnuplotshell。

ApacheBenchmark和gnuplot

这里包含gnuplot的版本、系统、版权等信息。最关键的是最后一条:

Terminal type set to ‘unknown’

什么是terminal?在gnuplot中,terminal就是说你打算用什么方式输出图片。这里默认的terminal是unknown,因为我们安装了gnuplot-nox。一般需要修改为wxt,也就是直接输出到电脑屏幕上。

2、设置terminal

set terminal dumb
plot sin(x)

ApacheBenchmark和gnuplot 以上,设置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)尝试绘图

ApacheBenchmark和gnuplot

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

4、gnuplot脚本

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

# 绘制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' 命令保存脚本。

绘制ab数据

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

响应时间

1、查看t10.dat文件

tail t10.dat
ApacheBenchmark和gnuplot
  • 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的关系图

# 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图像如下:

ApacheBenchmark和gnuplot

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

4、测试个人博客

ab -g t10.dat -t 10 -n 1000000 http://www.voidking.com/

5、再次执行t10.plt脚本

生成的图像如下:

ApacheBenchmark和gnuplot

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

吞吐量

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

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

#!/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

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

3、新建throughput.plt脚本

# 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"

生成的图像如下:

ApacheBenchmark和gnuplot

后记

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


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

嵌入式系统软件设计中的常用算法

嵌入式系统软件设计中的常用算法

周航慈 / 2010-1 / 24.00元

《嵌入式系统软件设计中的常用算法》根据嵌入式系统软件设计需要的常用算法知识编写而成。基本内容有:线性方程组求解、代数插值和曲线拟合、数值积分、能谱处理、数字滤波、数理统计、自动控制、数据排序、数据压缩和检错纠错等常用算法。从嵌入式系统的实际应用出发,用通俗易懂的语言代替枯燥难懂的数学推导,使读者能在比较轻松的条件下学到最基本的常用算法,并为继续学习其他算法打下基础。 《嵌入式系统软件设计中的......一起来看看 《嵌入式系统软件设计中的常用算法》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

URL 编码/解码
URL 编码/解码

URL 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试