http 性能測試 wrk使用教程

wrk是一個http的壓測工具,底層封裝了epoll(linux)和kqueue(bsd),因此性能特別好html

安裝

Unbuntu/Debian下的安裝

sudo apt-get install build-essential libssl-dev git -y
git clone https://github.com/wg/wrk.git wrk
cd wrk
make
# 把生成的wrk移到一個PATH目錄下面, 好比
sudo cp wrk /usr/local/bin
複製代碼

CentOs/RedHat/Fedora

sudo yum groupinstall 'Development Tools'
sudo yum install openssl-devel
sudo yum install git
git clone https://github.com/wg/wrk.git wrk
cd wrk
make
# 把生成的wrk移到一個PATH目錄下面, 好比
sudo cp wrk /usr/local/bin
複製代碼

基本壓測

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

使用12個線程運行30秒, 400個http併發git

命令行選項

-c, --connections: 總的http併發數

-d, --duration:    持續壓測時間, 好比: 2s, 2m, 2h

-t, --threads:     總線程數

-s, --script:      luajit腳本,使用方法往下看

-H, --header:      添加http header, 好比. "User-Agent: wrk"

    --latency:     在控制檯打印出延遲統計狀況

    --timeout:     http超時時間
複製代碼

lua腳本壓測

在基本壓測中, 每次發送的請求都是同樣的,不少時候咱們壓測的請求體是每一個請求都不同, 這時候就要寫lua基原本壓測github

使用POST方法壓測

wrk.method = "POST"
wrk.body   = "foo=bar&baz=quux"
wrk.headers["Content-Type"] = "application/x-www-form-urlencoded"
複製代碼
wrk -t2 -d30s -c1k -s xxx.lua http://192.168.17.1/
複製代碼

每一個request的參數都不同

request = function()
   uid = math.random(1, 10000000)
   path = "/test?uid=" .. uid
   return wrk.format(nil, path)
end
複製代碼

解釋一下wrk.format這個函數json

wrk.format這個函數的做用,根據參數和全局變量wrk生成一個http請求
函數簽名: function wrk.format(method, path, headers, body)
method:http方法,好比GET/POST等
path: url上的路徑(含函數)
headers: http header
body: http body
複製代碼

每一個線程先登陸而後壓測

token = nil
path  = "/authenticate"

request = function()
   return wrk.format("GET", path)
end

response = function(status, headers, body)
   if not token and status == 200 then
      token = headers["X-Token"]
      path  = "/resource"
      wrk.headers["X-Token"] = token
   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
複製代碼

若運行的時候報錯找不到cjson, 能夠安裝 luarocks install lua-cjsonbash

wrk lua腳本說明

wrk 壓測腳本有3個生命週期, 分別是 啓動階段,運行階段和結束階段,每一個線程都有本身的lua運行環境 併發

image

啓動階段

function setup(thread)
在腳本文件中實現setup方法,wrk就會在測試線程已經初始化但尚未啓動的時候調用該方法。wrk會爲每個測試線程調用一次setup方法,並傳入表明測試線程的對象thread做爲參數。setup方法中可操做該thread對象,獲取信息、存儲信息、甚相當閉該線程。
-- thread提供了1個屬性,3個方法
-- thread.addr 設置請求須要打到的ip
-- thread:get(name) 獲取線程全局變量
-- thread:set(name, value) 設置線程全局變量
-- thread:stop() 終止線程
複製代碼

運行階段

function init(args)
-- 每一個線程僅調用1次,args 用於獲取命令行中傳入的參數, 例如 --env=pre

function delay()
-- 每次請求調用1次,發送下一個請求以前的延遲, 單位爲ms

function request()
-- 每次請求調用1次,返回http請求

function response(status, headers, body)
-- 每次請求調用1次,返回http響應

複製代碼

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

結束階段

function done(summary, latency, requests)


latency.min              -- minimum value seen
latency.max              -- maximum value seen
latency.mean             -- average value seen
latency.stdev            -- standard deviation
latency:percentile(99.0) -- 99th percentile value
latency(i)               -- raw value and count

summary = {
  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
  }
}
複製代碼

該方法在整個測試過程當中只會調用一次,可從參數給定的對象中,獲取壓測結果,生成定製化的測試報告。dom

線程變量

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

-- 生成整個request的string,例如:返回
-- GET / HTTP/1.1
-- Host: tool.lu
function wrk.format(method, path, headers, body)
-- method: http方法, 如GET/POST/DELETE 等
-- path:   url的路徑, 如 /index, /index?a=b&c=d
-- headers: 一個header的table
-- body:    一個http body, 字符串類型

-- 獲取域名的IP和端口,返回table,例如:返回 `{127.0.0.1:80}`
function wrk.lookup(host, service)
-- host:一個主機名或者地址串(IPv4的點分十進制串或者IPv6的16進制串)
-- service:服務名能夠是十進制的端口號,也能夠是已定義的服務名稱,如ftp、http等


-- 判斷addr是否能鏈接,例如:`127.0.0.1:80`,返回 truefalse
function wrk.connect(addr)
複製代碼
相關文章
相關標籤/搜索