Mac 使用ab命令壓測配置及使用總結

ab是apacheBench的縮寫,是一個單線程命令,是Apache服務器自帶的一個web壓力測試工具,最初是用於測試Apache HTTP Server。使用時,ab命令會創建併發訪問線程,模擬多個訪問者同時對某一個URL地址進行訪問。

使用方法:

ab [option] [http://]host[:port]/path

例如:ab -n 100 -c 10 “http://en.wikipedia.org/wiki/Main_Page”

其中[option]是ab命令提供的可選參數,列舉一些常用的參數:

-n //requests Number of requests to perform 指定壓力測試總共訪問頁面的次數,默認是1

-c //concurrency Number of multiple requests to make at a time 用於一次產生請求的併發數,默認是1

-r //Don’t exit on socket receive errors.指定接收到錯誤信息時不退出程序。

-B //address Address to bind to when making outgoing connections 綁定出口IP地址

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

-s //timeout Seconds to max. wait for each response Default is 30 seconds 每個相應的最長的時間,默認是30秒

-g //filename Output collected data to gnuplot format file 輸出結果信息到gnuplot格式的文件中。

-e //filename Output CSV file with percentages served 輸出結果信息到CSV格式的文件中。

-S // Do not show confidence estimators and warnings. 不顯示預估和警告信息。

-d //Do not show percentiles served table. 不顯示百分比。

-k //Use HTTP KeepAlive feature 使用HTTP的KeepAlive特性。

-b //windowsize Size of TCP send/receive buffer, in bytes TCP的發送/接受窗口,以字節爲單位

-p //postfile File containing data to POST 包含需要post的數據文件,例如:-p a.txt,此外還需要結合-T,並且文件的內容需類似像"key1=value1&key2=vaule2"形式

-T //content-type Content-type header for POSTing 設置Context-Type請求頭信息,如-T “application/x-www-form-urlencoded”默認值爲text/plain

-X //proxy:port Proxyserver and port number to use 指定使用的代理服務器和端口號,例如:“126.10.10.3:88”

-v //verbosity How much troubleshooting info to print 設置打印幫助信息的詳細程度,4或更大值會顯示頭信息, 3或更大值可以顯示響應代碼(404, 200等), 2或更大值可以顯示警告和其他信息。

-w //Print out results in HTML tables 以HTML表的格式輸出結果。默認時,它是白色背景的兩列寬度的一張表。

-i //Use HEAD instead of GET 使用HEAD請求代替GET請求

-C //cookie-name=value 添加cookie信息,其典型形式是name=value的一個參數對。此參數可以重複,用逗號分割。

-H //attribute Add Arbitrary header line, eg. ‘Accept-Encoding: gzip’ Inserted after all normal header lines. (repeatable) 添加任意的請求頭,例如:“Accept-Encoding: gzip”,此參數可重複

-P // proxy-auth-username:password 對一箇中轉代理提供BASIC認證信任。用戶名和密碼用英文冒號隔開,並以base64編碼形式發送。無論服務器是否需要(即, 是否發送了401認證需求代碼),此字符串都會被髮送。

-A //Add Basic WWW Authentication, the attributes are a colon separated username and password.基本網絡認證,用戶名和密碼之間用英文冒號隔開。

-x //attributes String to insert as table attributes 插入字符串作爲table標籤的屬性。

-y //attributes String to insert as tr attributes 插入字符串作爲tr標籤的屬性。

-z //attributes String to insert as td or th attributes 插入字符串作爲td標籤的屬性。

基礎用法:

ab -n 請求次數 -c 併發數 地址

其中-c選項爲一次發送的請求數量,及併發量。
-n選項爲請求次數。
前期配置:
1.在Mac中配置Apache
①啓動Apache,打開終端

sudo apachectl -v
sudo apachectl start

②設置虛擬端終機
打開Apache的配置文件

sudo vi /etc/apache2/httpd.conf

在httpd.conf中找到“#Include /private/etc/apache2/extra/httpd-vhosts.conf”,去掉前面的“#”,保存並退出,去掉這一行的#意思是從/extra/httpd-vhosts.conf這個文件導入虛擬主機配置。
然後重啓Apache

sudo apachectl restart

運行如下命令,打開配置虛擬主機文件httpd-vhost.conf,配置虛擬主機:

sudo vi /etc/apache2/extra/httpd-vhosts.conf

進入編輯,把文件底部默認開啓的兩個作爲例子的虛擬主機,改爲如下配置:

<VirtualHost *:80>
	DocumentRoot "/Library/WebServer/Documents"
	ServerName localhost
	ErrorLog "/private/var/log/apache2/localhost-error_log"
	CustomLog "/private/var/log/apache2/localhost-access_log" common
</VirtualHost>

<VirtualHost *:80>
	DocumentRoot "/Users/snandy/work"
	ServerName mysites
	ErrorLog "/private/var/log/apache2/sites-error_log"
	CustomLog "/private/var/log/apache2/sites-access_log" common
<Directory />
            Options Indexes FollowSymLinks MultiViews
            AllowOverride None
            Order deny,allow
            Allow from all
  </Directory>
</VirtualHost>

保存並退出重啓:

:wq
sudo apachectl restart

配置完成之後開始壓測:

ab -n 4 -c 2 http://192.168.1.210/

-n後面的是請求數
-c後面的是併發數
測試解析示範:

[dev@web ~]$ ab -c 20 -n 50000 http://192.168.1.210/
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 192.168.1.210 (be patient)
Completed 5000 requests
Completed 10000 requests
Completed 15000 requests
Completed 20000 requests
Completed 25000 requests
Completed 30000 requests
Completed 35000 requests
Completed 40000 requests
Completed 45000 requests
Completed 50000 requests
Finished 50000 requests
Server Software: nginx/1.6.2
Server Hostname: 192.168.1.210
Server Port: 80
Document Path: /
Document Length: 41005 bytes # 請求的頁面大小
Concurrency Level: 20 # 併發量
Time taken for tests: 1180.733 seconds # 測試總共耗時
Complete requests: 50000 # 完成的請求
Failed requests: 0 # 失敗的請求
Write errors: 0 # 錯誤
Total transferred: 2067550000 bytes # 總共傳輸數據量
HTML transferred: 2050250000 bytes
Requests per second: 42.35 [#/sec] (mean) # 每秒鐘的請求量。(僅僅是測試頁面的響應速度)
Time per request: 472.293 [ms] (mean) # 等於 Time taken for tests/(complete requests/concurrency level) 即平均請求等待時間(用戶等待的時間)
Time per request: 23.615 [ms] (mean, across all concurrent requests) # 等於 Time taken for tests/Complete requests 即服務器平均請求響應時間 在併發量爲1時 用戶等待時間相同
Transfer rate: 1710.03 [Kbytes/sec] received # 平均每秒多少K,即帶寬速率
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 18.5 0 1001
Processing: 38 471 534.1 155 9269
Waiting: 37 456 524.6 147 9259
Total: 40 472 534.5 155 9269
Percentage of the requests served within a certain time (ms)
50% 155
66% 571
75% 783
80% 871
90% 1211
95% 1603
98% 1839
99% 2003
100% 9269 (longest request)

①Requests per second 吞吐率
計算公式:總請求數/處理完成這些請求數所花費的時間,即
Request per second=Complete requests/Time taken for tests
②Concurrency Level 併發用戶數
要注意區分這個概念和併發連接數之間的區別,一個用戶可能同時會產生多個會話,也即連接數。在HTTP/1.1下,IE7支持兩個併發連接,IE8支持6個併發連接,FireFox3支持4個併發連接,所以相應的,我們的併發用戶數就得除以這個基數。
③Time per request 用戶平均請求等待時間
計算公式:處理完成所有請求數所花費的時間/(總請求數/併發用戶數),即:
Time per request=Time taken for tests/(Complete requests/Concurrency Level)
④Time per request:across all concurrent requests 服務器平均請求等待時間
計算公式:處理完成所有請求數所花費的時間/總請求數,即:
Time taken for/testsComplete requests

參考鏈接:https://www.cnblogs.com/lixuan1998/p/7077923.html
https://blog.csdn.net/codeiswhat/article/details/51497831
https://blog.csdn.net/u012895183/article/details/83113384

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章