小言_互联网的博客

压力测试工具Apache Bench:1:概要介绍与压测示例

248人阅读  评论(0)

Apache Bench是Apache服务应用服务器自带的性能测试软件。最初它被设计用来对Apache应用服务器的性能在每秒钟响应多少请求等方面的性能给出一个直观的评价。

概要信息

Apache JMeter概要信息如下表所示:

项目 说明
官网 https://httpd.apache.org/docs/2.4/programs/ab.html
开源/闭源 开源
源码管理地址 https://github.com/apache/jmeter
License类别 Apache License 2.0
开发语言 C
当前稳定版本 2.3
操作系统支持 跨平台,支持Linux/Windows/Mac等
下载地址 http://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.41.tar.gz

Apache Bench说明

以Apache httpd应用服务器2.4.41的源码为例,通过其Makefile的构成可以看出Apache Bench的体量与规模。ab.c即为应用程序ab的源码,可以看到,加上注释也只有2K多的代码。

liumiaocn:support liumiao$ pwd
/tmp/httpd-2.4.41/support
liumiaocn:support liumiao$ ls ab.c
ab.c
liumiaocn:support liumiao$ wc -l ab.c
    2696 ab.c
liumiaocn:support liumiao$

而从Makefile中也可以看到,生成ab的直接代码只有ab.c(链接库的使用情况可以通过对二进制分析可以看出实际的依赖状况)。

 62 ab_OBJECTS = ab.lo
 63 ab_LDADD = $(PROGRAM_LDADD) $(MATH_LIBS) $(ab_LIBS)
 64 ab.lo: ab.c
 65         $(LIBTOOL) --mode=compile $(CC) $(ab_CFLAGS) $(ALL_CFLAGS) $(ALL_CPPFLAGS) \
 66             $(ALL_INCLUDES) $(PICFLAGS) $(LTCFLAGS) -c $< && touch $@
 67 ab: $(ab_OBJECTS)
 68         $(LIBTOOL) --mode=link $(CC) $(ALL_CFLAGS) $(PILDFLAGS) \
 69             $(LT_LDFLAGS) $(ALL_LDFLAGS) -o $@ $(ab_LTFLAGS) $(ab_OBJECTS) $(ab_LDADD)

安装方法

macOS: 缺省一般会安装ab

Alpine: apk add apache2-utils

CentOS/RHEL: yum -y install httpd-tools

Ubuntu: apt-get install apache2-utils

使用示例

环境准备:测试应用

在本地机器的8088端口使用Docker启动一个Nginx应用(使用其他方式也可),示例如下所示:

liumiaocn:~ liumiao$ docker images |grep nginx |grep latest
nginx                                           latest                          e445ab08b2be        2 months ago        126MB
liumiaocn:~ liumiao$ docker run -p 8088:80 -d --name=nginx-test nginx:latest
a80fb1a4fc20627891a6bd7394fd79ae9aefb7dc8cf72c12967bc2673a815308
liumiaocn:~ liumiao$ 

使用curl命令或者直接使用浏览器确认nginx已正常运行

liumiaocn:~ liumiao$ curl http://localhost:8088/
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
liumiaocn:~ liumiao$

ab版本确认

liumiaocn:~ liumiao$ uname -a
Darwin liumiaocn 18.0.0 Darwin Kernel Version 18.0.0: Wed Aug 22 20:13:40 PDT 2018; root:xnu-4903.201.2~1/RELEASE_X86_64 x86_64
liumiaocn:~ liumiao$ ab -V
This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

liumiaocn:~ liumiao$ 

执行压测

压测内容:对http://localhost:8088/并发启动100个进程,平均10次,共计执行1000次

执行命令如下所示:

执行命令:ab -n 1000 -c 100 http://localhost:8088/

执行参数说明:

  • -n: 测试执行总次数
  • -c: 并行度,类似JMeter中的线程组和LR中的Virtual User

执行日志如下所示:

liumiaocn:~ liumiao$ ab -n 1000 -c 100 http://localhost:8088/
This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
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 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        nginx/1.17.2
Server Hostname:        localhost
Server Port:            8088

Document Path:          /
Document Length:        612 bytes

Concurrency Level:      100
Time taken for tests:   0.591 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      845000 bytes
HTML transferred:       612000 bytes
Requests per second:    1693.02 [#/sec] (mean)
Time per request:       59.066 [ms] (mean)
Time per request:       0.591 [ms] (mean, across all concurrent requests)
Transfer rate:          1397.07 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   1.4      0       7
Processing:     8   55  21.1     54     130
Waiting:        1   54  20.9     52     129
Total:          8   56  20.6     54     134

Percentage of the requests served within a certain time (ms)
  50%     54
  66%     63
  75%     69
  80%     72
  90%     82
  95%     95
  98%    107
  99%    116
 100%    134 (longest request)
liumiaocn:~ liumiao$

返回结果说明如下所示:

项目 说明
Document Path 请求路径
Document Length 响应数据的正文长度
Concurrency Level 并行度,类似JMeter中的线程组和LR中的Virtual User
Time taken for tests 测试所花时间
Complete requests 成功(返回码为200)执行的请求数量
Failed requests 成功之外(返回码不是200)的请求数量
Total transferred 所有请求的响应数据总和(数据正文+Header信息)
HTML transferred 所有请求的数据正文数据总和
Requests per second 吞吐量,平均每秒完成的请求数
Time per request 平均每个请求所花时间 (用户平均等待时间)
Time per request(mean, across all concurrent requests) 服务器平均等待时间(吞吐量的倒数)
Transfer rate 传输率(单位时间从服务器获取的数据长度)

可以大体看到吞吐量和90%的用户请求在82ms内完成。

结果关联关系

RPS(吞吐量)公式:
Requests per second = Complete requests / Time taken for tests = 1000 / 0.591 = 1692 (近似等于1693)

Time per request公式:
Time per request = Time taken for tests / (Complete requests / Concurrency Level)= 0.591 / 10 = 0.0591s = 59.1 ms

Time per request(mean, across all concurrent requests)公式:
Time per request(mean, across all concurrent requests) = Time taken for tests / Complete requests = 0.591 / 1000 = 0.000591s = 0.591ms

Transfer rate公式:
Transfer rate = Total transferred / Time taken for tests = 845000 / 0.591= 1429780.034 = 1429.78 kb/s(近似等于1397.07)


转载:https://blog.csdn.net/liumiaocn/article/details/101924143
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场