wrk性能測試(詳解)

1、簡介html

  wrk 是一款針對 Http 協議的基準測試工具,它可以在單機多核 CPU 的條件下,使用系統自帶的高性能 I/O 機制,如 epoll,kqueue 等,經過多線程和事件模式,對目標機器產生大量的負載。git

wrk是開源的, 代碼在 github 上:https://github.com/wg/wrkgithub

安裝:https://www.cnblogs.com/savorboard/p/wrk.htmljson

 

優點:api

  • 輕量級性能測試工具
  • 安裝簡單
  • 學習曲線基本爲0,幾分鐘就學會使用了
  • 基於系統自帶的高性能I/O機制,如epoll,kqueue,利用異步的事件驅動框架,經過不多的線程就能夠壓出很大的併發量,例如幾萬、幾十萬,這是不少性能測試工具沒法作到的。

劣勢:數組

  • wrk 目前僅支持單機壓測,後續也不太可能支持多機器對目標機壓測,由於它自己的定位,並非用來取代 JMeter, LoadRunner 等專業的測試工具。

 

 2、格式及用法服務器

Usage: wrk <options> <url> Options: -c, --connections <N> Connections to keep open  -d, --duration <T> Duration of test  -t, --threads <N> Number of threads to use  -s, --script <S> Load Lua script file  -H, --header <H> Add header to request  --latency Print latency statistics  --timeout <T> Socket/request timeout  -v, --version Print version details  Numeric arguments may include a SI unit (1k, 1M, 1G) Time arguments may include a time unit (2s, 2m, 2h)

 

翻譯成中文:cookie

使用方法: wrk <選項> <被測HTTP服務的URL> Options: -c, --connections <N> 跟服務器創建並保持的TCP鏈接數量  -d, --duration <T> 壓測時間  -t, --threads <N> 使用多少個線程進行壓測,壓測時,是有一個主線程來控制咱們設置的n個子線程間調度  -s, --script <S> 指定Lua腳本路徑  -H, --header <H> 爲每個HTTP請求添加HTTP頭  --latency 在壓測結束後,打印延遲統計信息  --timeout <T> 超時時間  -v, --version 打印正在使用的wrk的詳細版本信 <N>表明數字參數,支持國際單位 (1k, 1M, 1G) <T>表明時間參數,支持時間單位 (2s, 2m, 2h)

 

 

3、簡單壓測及結果分析多線程

作一個簡單的壓測,分析下結果:併發

wrk -t8 -c200 -d30s --latency http://www.bing.com

 

輸出:

Running 30s test @ http://www.bing.com 8 threads and 200 connections Thread Stats Avg Stdev Max +/- Stdev Latency 46.67ms 215.38ms 1.67s 95.59% Req/Sec 7.91k 1.15k 10.26k 70.77%
Latency Distribution 50% 2.93ms 75% 3.78ms 90% 4.73ms 99% 1.35s 1790465 requests in 30.01s, 684.08MB read Requests/sec: 59658.29 Transfer/sec: 22.79MB

以上是使用8個線程200個鏈接,對bing首頁進行了30秒的壓測,並要求在壓測結果中輸出響應延遲信息。

如下是解釋壓測結果:

Running 30s test @ http://www.bing.com (壓測時間30s8 threads and 200 connections (共8個測試線程,200個鏈接) Thread Stats Avg Stdev Max +/- Stdev (平均值) (標準差)(最大值)(正負一個標準差所佔比例) Latency 46.67ms 215.38ms 1.67s 95.59%延遲) Req/Sec 7.91k 1.15k 10.26k 70.77%處理中的請求數) Latency Distribution (延遲分佈50% 2.93ms 75% 3.78ms 90% 4.73ms 99% 1.35s (99分位的延遲:%99的請求在1.35s之內1790465 requests in 30.01s, 684.08MB read (30.01秒內共處理完成了1790465個請求,讀取了684.08MB數據) Requests/sec: 59658.29平均每秒處理完成59658.29個請求) Transfer/sec: 22.79MB (平均每秒讀取數據22.79MB

 

 

