Site icon泓源视野

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/

参数:

 

完整的帮助文档(英文)

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":"iloveyou@mimvp.com","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":"iloveyou@mimvp.com","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)

 

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

htop 安装 : sudo yum -y install htop

 

 

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.

参数说明:

-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 开发者工具  —>  网络   —> 消息头,如下图

 

2)Chrome 谷歌浏览器查看

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

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

Exit mobile version