1. 程式人生 > >linux 下ab壓力測試

linux 下ab壓力測試

服務器 percent 文件的 src cpu 情況 zip min ear

1.ab的簡介

ab是apachebench命令的縮寫。

ab是apache自帶的壓力測試工具。ab非常實用,它不僅可以對apache服務器進行網站訪問壓力測試,也可以對或其它類型的服務器進行壓力測試。比如nginx、tomcat、IIS等

2.ab的原理

ab的原理:ab命令會創建多個並發訪問線程,模擬多個訪問者同時對某一URL地址進行訪問。它的測試目標是基於URL的,因此,它既可以用來測試apache的負載壓力,也可以測試nginx、lighthttp、tomcat、IIS等其它Web服務器的壓力。

ab命令對發出負載的計算機要求很低,它既不會占用很高CPU,也不會占用很多內存。但卻會給目標服務器造成巨大的負載,其原理類似CC攻擊。自己測試使用也需要註意,否則一次上太多的負載。可能造成目標服務器資源耗完,嚴重時甚至導致死機。

3.ab的安裝

yum -y install httpd-tools

測試安裝是否成功:
[root@vic html]# ab -V 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/

4.ab的參數說明

技術分享圖片
[root@vic html]# ab --help
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
    -t timelimit    Seconds to max. wait for responses
    -b windowsize   Size of TCP send/receive buffer, in bytes
    -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 for POSTing, 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.
    -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 (SSL2, SSL3, TLS1, or ALL)
技術分享圖片

詳情說明:

-n在測試會話中所執行的請求個數。默認時,僅執行一個請求。請求的總數量

-c一次產生的請求個數。默認是一次一個。請求的用戶量

-t測試所進行的最大秒數。其內部隱含值是-n 50000,它可以使對服務器的測試限制在一個固定的總時間以內。默認時,沒有時間限制。

-V顯示版本號並退出。

5.性能指標

5.1吞吐量(Requests per second)

技術分享圖片
服務器並發處理能力的量化描述,單位是reqs/s,指的是在某個並發用戶數下單位時間內處理的請求數。某個並發用戶數下單位時間內能處理的最大請求數,稱之為最大吞吐率。
記住:吞吐率是基於並發用戶數的。這句話代表了兩個含義:
a、吞吐率和並發用戶數相關
b、不同的並發用戶數下,吞吐率一般是不同的
計算公式:總請求數/處理完成這些請求數所花費的時間,即
Request per second=Complete requests/Time taken for tests
必須要說明的是,這個數值表示當前機器的整體性能,值越大越好。
技術分享圖片

5.2 並發連接數(The number of concurrent connections)

並發連接數指的是某個時刻服務器所接受的請求數目,簡單的講,就是一個會話。

5.3 並發用戶數(Concurrency Level)

要註意區分這個概念和並發連接數之間的區別,一個用戶可能同時會產生多個會話,也即連接數。在HTTP/1.1下,IE7支持兩個並發連接,IE8支持6個並發連接,FireFox3支持4個並發連接,所以相應的,我們的並發用戶數就得除以這個基數。

5.4 用戶平均請求等待時間(Time per request)

計算公式:處理完成所有請求數所花費的時間/(總請求數/並發用戶數),即:
Time per request=Time taken for tests/(Complete requests/Concurrency Level)

5.5 服務器平均請求等待時間(Time per request:across all concurrent requests)

計算公式:處理完成所有請求數所花費的時間/總請求數,即:
Time taken for/testsComplete requests
可以看到,它是吞吐率的倒數。
同時,它也等於用戶平均請求等待時間/並發用戶數,即
Time per request/Concurrency Level

6.ab的應用

ab的命令參數比較多,我們經常使用的是-c和-n參數。

ab -c 10 -n 100 http://www.myvick.cn/index.php :同時處理100個請求並運行10次index.php

  -c10表示並發用戶數為10

  -n100表示請求總數為100

技術分享圖片
[root@vic html]# ab -c 10 -n 100 http://www.myvick.cn/index.php
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 www.myvick.cn (be patient).....done


Server Software:        nginx/1.13.6   #測試服務器的名字
Server Hostname:        www.myvick.cn  #請求的URL主機名
Server Port:            80             #web服務器監聽的端口

Document Path:          /index.php    #請求的URL中的根絕對路徑,通過該文件的後綴名,我們一般可以了解該請求的類型
Document Length:        799 bytes       #HTTP響應數據的正文長度

Concurrency Level:      10        # 並發用戶數,這是我們設置的參數之一
Time taken for tests:   0.668 seconds   #所有這些請求被處理完成所花費的總時間 單位秒
Complete requests:      100         # 總請求數量,這是我們設置的參數之一
Failed requests:        0          # 表示失敗的請求數量,這裏的失敗是指請求在連接服務器、發送數據等環節發生異常,以及無響應後超時的情況
Write errors:           0
Total transferred:      96200 bytes    #所有請求的響應數據長度總和。包括每個HTTP響應數據的頭信息和正文數據的長度
HTML transferred:       79900 bytes    # 所有請求的響應數據中正文數據的總和,也就是減去了Total transferred中HTTP響應數據中的頭信息的長度
Requests per second:    149.71 [#/sec] (mean) #吞吐率,計算公式:Complete requests/Time taken for tests  總請求數/處理完成這些請求數所花費的時間
Time per request:       66.797 [ms] (mean)   # 用戶平均請求等待時間,計算公式:Time token for tests/(Complete requests/Concurrency Level)。處理完成所有請求數所花費的時間/(總請求數/並發用戶數)
Time per request:       6.680 [ms] (mean, across all concurrent requests) #服務器平均請求等待時間,計算公式:Time taken for tests/Complete requests,正好是吞吐率的倒數。也可以這麽統計:Time per request/Concurrency Level
Transfer rate:          140.64 [Kbytes/sec] received  #表示這些請求在單位時間內從服務器獲取的數據長度,計算公式:Total trnasferred/ Time taken for tests,這個統計很好的說明服務器的處理能力達到極限時,其出口寬帶的需求量。

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1    2   0.7      2       5
Processing:     2   26  81.3      3     615
Waiting:        1   26  81.3      3     615
Total:          3   28  81.3      6     618

Percentage of the requests served within a certain time (ms)
  50%      6
  66%      6
  75%      7
  80%      7
  90%     10
  95%    209
  98%    209
  99%    618
 100%    618 (longest request)

#Percentage of requests served within a certain time(ms)這部分數據用於描述每個請求處理時間的分布情況,比如以上測試,80%的請求處理時間都不超過7ms,這個處理時間是指前面的Time per request,即對於單個用戶而言,平均每個請求的處理時間
技術分享圖片

7 nginx和apache對比

  nginx性能確實比apache性能高。nginx的吞吐量大於apache的,吞吐量越大越好

linux 下ab壓力測試