1. 程式人生 > >http性能測試點滴

http性能測試點滴

root p s 不可 add 註意 body -c 信息 測試的

WeTest 導讀

在服務上線之前,性能測試必不可少。本文主要介紹性能測試的流程,需要關註的指標,性能測試工具apache bench的使用,以及常見的坑。


  • 什麽是性能測試

性能測試是通過自動化的測試工具模擬多種正常、峰值以及異常負載條件來對系統的各項性能指標進行測試。負載測試和壓力測試都屬於性能測試,兩者可以結合進行。通過負載測試,確定在各種工作負載下系統的性能,目標是測試當負載逐漸增加時,系統各項性能指標的變化情況。壓力測試是通過確定一個系統的瓶頸或者不能接受的性能點,來獲得系統能提供的最大服務級別的測試。

  • 性能測試的目標是什麽

性能測試最終的目的,是找到系統的瓶頸,一般來說,是找到服務單機最大TPS(每秒完成的事務數)。

需要註意的是,服務的TPS需要結合請求平均耗時來綜合考慮。例如:服務TPS壓到1000,平均請求耗時500ms,但是假如我們定的服務請求耗時不能超過200ms,那麽這個1000的TPS是無效的。

很多場景下,服務都會設置超時時間,若平均耗時超過此超時時間,則可認為服務處於不可用狀態。

  • 什麽時候需要性能測試

1.功能測試完成之後,上線之前。

正常情況下,上線之前,都應該進行性能測試,尤其是請求量較大的接口,重點業務的核心接口,以及直接影響用戶操作流程的接口。

2.各種大促,運營活動開始之前。

大促,運營活動,都會導致流量激增,因此上線之前做好壓力測試,評估系統性能是否滿足預估流量,提前做好準備。

舉個反面例子:聚美優品,年年大促年年掛。

再來個正面的例子:每年雙十一之前,阿裏都會有全鏈路壓測,各個業務自己也會有獨立的壓測,阿裏在這塊做得還是非常不錯的。

  • 怎麽做性能測試

常見的http性能測試工具

1. httpload

技術分享圖片

2. wrk

技術分享圖片

3. apache bench

技術分享圖片

技術分享圖片

技術分享圖片

最終我們選擇apache bench

看上去wrk才是最完美的,但是我們卻選擇了ab。我們驗證過各種工具請求數據是否準確,壓測的時候,通過後臺日誌記錄,最終得出結論,ab的請求數誤差在千分之二左右,而其他兩個工具在千分之五左右。

不過不得不說,wrk的確是一款非常優秀的壓測工具,采用異步IO模型,能壓到非常高的TPS。曾經用空邏輯接口壓到過7w的TPS,而相同接口,ab只能壓到2w多。

  • apache bench的使用

前面已經給了一個簡單的例子了,下面詳細介紹下ab的使用。

如何安裝?如果docker容器已經安裝的apache,那麽恭喜,ab是apache自帶的一個組件,不用重新安裝了。當然,也可以自己單獨安裝apache bench。

技術分享圖片

ab 常用參數介紹

