Linux 下的Web网页性能压测工具: ApacheBench 和 WebBench 变相版ddos攻击-泓源视野

Linux 下的Web网页性能压测工具: ApacheBench 和 WebBench 变相版ddos攻击

Linux 下的Web网页性能压测工具: ApacheBench 和 WebBench

 

1. ApacheBench

ab 压测工具安装

ab是针对apache的性能测试工具,可以只安装ab工具,支持 http 和 https 网页。

Ubuntu安装:sudo apt-get -y install apache2-utils

CentOS安装:sudo yum -y install httpd-tools

 

ab 参数详细解释

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

示例: ab -n 1000 -c 100 https://mimvp.com/

参数:

  • -n 在测试会话中所执行的请求个数。默认仅执行一个请求。
  • -c 一次产生的请求个数。默认是一次一个请求;若为100,则表示每次并发请求100个。
  • -t 测试所进行的最大秒数。其内部隐含值是-n 50000,它可以使对服务器的测试限制在一个固定的总时间以内。默认时,没有时间限制。
  • -p 包含了需要POST的数据的文件。
  • -P 对一个中转代理提供BASIC认证信任。用户名和密码由一个:隔开,并以base64编码形式发送。无论服务器是否需要(即, 是否发送了401认证需求代码),此字符串都会被发送。
  • -T POST数据所使用的Content-type头信息。
  • -v 设置显示信息的详细程度-4或更大值会显示头信息,3或更大值可以显示响应代码(404,200等),2或更大值可以显示警告和其他信息。
  • -V 显示版本号并退出。
  • -w 以HTML表的格式输出结果。默认时,它是白色背景的两列宽度的一张表。
  • -i 执行HEAD请求,而不是GET。
  • -x 设置属性的字符串。
  • -X 对请求使用代理服务器。
  • -y 设置属性的字符串。
  • -z 设置属性的字符串。
  • -C 对请求附加一个Cookie:行。其典型形式是name=value的一个参数对,此参数可以重复。
  • -H 对请求附加额外的头信息。此参数的典型形式是一个有效的头信息行,其中包含了以冒号分隔的字段和值的对(如,”Accept-Encoding:zip/zop;8bit”)。
  • -A 对服务器提供BASIC认证信任。用户名和密码由一个:隔开,并以base64编码形式发送。无论服务器是否需要(即,是否发送了401认证需求代码),此字符串都会被发送。
  • -h 显示使用方法。
  • -d 不显示”percentage served within XX [ms] table”的消息(为以前的版本提供支持)。
  • -e 产生一个以逗号分隔的(CSV)文件,其中包含了处理每个相应百分比的请求所需要(从1%到100%)的相应百分比的(以微妙为单位)时间。由于这种格式已经“二进制化”,所以比’gnuplot’格式更有用。
  • -g把所有测试结果写入一个’gnuplot’或者TSV(以Tab分隔的)文件。此文件可以方便地导入到Gnuplot,IDL,Mathematica,Igor甚至Excel中。其中的第一行为标题。
  • -i 执行HEAD请求,而不是GET。
  • -k 启用HTTP KeepAlive功能,即在一个HTTP会话中执行多个请求。默认时,不启用KeepAlive功能。
  • -q 如果处理的请求数大于150,ab每处理大约10%或者100个请求时,会在stderr输出一个进度计数。此-q标记可以抑制这些信息。

 

