服務器負載太大而影響程序效率是很常見的,Apache服務器自帶有一個叫ab(ApacheBench)的工具,在bin目錄下。ab專門用於HTTP Server的benchmark testing,可以同時模擬多個併發請求,使用這個輕巧的工具我們可以對服務器進行負載測試。
今天在公司也用它作一些測試,現在整理了下它的一些東西分享下。
首先我們要得到Apache服務器的目錄下bin的路徑,我電腦中的路徑是D:\wamp\bin\apache\Apache2.2.21\bin,打開cmd,轉到這個目錄下,在其中輸入:ab -n 10 -c 10 //www.jb51.net/ 這條指令,這條指令的意思是:ab -n 全部請求數 -c 併發數 測試URL。這裏值得注意的是,如果你的測試URL是一個網站的網址,請記得在其後加上/,否則會無法工作。
以下是我運行的結果:
D:\wamp\bin\apache\Apache2.2.21\bin>ab -n 10 -c 10 //www.jb51.net/
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.jb51.net (be patient)…..done
Server Software: Microsoft-IIS/6.0 //Microsoft-IIS服務器版本6.0
Server Hostname: www.jb51.net //服務器主機名
Server Port: 80 //服務器端口
Document Path: / //測試的頁面文檔
Document Length: 32639 bytes //文檔大小
Concurrency Level: 10 //併發數
Time taken for tests: 13.548 seconds //整個測試持續的時間
Complete requests: 10 //完成的請求數量
Failed requests: 0 //失敗的請求數量
Write errors: 0
Total transferred: 331070 bytes //整個場景中的網絡傳輸量
HTML transferred: 326390 bytes //整個場景中的HTML內容傳輸量
Requests per second: 0.74 [#/sec] (mean) //每秒事務數 ,後面括號中的 mean 表示這是一個平均值
Time per request: 13547.775 [ms] (mean) //平均事務響應時間 ,後面括號中的 mean 表示這是一個平均值
Time per request: 1354.777 [ms] (mean, across all concurrent requests) //每個請求實際運行時間的平均值
Transfer rate: 23.86 [Kbytes/sec] received //平均每秒網絡上的流量,可以幫助排除是否存在網絡流量過大導致響應時間延長的問題
Connection Times (ms) //網絡上消耗的時間的分解
min mean[+/-sd] median max
Connect: 1 2 0.8 2 3
Processing: 2163 3981 3420.2 2957 13540
Waiting: 1305 3204 3595.3 2096 13169
Total: 2164 3983 3420.0 2959 13541
//以下是整個場景中所有請求的響應情況。在場景中每個請求都有一個響應時間,其中50%的用
戶響應時間小於2959毫秒,66% 的用戶響應時間小於3074毫秒,最大的響應時間小於13541 毫
秒。由於對於併發請求,cpu實際上並不是同時處理的,而是按照每個請求獲得的時間片逐個輪轉
處理的,所以基本上第一個Time per request時間約等於第二個Time per request時間乘以
併發請求數。
Percentage of the requests served within a certain time (ms)
50% 2959
66% 3074
75% 3974
80% 4008
90% 13541
95% 13541
98% 13541
99% 13541
100% 13541 (longest request)
ab的指令中參數的介紹
-n requests 全部請求數
-c concurrency 併發數
-t timelimit 最傳等待迴應時間
-p postfile POST數據文件
-T content-type POST Content-type
-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 加入cookie, eg. ‘Apache=1234. (repeatable)
-H attribute 加入http頭, eg. ‘Accept-Encoding: gzip'
Inserted after all normal header lines. (repeatable)
-A attribute http驗證,分隔傳遞用戶名及密碼
-P attribute Add Basic Proxy Authentication, the attributes
are a colon separated username and password.
-X proxy:port 代理服務器
-V 查看ab版本
-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
-h Display usage information (this message)