日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用wrk进行性能测试

發布時間:2025/3/20 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用wrk进行性能测试 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1 wrk介紹

wrk是一款現代化的HTTP性能測試工具,即使運行在單核CPU上也能產生顯著的壓力。它融合了一種多線程設計,并使用了一些可擴展事件通知機制,例如epoll and kqueue。一個可選的LuaJIT腳本能產生HTTP請求,響應處理和自定義報告,更詳細的腳本內容可以參考scripts目錄下的一些例子。

2 wrk下載和安裝

先安裝git

cd /usr/local/src sudo yum install git -y

下載wrk文件

git clone https://github.com/wg/wrk.git cd wrk make

編譯成功后,目錄下就會有一個wrk文件。如果編譯過程中,出現如下錯誤:

若報錯gcc: Command not found,則需安裝gcc,參考wrk編譯報錯gcc: Command not found

若報錯fatal error: openssl/ssl.h: No such file or directory,則需要安裝openssl的庫。

sudo apt-get install libssl-dev

或者

sudo yum install openssl-devel

3 一個簡單的例子

wrk -t12 -c400 -d30s http://127.0.0.1:8080/index.html

它將會產生如下測試,12個線程(threads),保持400個HTTP連接(connections)開啟,測試時間30秒(seconds)。

詳細的命令行參數如下:

-c, --connections(連接數): total number of HTTP connections to keep open with each thread handling N = connections/threads-d, --duration(測試持續時間): duration of the test, e.g. 2s, 2m, 2h-t, --threads(線程): total number of threads to use-s, --script(腳本): LuaJIT script, see SCRIPTING-H, --header(頭信息): HTTP header to add to request, e.g. "User-Agent: wrk"--latency(響應信息): print detailed latency statistics--timeout(超時時間): record a timeout if a response is not received within this amount of time.

下面是輸出結果:

Running 30s test @ http://127.0.0.1:8080/index.html12 threads and 400 connectionsThread Stats Avg Stdev Max +/- StdevLatency 635.91us 0.89ms 12.92ms 93.69%Req/Sec 56.20k 8.07k 62.00k 86.54%22464657 requests in 30.00s, 17.76GB read Requests/sec: 748868.53 Transfer/sec: 606.33MB

結果解讀如下:

Latency: 響應信息, 包括平均值, 標準偏差, 最大值, 正負一個標準差占比。
Req/Sec: 每個線程每秒鐘的完成的請求數,同樣有平均值,標準偏差,最大值,正負一個標準差占比。
30秒鐘總共完成請求數為22464657,讀取數據量為17.76GB。
線程總共平均每秒鐘處理748868.53個請求(QPS),每秒鐘讀取606.33MB數據量。

4 發送post請求例子

首先需要創建一個post.lua文件,內容如下:

wrk.method = "POST" wrk.headers["uid"] = "127.0.0.1" wrk.headers["Content-Type"] = "application/json" wrk.body ='{"uid":"127.0.0.1","Version":"1.0","devicetype":"web","port":"8080"}'

測試執行命令如下:

./wrk --latency -t100 -c1500 -d120s --timeout=15s -s post.lua http://127.0.0.1:8080/index.html

這個腳本加入了--lantency:輸出結果里可以看到響應時間分布情況,

Running 2m test @ http://127.0.0.1:8080/index.html100 threads and 1500 connectionsThread Stats Avg Stdev Max +/- StdevLatency 127.26ms 157.88ms 2.44s 87.94%Req/Sec 177.91 45.09 1.10k 69.97%Latency Distribution50% 66.05ms75% 143.57ms90% 325.41ms99% 760.20ms2138290 requests in 2.00m, 3.17GB read Requests/sec: 17804.57 Transfer/sec: 27.05MB

5 帶隨機參數的get請求例子

如果想構造不同的get請求,請求帶隨機參數,則lua腳本如下:

request = function() num = math.random(1000,9999)path = "/test.html?t=" .. numreturn wrk.format("GET", path) end

6 添加參數txt文件的get請求例子

如果要測試的url需要參數化,uids.txt文件內容如下:

100 101 102

lua腳本如下:

urimap = {} counter = 0 function init(args)for line in io.lines("uids.txt") doprint(line)urimap[counter] = linecounter = counter + 1endcounter = 0 endrequest = function()local path ="/GetInfo.aspx?u=%s&m=1"parms = urimap[counter%(table.getn(urimap) + 1)]path = string.format(path,parms)counter = counter + 1return wrk.format(nil, path) end

7 添加參數txt文件的post請求例子

lua腳本如下:

urimap = {} counter = 0 function init(args)for line in io.lines("uids.txt") dourimap[counter] = linecounter = counter + 1endcounter = 0 endrequest = function()local body1 = '{"uid":"100%s'local body2 = '","name":"1"}'parms = urimap[counter%(table.getn(urimap) + 1)]path = "/getinfo"method = "POST"wrk.headers["Content-Type"] = "application/json"body = string.format(body1,parms)..body2counter = counter + 1return wrk.format(method, path, wrk.headers, body) end

如果要打印返回數據,可添加如下腳本:

a=1 function response(status, headers, body)if(a==1)thena=2print(body)end end

8 提交不同表單內容例子

lua腳本如下:

wrk.method = "POST" wrk.body = "" wrk.headers["Content-Type"] = "application/x-www-form-urlencoded"-- 提交不同表單內容 local queries = {"version=1.0","version=2.0","version=3.0" } local i = 0 request = function()local body = wrk.format(nil, nil, nil, queries[i % #queries + 1])i = i + 1return body end

9 訪問多個url例子

如果要隨機測試多個url,可參考wrk-scripts這個項目。

需要創建一個文件名為paths.txt,里面每行是一個要測試的url網址。lua腳本如下:

counter = 0-- Initialize the pseudo random number generator - http://lua-users.org/wiki/MathLibraryTutorial math.randomseed(os.time()) math.random(); math.random(); math.random()function file_exists(file)local f = io.open(file, "rb")if f then f:close() endreturn f ~= nil endfunction shuffle(paths)local j, klocal n = #pathsfor i = 1, n doj, k = math.random(n), math.random(n)paths[j], paths[k] = paths[k], paths[j]endreturn paths endfunction non_empty_lines_from(file)if not file_exists(file) then return {} endlines = {}for line in io.lines(file) doif not (line == '') thenlines[#lines + 1] = lineendendreturn shuffle(lines) endpaths = non_empty_lines_from("paths.txt")if #paths <= 0 thenprint("multiplepaths: No paths found. You have to create a file paths.txt with one path per line")os.exit() endprint("multiplepaths: Found " .. #paths .. " paths")request = function()path = paths[counter]counter = counter + 1if counter > #paths thencounter = 0endreturn wrk.format(nil, path) end

?

轉載于:https://www.cnblogs.com/ycyzharry/p/8372168.html

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的使用wrk进行性能测试的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。