參數說明:
格式:ab [options] [http://]hostname[:port]/path

-n requests Number of requests to perform //本次測試發起的總請求數
-c concurrency Number of multiple requests to make   //一次產生的請求數(或並發數)
-t timelimit Seconds to max. wait for responses    //測試所進行的最大秒數,默認沒有時間限制。
-r Don‘t exit on socket receive errors. // 拋出異常繼續執行測試任務
-p postfile File containing data to POST  //包含了需要POST的數據的文件,文件格式如“p1=1&p2=2”.使用方法是 -p 111.txt

-T content-type Content-type header for POSTing
//POST數據所使用的Content-type頭信息,如 -T “application/x-www-form-urlencoded” 。 (配合-p)
-v verbosity How much troubleshooting info to print
//設置顯示信息的詳細程度 – 4或更大值會顯示頭信息, 3或更大值可以顯示響應代碼(404, 200等), 2或更大值可以顯示警告和其他信息。 -V 顯示版本號並退出。
-C attribute Add cookie, eg. -C “c1=1234,c2=2,c3=3” (repeatable)
//-C cookie-name=value 對請求附加一個Cookie:行。 其典型形式是name=value的一個參數對。此參數可以重復,用逗號分割。
提示:可以借助session實現原理傳遞 JSESSIONID參數, 實現保持會話的功能,如-C ” c1=1234,c2=2,c3=3, JSESSIONID=FF056CD16DA9D71CB131C1D56F0319F8″ 。
-w Print out results in HTML tables  //以HTML表的格式輸出結果。默認時,它是白色背景的兩列寬度的一張表。
-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
-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
-h Display usage information (this message)

  • 性能測試報告

技術分享圖片

測試報告應該包含以下內容。當然,根據場景不同,可以適當增減指標,例如有的業務要求關註cpu,內存,IO等指標,此時就應該加上相關指標。

技術分享圖片

  • 常見的坑

1.AB發送的是http1.0請求。

2.-t可以指定時間,-n指定發送請求總數,同時使用時壓測會在-t秒或者發送了-n個請求之後停止。但是-t一定要在-n之前(ab的bug,-n在-t之前最多只會跑5s)。

3.為了使測試結果更可靠,單次壓測時間應在2分鐘以上。

理論上,壓測時間越長,結果誤差越小。同時,可以在瓶頸附近進行長時間壓測,例如一個小時或者一天,可以用來測試系統穩定性。許多系統的bug都是在持續壓力下才會暴露出來。

4.小心壓測客戶端成為瓶頸。

例如上傳,下載接口的壓測,此時壓測客戶端的網絡上行,下行速度都會有瓶頸,千萬小心服務器還沒到達瓶頸時,客戶端先到了瓶頸。此時,可以利用多客戶端同時壓測。

5.ab可以將參數寫入文件中,用此種方式可以測試上傳文件的接口。

需要配合-p -t 使用。

$ ab -n 10000 -c 8 -p post_image_1k.txt -T "multipart/form-data; boundary=1234567890" http://xxxxxxx

文件內容如下:

技術分享圖片

技術分享圖片

6.ab不支持動態構建請求參數,wrk可配合lua腳本支持動態構建請求參數,還是比較牛的。

package.path = ‘/root/wrk/?.lua;‘

local md5 = require "md5"
local body = [[BI_login|userid{145030}|openid{4-22761563}|source{}|affiliate{}|creative{}|family{}|genus{0}|ip{180.111.151.116}|from_uid{0}|login_date{2016-11-04}|login_time{10:40:13}|extra{}|srcid{1}|snid{1002}|clientid{1253}|gameid{2100012}
BI_logout|userid{184103}|openid{4-22784181}|family{}|genus{0}|ip{218.2.96.82}|logout_date{2016-11-04}|logout_time{10:40:42}|extra{}|srcid{1}|snid{1002}|clientid{1253}|gameid{2100012}
BI_role_logout|roleid{184103}|userid{184103}|openid{4-22784181}|ip{218.2.96.82}|level{100}|money{468}|power{1}|exp{252}|lijin{0}|online_time{0}|mapid{0}|posx{0}|posy{0}|rolelogout_date{2016-11-04}|rolelogout_time{10:40:42}|extra{0}|srcid{0}|snid{1002}|clientid{1253}|gameid{2100012}
BI_logout|userid{71084}|openid{4-20974629}|family{}|genus{0}|ip{117.136.8.76}|logout_date{2016-11-04}|logout_time{10:40:43}|extra{}|srcid{1}|snid{1002}|clientid{1253}|gameid{2100012}]]

--local body = "hello"
wrk.headers["Content-Type"] = "text/xml"

local i=0
request = function()
i = i+1
local path = "/v1/pub?gameid=510038&timestamp=%s&key=510038&type=basic&sign=%s"
local time = os.time()*1000
local v = "510038" .. time .. "basic98889999"
local sign = md5.sumhexa(v)
path = string.format(path, time, sign)
--print(path)
return wrk.format("POST", path, nil, body)
end

技術分享圖片


騰訊WeTest推出的“壓測大師”,一分鐘完成用例配置,無需維護測試環境,支持http協議、API接口、網站等主流壓測場景。

點擊:https://wetest.qq.com/gaps 即可體驗。

如果使用當中有任何疑問,歡迎聯系騰訊WeTest企業QQ:2852350015。

http性能測試點滴