完整的帮助文档(英文)

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
[root@mimvp-bj script]# ab -h
Usage: ab [options] [http[s]://]hostname[:port]/path
Options are:
    -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
    -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.
    -h              Display usage information (this message)
    -Z ciphersuite  Specify SSL/TLS cipher suite (See openssl ciphers)
    -f protocol     Specify SSL/TLS protocol
                    (SSL3, TLS1, TLS1.1, TLS1.2 or ALL)

 

POST 请求

### post data 文件 user_login 内容: {"email":"[email protected]","password":"mimvp.com"}

ab -n 100 -kc 10 -p user_login -T 'application/json' -H 'Accept-Encoding:gzip, deflate' -H 'accept-language:zh-Hans-CN;q=1, en-CN;q=0.9' https://mimvp.com/usercenter/login

参数说明:

-n 100  表示请求100次

-kc 10  表示保持 HTTP KeepAlive 一次并发10个请求

-p user_login 表示POST请求的文件,json数据格式,如上 {"email":"[email protected]","password":"mimvp.com"}

-T 'application/json'  POST数据所使用的Content-type头信息

-H 'Accept-Encoding:gzip, deflate'  表示请求附加额外的头信息,网页压缩算法

-H 'accept-language:zh-Hans-CN;q=1, en-CN;q=0.9'  表示请求附加额外的头信息,网页编码

https://mimvp.com/usercenter/login  表示请求的网址,如米扑科技主页

 

GET 请求

ab -n 1000 -c 100 https://mimvp.com/

运行结果如下:

Completed 1000 requests
Finished 1000 requests

Server Software:        nginx
Server Hostname:        mimvp.com
Server Port:            443
SSL/TLS Protocol:       TLSv1.2,ECDHE-RSA-AES128-GCM-SHA256,2048,128

Document Path:          /
Document Length:        16740 bytes         // 页面大小

Concurrency Level:      100                       // 并发数
Time taken for tests:   295.766 seconds    // 总耗时
Complete requests:      1000                     // 请求数
Failed requests:        141                           // 失败请求数,主要是SSL连接失败,大都是SSL证书授权失败
(Connect: 0, Receive: 0, Length: 141, Exceptions: 0)
Write errors:           0
Non-2xx responses:      8
Total transferred:      14750512 bytes                   // 总共传输字节数,包含http的头信息等
HTML transferred:       14550084 bytes               // html字节数,实际的页面传递字节数
Requests per second:    3.38 [#/sec] (mean)         // 每秒多少请求,非常重要的参数,表示服务器的吞吐量,即并发的每一个请求耗时的倒数
Time per request:       29576.594 [ms] (mean)     // 每次请求耗时,因为一次请求并发100个,因此每个并发耗时需除以100
Time per request:       295.766 [ms] (mean, across all concurrent requests)     // 并发的每一个请求耗时
Transfer rate:          48.70 [Kbytes/sec] received       // 每秒获取的数据长度

Connection Times (ms)
min  mean[+/-sd] median   max
Connect:        0  720 894.8    434    4550
Processing:    12 21135 19444.4  13948  130109
Waiting:        0 21325 19781.2  14559  130109
Total:         28 21855 19515.1  14579  130547

Percentage of the requests served within a certain time (ms)
50%  14579         ## 50%的请求在 14579ms 内返回
66%  21043         ## 66%的请求在 21043ms 内返回
75%  26262
80%  30834
90%  52050
95%  65124
98%  79861
99%  89688
100%  130547 (longest request)

Linux 下的Web网页性能压测工具: ApacheBench 和 WebBench 变相版ddos攻击插图

 

测试时,可以在被测试的服务器上,使用htop命令查看CPU和内存的实时使用情况:

htop 安装 : sudo yum -y install htop

Linux 下的Web网页性能压测工具: ApacheBench 和 WebBench 变相版ddos攻击插图1

 

 

2. WebBench

WebBench 最多可以模拟3万个并发连接,去测试网站的负载能力

米扑认为,WebBench比Apache自带的ab压力测试工具好,安装也方便。

Web Bench is very simple tool for benchmarking WWW or proxy servers. Uses fork() for simulating multiple clients and can use HTTP/0.9-HTTP/1.1 requests. This benchmark is not very realistic, but it can test if your HTTPD can realy handle that many clients at once (try to run some CGIs) without taking your machine down. Displays pages/min and bytes/sec. Can be used in more aggressive mode with -f switch.

WebBench 官网:http://home.tiscali.cz/~cz210552/webbench.html

 

1)准备工作

yum -y install ctags wget make apr* autoconf automake gcc gcc-c++

 

2)WebBench 下载安装

wget http://home.tiscali.cz/~cz210552/distfiles/webbench-1.5.tar.gz

tar zxvf webbench-1.5.tar.gz
cd webbench-1.5
make && make install

 

3) WebBench 参数含义

[root@mimvp-bj script]# webbench -h
webbench [option]... URL
-f |--force               Don't wait for reply from server.
-r |--reload              Send reload request - Pragma: no-cache.
-t |--time <sec>          Run benchmark for <sec> seconds. Default 30.
-p |--proxy <server:port> Use proxy server for request.
-c |--clients <n>         Run <n> HTTP clients at once. Default one.
-9 |--http09              Use HTTP/0.9 style requests.
-1 |--http10              Use HTTP/1.0 protocol.
-2 |--http11              Use HTTP/1.1 protocol.
--get                    Use GET request method.
--head                   Use HEAD request method.
--options                Use OPTIONS request method.
--trace                  Use TRACE request method.
-? |-h|--help             This information.
-V |--version             Display program version.

 

4)WebBench 压测示例

[root@mimvp-bj script]# webbench -c 500 -t 30 http://mimvp.com/
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://mimvp.com/
500 clients, running 30 sec.

Speed=12348 pages/min, 73882 bytes/sec.
Requests: 6174 susceed, 0 failed.

Linux 下的Web网页性能压测工具: ApacheBench 和 WebBench 变相版ddos攻击插图2

参数说明:

-c 500  表示同时产生500个并发连接

-t 30  表示请求持续时间

 

计算每次请求耗时:

Seconds / Requests = 30 / 6174 = 4.86 ms/次

 

 

nginx 支持 http2

参见 Module ngx_http_v2_module,需要加上编译参数 --with-http_v2_module

米扑科技搭建的云服务器编译命令如下:

./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module

http2需要浏览器配合,Chrome和Firefox浏览器都已经在2015年就支持了http2,http2给https加速不少。

例如,米扑科技的域名:https://mimvp.com   或  米集财富: https://mimji.com

1)FireFox 火狐浏览器查看

浏览器打开米扑科技首页 https://mimvp.com —> F12 开发者工具  —>  网络   —> 消息头,如下图

Linux 下的Web网页性能压测工具: ApacheBench 和 WebBench 变相版ddos攻击插图3

 

2)Chrome 谷歌浏览器查看

打开链接:chrome://net-internals/#http2

Linux 下的Web网页性能压测工具: ApacheBench 和 WebBench 变相版ddos攻击插图4

经过服务器硬件和软件的升级,米扑科技及其产品都开启https后,访问速度提高很多、网页打开速度很快。

本文由 泓源视野 作者:admin 发表,其版权均为 泓源视野 所有,文章内容系作者个人观点,不代表 泓源视野 对观点赞同或支持。如需转载,请注明文章来源。
22

发表评论

Protected with IP Blacklist CloudIP Blacklist Cloud
您是第8234160 位访客, 您的IP是:[3.133.79.70]