Mac 使用ab命令压测配置及使用总结

ab是apacheBench的缩写,是一个单线程命令,是Apache服务器自带的一个web压力测试工具,最初是用于测试Apache HTTP Server。使用时,ab命令会创建并发访问线程,模拟多个访问者同时对某一个URL地址进行访问。

使用方法:

ab [option] [http://]host[:port]/path

例如:ab -n 100 -c 10 “http://en.wikipedia.org/wiki/Main_Page”

其中[option]是ab命令提供的可选参数,列举一些常用的参数:

-n //requests Number of requests to perform 指定压力测试总共访问页面的次数,默认是1

-c //concurrency Number of multiple requests to make at a time 用于一次产生请求的并发数,默认是1

-r //Don’t exit on socket receive errors.指定接收到错误信息时不退出程序。

-B //address Address to bind to when making outgoing connections 绑定出口IP地址

-t //timelimit Seconds to max. to spend on benchmarking This implies -n 50000 测试所进行的最大秒数。其内部隐含值是-n 50000。它可以使对服务器的测试限制在一个固定的总时间以内。默认时,没有时间限制。

-s //timeout Seconds to max. wait for each response Default is 30 seconds 每个相应的最长的时间,默认是30秒

-g //filename Output collected data to gnuplot format file 输出结果信息到gnuplot格式的文件中。

-e //filename Output CSV file with percentages served 输出结果信息到CSV格式的文件中。

-S // Do not show confidence estimators and warnings. 不显示预估和警告信息。

-d //Do not show percentiles served table. 不显示百分比。

-k //Use HTTP KeepAlive feature 使用HTTP的KeepAlive特性。

-b //windowsize Size of TCP send/receive buffer, in bytes TCP的发送/接受窗口,以字节为单位

-p //postfile File containing data to POST 包含需要post的数据文件,例如:-p a.txt,此外还需要结合-T,并且文件的内容需类似像"key1=value1&key2=vaule2"形式

-T //content-type Content-type header for POSTing 设置Context-Type请求头信息,如-T “application/x-www-form-urlencoded”默认值为text/plain

-X //proxy:port Proxyserver and port number to use 指定使用的代理服务器和端口号,例如:“126.10.10.3:88”

-v //verbosity How much troubleshooting info to print 设置打印帮助信息的详细程度,4或更大值会显示头信息, 3或更大值可以显示响应代码(404, 200等), 2或更大值可以显示警告和其他信息。

-w //Print out results in HTML tables 以HTML表的格式输出结果。默认时,它是白色背景的两列宽度的一张表。

-i //Use HEAD instead of GET 使用HEAD请求代替GET请求

-C //cookie-name=value 添加cookie信息,其典型形式是name=value的一个参数对。此参数可以重复,用逗号分割。

-H //attribute Add Arbitrary header line, eg. ‘Accept-Encoding: gzip’ Inserted after all normal header lines. (repeatable) 添加任意的请求头,例如:“Accept-Encoding: gzip”,此参数可重复

-P // proxy-auth-username:password 对一个中转代理提供BASIC认证信任。用户名和密码用英文冒号隔开,并以base64编码形式发送。无论服务器是否需要(即, 是否发送了401认证需求代码),此字符串都会被发送。

-A //Add Basic WWW Authentication, the attributes are a colon separated username and password.基本网络认证,用户名和密码之间用英文冒号隔开。

-x //attributes String to insert as table attributes 插入字符串作为table标签的属性。

-y //attributes String to insert as tr attributes 插入字符串作为tr标签的属性。

-z //attributes String to insert as td or th attributes 插入字符串作为td标签的属性。

基础用法:

ab -n 请求次数 -c 并发数 地址

其中-c选项为一次发送的请求数量,及并发量。
-n选项为请求次数。
前期配置:
1.在Mac中配置Apache
①启动Apache,打开终端

sudo apachectl -v
sudo apachectl start

②设置虚拟端终机
打开Apache的配置文件

sudo vi /etc/apache2/httpd.conf

在httpd.conf中找到“#Include /private/etc/apache2/extra/httpd-vhosts.conf”,去掉前面的“#”,保存并退出,去掉这一行的#意思是从/extra/httpd-vhosts.conf这个文件导入虚拟主机配置。
然后重启Apache

sudo apachectl restart

运行如下命令,打开配置虚拟主机文件httpd-vhost.conf,配置虚拟主机:

sudo vi /etc/apache2/extra/httpd-vhosts.conf

进入编辑,把文件底部默认开启的两个作为例子的虚拟主机,改为如下配置:

<VirtualHost *:80>
	DocumentRoot "/Library/WebServer/Documents"
	ServerName localhost
	ErrorLog "/private/var/log/apache2/localhost-error_log"
	CustomLog "/private/var/log/apache2/localhost-access_log" common
</VirtualHost>

<VirtualHost *:80>
	DocumentRoot "/Users/snandy/work"
	ServerName mysites
	ErrorLog "/private/var/log/apache2/sites-error_log"
	CustomLog "/private/var/log/apache2/sites-access_log" common
<Directory />
            Options Indexes FollowSymLinks MultiViews
            AllowOverride None
            Order deny,allow
            Allow from all
  </Directory>
</VirtualHost>

保存并退出重启:

:wq
sudo apachectl restart

配置完成之后开始压测:

ab -n 4 -c 2 http://192.168.1.210/

-n后面的是请求数
-c后面的是并发数
测试解析示范:

[dev@web ~]$ ab -c 20 -n 50000 http://192.168.1.210/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 192.168.1.210 (be patient)
Completed 5000 requests
Completed 10000 requests
Completed 15000 requests
Completed 20000 requests
Completed 25000 requests
Completed 30000 requests
Completed 35000 requests
Completed 40000 requests
Completed 45000 requests
Completed 50000 requests
Finished 50000 requests
Server Software: nginx/1.6.2
Server Hostname: 192.168.1.210
Server Port: 80
Document Path: /
Document Length: 41005 bytes # 请求的页面大小
Concurrency Level: 20 # 并发量
Time taken for tests: 1180.733 seconds # 测试总共耗时
Complete requests: 50000 # 完成的请求
Failed requests: 0 # 失败的请求
Write errors: 0 # 错误
Total transferred: 2067550000 bytes # 总共传输数据量
HTML transferred: 2050250000 bytes
Requests per second: 42.35 [#/sec] (mean) # 每秒钟的请求量。(仅仅是测试页面的响应速度)
Time per request: 472.293 [ms] (mean) # 等于 Time taken for tests/(complete requests/concurrency level) 即平均请求等待时间(用户等待的时间)
Time per request: 23.615 [ms] (mean, across all concurrent requests) # 等于 Time taken for tests/Complete requests 即服务器平均请求响应时间 在并发量为1时 用户等待时间相同
Transfer rate: 1710.03 [Kbytes/sec] received # 平均每秒多少K,即带宽速率
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 18.5 0 1001
Processing: 38 471 534.1 155 9269
Waiting: 37 456 524.6 147 9259
Total: 40 472 534.5 155 9269
Percentage of the requests served within a certain time (ms)
50% 155
66% 571
75% 783
80% 871
90% 1211
95% 1603
98% 1839
99% 2003
100% 9269 (longest request)

①Requests per second 吞吐率
计算公式:总请求数/处理完成这些请求数所花费的时间,即
Request per second=Complete requests/Time taken for tests
②Concurrency Level 并发用户数
要注意区分这个概念和并发连接数之间的区别,一个用户可能同时会产生多个会话,也即连接数。在HTTP/1.1下,IE7支持两个并发连接,IE8支持6个并发连接,FireFox3支持4个并发连接,所以相应的,我们的并发用户数就得除以这个基数。
③Time per request 用户平均请求等待时间
计算公式:处理完成所有请求数所花费的时间/(总请求数/并发用户数),即:
Time per request=Time taken for tests/(Complete requests/Concurrency Level)
④Time per request:across all concurrent requests 服务器平均请求等待时间
计算公式:处理完成所有请求数所花费的时间/总请求数,即:
Time taken for/testsComplete requests

参考链接:https://www.cnblogs.com/lixuan1998/p/7077923.html
https://blog.csdn.net/codeiswhat/article/details/51497831
https://blog.csdn.net/u012895183/article/details/83113384

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章