使用ab命令對網站進行壓力測試

  • 2019 年 10 月 4 日
  • 筆記

概述

這是apache自帶的一個網站壓力測試工具,是ApacheBench的縮寫,我們可以使用這個工具來對我們的網站進行壓力測試

安裝

在ubuntu上要安裝下面這個包 sudo apt-get install apache2-utils 在centos下是下面這個包 sudo yum install httpd-tools -y

使用

可以直接輸入ab來看參數和使用方法

➜  ~ ab  ab: wrong number of arguments  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      -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)  ➜  ~  

下面介紹一個最常用的使用方法 ab -c 10 -n 100 http://www.example.com/ 上面的意思是模仿10個用戶對網站進行1000次請求 下面是輸出

➜  ~  ab -c 10 -n 100 http://www.linux.cn/  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 www.linux.cn (be patient).....done      Server Software:        nginx  Server Hostname:        www.linux.cn  Server Port:            80    Document Path:          /  Document Length:        178 bytes    Concurrency Level:      10  Time taken for tests:   0.861 seconds  Complete requests:      100  Failed requests:        0  Non-2xx responses:      100  Total transferred:      35800 bytes  HTML transferred:       17800 bytes  Requests per second:    116.09 [#/sec] (mean)  Time per request:       86.142 [ms] (mean)  Time per request:       8.614 [ms] (mean, across all concurrent requests)  Transfer rate:          40.59 [Kbytes/sec] received    Connection Times (ms)                min  mean[+/-sd] median   max  Connect:       32   41   7.3     37      53  Processing:    33   42   7.4     38      55  Waiting:       33   42   7.4     38      55  Total:         65   83  14.7     75     108    Percentage of the requests served within a certain time (ms)    50%     75    66%     78    75%    102    80%    103    90%    105    95%    106    98%    107    99%    108   100%    108 (longest request)  

其實你最主要看的輸出參數是下面這幾個

  • Server Software 服務器軟件
  • Server Hostname 主機名
  • Server Port 服務器端口
  • Time taken for tests 測試時間
  • Complete requests完成的請求
  • Failed requests 失敗的請求
  • Time per request 每次請求的時間
  • Transfer rate 傳輸速度