4、使用lua腳本進行壓測

  lua腳本是一種輕量小巧的腳本語言,用標準c語言編寫,並以源代碼形式開放,其設計目的是爲了嵌入應用程序中,從而爲程序提供靈活的擴展和定製功能。wrk工具嵌入了lua腳本語言,所以,在自定義壓測場景時,可在wrk目錄下使用lua定製壓測場景。

  一、lua聲明週期

    共有三個階段,啓動階段,運行階段,結束階段。wrk支持在這三個階段對壓測進行個性化。   

啓動階段
function setup(thread)

 

在腳本文件中實現setup方法,wrk就會在測試線程已經初始化但尚未啓動的時候調用該方法。wrk會爲每個測試線程調用一次setup方法,並傳入表明測試線程的對象thread做爲參數。setup方法中可操做該thread對象,獲取信息、存儲信息、甚相當閉該線程。

thread.addr - get or set the thread's server address thread:get(name) - get the value of a global in the thread's env thread:set(name, value) - set the value of a global in the thread's env thread:stop() - stop the thread

 

運行階段
function init(args) --由測試線程調用,只會在進入運行階段時,調用一次。支持從啓動wrk的命令中,獲取命令行參數; function delay() --在每次發送request以前調用,若是須要delay,那麼delay相應時間; function request() --用來生成請求;每一次請求都會調用該方法,因此注意不要在該方法中作耗時的操做; function response(status, headers, body) --在每次收到一個響應時調用;爲提高性能,若是沒有定義該方法,那麼wrk不會解析headers和body;

 

結束階段
function done(summary, latency, requests) --在整個測試過程當中只會調用一次,可從參數給定的對象中,獲取壓測結果,生成定製化的測試報告。

   

  二、自定義腳本中可訪問的變量和方法:

  變量:wrk

wrk = { scheme = "http", host = "localhost", port = nil, method = "GET", path = "/", headers = {}, body = nil, thread = <userdata>, }

 

  方法:wrk.fomat wrk.lookup wrk.connect                  

function wrk.format(method, path, headers, body)  --根據參數和全局變量wrk,生成一個HTTP rquest string。
function wrk.lookup(host, service)  --給定host和service(port/well known service name),返回全部可用的服務器地址信息。
function wrk.connect(addr)  --測試與給定的服務器地址信息是否能夠成功建立鏈接

 

三、lua腳本壓測實例

  壓測命令:wrk -t8 -c200 -d30s --latency  -s test.lua http://www.bing.com

  test.lua是用lua寫的壓測腳本,以下是壓測腳本的實例:

  使用post方法壓測

wrk.method = "POST" wrk.headers["S-COOKIE2"]="a=2&b=Input&c=10.0&d=20191114***" wrk.body = "recent_seven=20191127_32;20191128_111" wrk.headers["Host"]="api.shouji.**.com"
function response(status,headers,body) if status ~= 200 then --將服務器返回狀態碼不是200的請求結果打印出來 print(body) -- wrk.thread:stop() end end

 

   發送json

request = function() local headers = { } headers['Content-Type'] = "application/json" body = { mobile={"1533899828"}, params={code=math.random(1000,9999)} } local cjson = require("cjson") body_str = cjson.encode(body) return wrk.format('POST', nil, headers, body_str) end

 

wrk讀取文件,實現隨機header-cookie

idArr = {} falg = 0 wrk.method = "POST" wrk.body = "a=1" function init(args) for line in io.lines("integral/cookies.txt") do print(line) idArr[falg] = line falg = falg+1 end falg = 0 end --wrk.method = "POST" --wrk.body = "a=1" --wrk.path = "/v1/points/reading"  request = function() parms = idArr[math.random(0,4)] --隨機傳遞文件中的參數 --parms = idArr[falg%(table.getn(idArr)+1)] 循環傳遞文件中的參數 wrk.headers["S-COOKIE2"] = parms falg = falg+1 return wrk.format() end

 

  wrk建立數組並初始化,拼接隨機參數

idArr = {}; function init(args) idArr[1] = "1"; idArr[2] = "2"; idArr[3] = "3"; idArr[4] = "4"; end request = function() parms = idArr[math.random(1,4)] path = "/v1/points/reading?id="..parms return wrk.format("GET",path) end

 

原文出處:https://www.cnblogs.com/l199616j/p/12156600.html

相關文章
相關標籤/搜索