安裝了 apache 後, 在 bin 目錄下會有個叫 ab 的工具。它能夠模擬併發的 http 請求。咱們能夠用它來進行簡單的壓力測試。
進入 apache 的 bin 目錄,運行 ./ab -V 能夠查看相關信息:
This is ApacheBench, Version 2.0.40-dev <$Revision: 1.146 $> apache-2.0
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright 2006 The Apache Software Foundation, http://www.apache.org/
進行某個地址的測試:
[root
@localhost ~]# ab -n1000 -c10 http://statis.abc.cn/
This is ApacheBench, Version 2.0.40-dev <$Revision: 1.146 $> apache-2.0
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright 2006 The Apache Software Foundation, http://www.apache.org/
Benchmarking statis.abc.cn (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Finished 1000 requests
Server Software: nginx/1.0.10
Server Hostname: statis.abc.cn
Server Port: 80
Document Path: /
Document Length: 4881 bytes
Concurrency Level: 10
Time taken for tests: 5.341769 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 5253000 bytes
HTML transferred: 4881000 bytes
Requests per second: 187.20 [#/sec] (mean)
Time per request: 53.418 [ms] (mean)
Time per request: 5.342 [ms] (mean, across all concurrent requests)
Transfer rate: 960.17 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 19 11.6 18 40
Processing: 14 33 12.0 33 57
Waiting: 10 29 11.9 29 53
Total: 24 52 9.5 51 91
Percentage of the requests served within a certain time (ms)
50% 51
66% 55
75% 56
80% 59
90% 65
95% 73
98% 78
99% 82
100% 91 (longest request)
--------------------------------------
上面信息的解釋:
-n1000
總請求數爲 1000
-c10
併發用戶數爲 10
http://app.abc.cn/
請求目標URL
Server Software
被測試的地址用的WEB服務器名稱。這裏是 nginx 1.0.10.它來自 HTTP 響應頭。
Server Hostname
請求的URL的主機部分名稱,也來自 HTTP 響應頭。
Server Port
被測試的WEB服務器軟件監聽端口。
DocumentPath
請求的URL中的根絕對路徑,也是來自HTTP請求頭。
DocumentLength
響應數據正文長度
Concurrency Level
併發用戶數,這裏是咱們設置的參數。
Time taken for tests
全部這些請求處理完成所用的時間。
Complete requests
總請求數,這裏是咱們設置的參數
Failed requests
失敗的請求數,失敗指的是請求在鏈接服務器,發送數據,接收數據等環境發生異常,以及無響應後超時的狀況。對於超時的時間,能夠在 ab -t 參數設置.
Total transferred
全部請求的響應數據長度總和。包括每一個HTTP響應數據的頭信息和正文數據長度。注意,它不包括請求數據的長度。
HTML transferred
表示全部請求的響應數據中正文數據的總和,也就是 Total transferred 中送去響應頭的數據總和。
Requests per second
吞吐率: Complete requests / Time
Time per request
用戶平均請求等待時間:Time taken for tests / (Complete requests / Concurrency Level)
Time per request
服務器平均請求處理時間:Time taken for tests /Complete requests.這正是吞吐率的倒數
Transfer rate
這些請求在單位時間內從服務器獲取的數據長度:Total transferred / Time taken for tests
這個統計能夠很好說明服務器在處理能力達到極限時,其出口帶寬的需求量。
Percentage of the requests served within a certain time (ms)
每一個請求處理時間的分佈狀況。上面的結果顯示 80% 的請求響應時間不超過 59ms;(這是很是長的時間了,可能頁面裏有些複雜的邏輯)
*******************************************************
將併發用戶數從 10 變成 100,其它條件不變,看結果
[root
@localhost ~]# ab -n1000 -c100 http://statis.abc.cn/
Server Software: nginx/1.0.10
Server Hostname: statis.easymobi.cn
Server Port: 80
Document Path: /
Document Length: 4881 bytes
Concurrency Level: 100
Time taken for tests: 4.746772 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 5265312 bytes
HTML transferred: 4891824 bytes
Requests per second: 210.67 [#/sec] (mean)
Time per request: 474.677 [ms] (mean)
Time per request: 4.747 [ms] (mean, across all concurrent requests)
Transfer rate: 1083.05 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 1 136 343.4 111 3130
Processing: 22 241 281.6 128 2382
Waiting: 18 181 166.8 125 1176
Total: 41 377 422.1 240 3247
Percentage of the requests served within a certain time (ms)
50% 240
66% 248
75% 255
80% 365
90% 766
95% 1064
98% 1483
99% 3232
100% 3247 (longest request)
從結果中看出,吞吐率從 187.20 增加到 210.67.服務器平均請求處理時間從原來的 5.342ms 降到 4.747ms.而平均等待時間從 53.418 增長到 474.677.
可見,隨着併發用戶的變化,吞吐率,等待時間,請求處理時間都發生了相應的變化。