內容
本文介紹了壓力測試工具Apache Bench在Centos系統中的安裝,常用的測試指令,以及測試結果的解讀。
版本
操作系統: CentOS 7.2 64位
Apache Bench:2.3
說明
轉載請說明出處:Linux入門實踐筆記(六)——壓力測試工具Apache Bench的安裝、使用和結果解讀
步驟
Apache Bench的介紹
Apache Bench,簡稱ab,是Apache服務器自帶的基准性能測試的工具。可用於壓力測試,用來測試系統的吞吐量、服務端請求平均處理時間、用戶請求平均等待時間等性能指標。
ab命令上手很簡單,通過ab命令可以創建並發線程模擬多用戶訪問或調用某一URL地址。ab命令的選項、輸出的介紹可以參考其當前版本官網文檔。
首先,我們介紹下ab檢測的性能指標:
吞吐量
吞吐量(Requests per second)是在某個並發度下服務器每秒處理的請求數。它是服務器並發處理能力的量化描述,單位是reqs/s。計算公式為:總請求數/處理請求的總耗時。 吞吐量越大說明服務器的性能越好。
請求平均處理時間
請求平均處理時間(Time per request,across all concurrent requests)是服務器處理請求的平均時間,計算公式為:處理請求的總耗時/總請求數。它是服務器吞吐量的倒數。也等於,用戶請求平均等待時間/並發用戶數。
請求平均等待時間
請求平均等待時間(Time per request)是用戶等待請求響應的平均時間,計算公式為:處理請求的總耗時/(總請求數/並發用戶數)。
“請求平均處理時間”和“請求平均等待時間”兩個概念非常容易混淆,舉一個例子進行說明:比如100個用戶同時執行上傳文檔的操作,那么並發用戶數為100,假設服務器可以同時處理這100個請求,並且每個文件上傳操作的耗時都是1s。那么請求總耗時時間為1s,吞吐量為100reqs/s。請求平均處理時間為0.01s,請求平均等待時間為1s。也就是說,請求平均處理時間是從服務器的角度出發的,請求平均等待時間是從用戶的角度出發的。
Apache Bench的安裝
執行sudo yum -y install httpd-tools 安裝apache httpd的工具包,這個工具包中包含壓力測試工具Apache Bench。
#安裝apache httpd的工具包
[root@Server ~]# yum install httpd-tools
Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile Resolving Dependencies --> Running transaction check ---> Package httpd-tools.x86_64 0:2.4.6-80.el7.centos.1 will be installed --> Finished Dependency Resolution #中間省略
Installed: httpd-tools.x86_64 0:2.4.6-80.el7.centos.1 Complete!
執行命令ab -V,如果成功打印ab的版本信息,則表明ab安裝成功。
#成功打印ab的版本信息
[user1@Server ~]$ ab -V This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Apache Bench的使用和結果解讀
執行ab -n 100 -c 5 http://www.baidu.com/
,對百度進行ab測試。命令中的-n表示總共請求100次,-c表示並發用戶數為5,即使用5個並發線程同時請求。
注意,此處填寫的url不能只是域名,還要包含請求的路徑,如果請求根目錄,必須以"/"結尾。因此執行ab -n 100 -c 5 http://www.baidu.com
是不行的。
#使用5個並發線程,對百度進行100次請求。
[user1@Server ~]$ ab -n 100 -c 5 http://www.baidu.com/ #打印ab的版本信息
This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/ #進行基准測試
Benchmarking www.baidu.com (be patient).....done #測試結果 #HTTP服務器信息
Server Software: BWS/1.1
#服務器域名
Server Hostname: www.baidu.com #服務器端口號
Server Port: 80
#請求資源的標識符uri
Document Path: /
#第一次成功請求響應的頁面大小,如果后續請求響應的頁面大小發生變化,會認為請求出錯。
Document Length: 118155 bytes #並發數
Concurrency Level: 5
#整個請求的總耗時
Time taken for tests: 1.244 seconds #成功收到響應的請求數
Complete requests: 100
#失敗請求數,失敗包括連接,讀取,不正確的內容長度或異常4種情況。
Failed requests: 98 (Connect: 0, Receive: 0, Length: 98, Exceptions: 0) #寫入錯誤數
Write errors: 0
#測試過程從服務器接收到的數據總量(包含Header信息)
Total transferred: 11968154 bytes #測試過程從服務器接收到的HTML頁面的大小(不包含Hreader信息)
HTML transferred: 11872694 bytes #服務器的吞吐量:平均每秒處理的請求數
Requests per second: 80.39 [#/sec] (mean) #???
Time per request: 62.197 [ms] (mean) #請求的平均處理時間,是服務器吞吐量的倒數
Time per request: 12.439 [ms] (mean, across all concurrent requests) #每秒數據流量的大小
Transfer rate: 9395.67 [Kbytes/sec] received #連接耗時
Connection Times (ms) #最小 平均 最大
min mean[+/-sd] median max Connect: 6 10 5.3 7 34 Processing: 23 43 44.7 29 390 Waiting: 9 14 5.7 11 32 Total: 29 53 47.0 37 408
#特定耗時的請求數占比
Percentage of the requests served within a certain time (ms) 50% 37 #50%的請求在37ms以內
66% 55
75% 60
80% 63
90% 76
95% 102
98% 273
99% 408 #99%的請求在408ms以內
100% 408 (longest request)