使用 WRK 压力测试工具对 ASP.NET Core 的接口进行压力测试
0. 簡(jiǎn)要介紹
WRK 是一款輕量且易用的 HTTP 壓力測(cè)試工具,通過(guò)該工具我們可以方便地對(duì)我們所開發(fā)的 WebAPI 項(xiàng)目進(jìn)行壓力測(cè)試,并且針對(duì)測(cè)試的情況返回結(jié)果。
PS:Wrk 并不能針對(duì)測(cè)試的結(jié)果生成動(dòng)態(tài)的圖表,如果有這種需要,可以嘗試使用另一款工具 Vegeta。該項(xiàng)目使用的 Golang 進(jìn)行編寫,其 GitHub 地址為:https://github.com/tsenart/vegeta
下面的內(nèi)容就是一個(gè)標(biāo)準(zhǔn)的測(cè)試結(jié)果信息:
Copy
針對(duì) 127.0.0.1:8080 進(jìn)行壓力測(cè)試wrk -t12 -c400 -d30s http://127.0.0.1:8080/index.htmlCopy
這里是測(cè)試結(jié)果Running 30s test @ http://127.0.0.1:8080/index.html ?12 threads and 400 connections ?Thread Stats ? Avg ? ? ?Stdev ? ? Max ? +/- Stdev ? ?Latency ? 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.33MB1. 安裝
關(guān)于 OS X 與 Windows 的安裝可以參考 Wrk 官方 WIKI 進(jìn)行操作,本文主要講解一下 CentOS 7.x 下如果進(jìn)行編譯。
Copy
sudo yum groupinstall 'Development Tools' sudo yum install -y openssl-devel git git clone https://github.com/wg/wrk.git wrk cd wrk make編譯之后,你會(huì)得到如下結(jié)果:
可以看到生成了一個(gè) wrk 的可執(zhí)行文件,你可以將其添加到環(huán)境變量的 PATH 當(dāng)中,這里就不再贅述,我們等會(huì)兒使用的時(shí)候直接 ./wrk 使用。
2. 命令說(shuō)明
Copy
./wrk -H "Authorization: Bearer TokenValue" -t 2 -c 50 -d 10s --latency --timeout 1s "http://"上面的命令就是一個(gè)典型的壓力測(cè)試命令,關(guān)于參數(shù)的含義請(qǐng)看下表。
| -c | 與 HTTP 保持連接的連接數(shù),最終每個(gè)線程能夠處理的為 連接數(shù)/線程數(shù)。 | -c 50 |
| -d | 指定壓力測(cè)試的時(shí)間有多長(zhǎng)。 | -d 10s,其他單位有 2s,2m,2h 如果不帶單位的話,默認(rèn)為秒。 |
| -t | 壓力測(cè)試時(shí)所使用的線程數(shù)目,最好為你 CPU 核心的數(shù)量。 | -t 4 |
| -s | 指定要執(zhí)行的 Lua 腳本 | -s ./post.lua |
| -H | 執(zhí)行請(qǐng)求的時(shí)候所附帶的 Header 組。 | -H "User-Agent: wrk" |
| --latency | 打印詳細(xì)的統(tǒng)計(jì)信息。 | --latency |
| --timeout | 每次請(qǐng)求所返回響應(yīng)體的時(shí)間,如果超過(guò)了配置的時(shí)間,則視為請(qǐng)求超時(shí)。 | --timeout 1s |
3. 開始?jí)毫y(cè)試
執(zhí)行了上述代碼之后我們可以看到很直觀的信息,第一個(gè)就是 20s 的時(shí)間內(nèi)完成了 2887 次請(qǐng)求,一共接受到了 2.46MB 的數(shù)據(jù)。在 Socket errors 里面我們可以看到有 35 個(gè)請(qǐng)求產(chǎn)生了超時(shí)的情況,每秒執(zhí)行的請(qǐng)求大概為 144.20 個(gè),每秒的數(shù)據(jù)傳輸大概為 125.75 KB。
除此之外,還說(shuō)明了平均每次請(qǐng)求所消耗的時(shí)間為 338.44 ms,最極端的情況為 994.27ms。
4. LUA 腳本
在第三節(jié)我們可以看到一些標(biāo)準(zhǔn)的 GET 請(qǐng)求我們可以直接通過(guò)指定命令來(lái)進(jìn)行測(cè)試,即便該接口有授權(quán)驗(yàn)證,我們可以通過(guò)?-H?參數(shù)來(lái)指定?Authorization?頭來(lái)實(shí)現(xiàn)權(quán)限驗(yàn)證。
但是針對(duì)一些復(fù)雜的情況,我們就需要編寫 LUA 腳本來(lái)實(shí)現(xiàn)壓力測(cè)試了。
官方編寫了很多的 LUA 腳本 DEMO ,存放在 GitHub 上面,其地址為:https://github.com/wg/wrk/tree/master/scripts。
這里我們以實(shí)現(xiàn) POST 請(qǐng)求為例:
Copy
wrk.method = "POST"wrk.body ? = '{"username":"admin","password":"123qwe","rememberClient":true}'wrk.headers["Content-Type"] = "application/json"這里我們的接口地址更改了一下,改變成了 Login 接口,該接口需要傳入用戶名與密碼,并且其 Method 為 POST。
將上述 LUA 腳本保存為 post.lua 文件,然后通過(guò)?-s?參數(shù)指定 LUA 腳本的路徑并執(zhí)行。
5. LUA 腳本相關(guān)詳解
WRK 中執(zhí)行 HTTP 請(qǐng)求的時(shí)候,調(diào)用 Lua 分為 3 個(gè)階段,setup、running、done,每個(gè) WRK 線程中都有獨(dú)立的腳本環(huán)境。
5.1 WRK 的全局屬性
Copy
wrk = { ?scheme ?= "http", ?host ? ?= "localhost", ?port ? ?= nil, ?method ?= "GET", ?path ? ?= "/", ?headers = {}, ?body ? ?= nil, ?thread ?= <userdata>, }5.2 WRK 的全局方法
Copy
-- 生成整個(gè)request的string,例如:返回-- GET / HTTP/1.1-- Host: tool.lufunction wrk.format(method, path, headers, body)-- 獲取域名的IP和端口,返回table,例如:返回 `{127.0.0.1:80}`function wrk.lookup(host, service)-- 判斷addr是否能連接,例如:`127.0.0.1:80`,返回 true 或 falsefunction wrk.connect(addr)5.3 Setup 階段
setup()?方法是在線程創(chuàng)建之后,啟動(dòng)之前。
Copy
function setup(thread)-- thread提供了1個(gè)屬性,3個(gè)方法-- thread.addr 設(shè)置請(qǐng)求需要打到的ip-- thread:get(name) 獲取線程全局變量-- thread:set(name, value) 設(shè)置線程全局變量-- thread:stop() 終止線程5.4 Running 階段
Copy
function init(args)-- 每個(gè)線程僅調(diào)用1次,args 用于獲取命令行中傳入的參數(shù), 例如 --env=prefunction delay()-- 每個(gè)線程調(diào)用多次,發(fā)送下一個(gè)請(qǐng)求之前的延遲, 單位為msfunction request()-- 每個(gè)線程調(diào)用多次,返回http請(qǐng)求function response(status, headers, body)-- 每個(gè)線程調(diào)用多次,返回http響應(yīng)5.5 Done 階段
可以用于自定義結(jié)果報(bào)表,整個(gè)過(guò)程中只執(zhí)行一次。
Copy
function done(summary, latency, requests)latency.min ? ? ? ? ? ? ?-- minimum value seenlatency.max ? ? ? ? ? ? ?-- maximum value seenlatency.mean ? ? ? ? ? ? -- average value seenlatency.stdev ? ? ? ? ? ?-- standard deviationlatency:percentile(99.0) -- 99th percentile valuelatency(i) ? ? ? ? ? ? ? -- raw value and countsummary = { ?duration = N, ?-- run duration in microseconds ?requests = N, ?-- total completed requests ?bytes ? ?= N, ?-- total bytes received ?errors ? = { ? ?connect = N, -- total socket connection errors ? ?read ? ?= N, -- total socket read errors ? ?write ? = N, -- total socket write errors ? ?status ?= N, -- total HTTP status codes > 399 ? ?timeout = N ?-- total request timeouts ?} }而官方的?setup.lua?腳本則是重載這些方法并使用的一個(gè) DEMO:
Copy
-- example script that demonstrates use of setup() to pass-- data to and from the threadslocal counter = 1local threads = {}function setup(thread) ? thread:set("id", counter) ? table.insert(threads, thread) ? counter = counter + 1endfunction init(args) ? requests ?= 0 ? responses = 0 ? local msg = "thread %d created" ? print(msg:format(id))endfunction request() ? requests = requests + 1 ? return wrk.request()endfunction response(status, headers, body) ? responses = responses + 1endfunction done(summary, latency, requests) ? for index, thread in ipairs(threads) do ? ? ?local id ? ? ? ?= thread:get("id") ? ? ?local requests ?= thread:get("requests") ? ? ?local responses = thread:get("responses") ? ? ?local msg = "thread %d made %d requests and got %d responses" ? ? ?print(msg:format(id, requests, responses)) ? endend6. 參考資料
wrk中的lua腳本:https://type.so/linux/lua-script-in-wrk.html
http 性能測(cè)試 wrk使用教程:https://juejin.im/post/5a59e74f5188257353008fea
原文地址:https://www.cnblogs.com/myzony/p/9798116.html
.NET社區(qū)新聞,深度好文,歡迎訪問(wèn)公眾號(hào)文章匯總 http://www.csharpkit.com
總結(jié)
以上是生活随笔為你收集整理的使用 WRK 压力测试工具对 ASP.NET Core 的接口进行压力测试的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 微服务之:从零搭建ocelot网关和co
- 下一篇: 你准备好了在云中工作吗?