標籤:
轉載地址:http://www.tuicool.com/articles/IFjIJjU
wrk是一款現代化的http壓測工具,提供lua指令碼的功能可以滿足每個請求或部分請求的差異化。
wrk中執行http請求的時候,調用lua分為3個階段,setup,running,done,每個wrk線程中都有獨立的指令碼環境。
wrk的全域屬性
wrk = { scheme = "http", host = "localhost", port = nil, method = "GET", path = "/", headers = {}, body = nil, thread = <userdata>,}
wrk的全域方法
-- 產生整個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)
Setup階段
setup是線上程建立之後,啟動之前。
function setup(thread)-- thread提供了1個屬性,3個方法-- thread.addr 佈建要求需要打到的ip-- thread:get(name) 擷取線程全域變數-- thread:set(name, value) 設定線程全域變數-- thread:stop() 終止線程
Running階段
function init(args)-- 每個線程僅調用1次,args 用於擷取命令列中傳入的參數, 例如 --env=prefunction delay()-- 每個線程調用多次,發送下一個請求之前的延遲, 單位為msfunction request()-- 每個線程調用多次,返回http請求function response(status, headers, body)-- 每個線程調用多次,返回http響應
Done階段
可以用於自訂結果報表,整個過程中只執行一次
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 }}
例子表單的提交
wrk.method = "POST"wrk.body = "" -- 直接寫死,如果不需要請求資料的差異化wrk.headers["Content-Type"] = "application/x-www-form-urlencoded"-- 如果要實現每次都不一樣的表單內容local queries = { "language=php", "language=java", "language=lua"}local i = 0request = function() local body = wrk.format(nil, nil, nil, queries[i % #queries + 1]) i = i + 1 return bodyend
wrk中的lua指令碼(轉)