輕量級性能測試工具wrk - 使用(實戰篇)

 

一、發送POST請求例子

wrk壓力測試POST請求--以本地項目地址爲例:http://192.168.180.126

登錄接口:/api/user/login/

請求數據:

{
    "username":"admin",
    "password":"admin123456",
    "code":666
}

1.編寫lua腳本,填寫post的數據,如login.lua

wrk.method = "POST"
wrk.body = '{"username":"admin","password":"admin123456","code":666}'
wrk.headers["Content-Type"] = "application/json"

function request()
   return wrk.format("POST",nil,nil,body)
end

function response(status, headers, body)
   if status ~= 200 then
      print(body)
      wrk.thread:stop()
   end
end

2.執行wrk,開始壓力測試。

wrk -t 16 -c 100 -d 30s --latency --timeout 5s -s login.lua http://192.168.180.126/api/user/login/

壓測結果如下:

二、發送帶隨機參數的get請求例子

wrk壓力測試帶隨機參數的get請求--以本地項目地址爲例: http://192.168.160.30:8080

get接口:/academy/train/course/?id=

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

request = function()
num = math.random(1,10)
        path = "/academy/train/course/?id="..num
        return wrk.format("GET",path)
end

2.執行wrk,開始壓力測試。

wrk -t 16 -c 100 -d 30s --latency --timeout 5s -s test.lua http://192.168.160.30:8080

壓測結果如下:

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

wrk壓力測試添加參數txt文件的get請求--以本地項目地址爲例: http://192.168.160.30:8080

get接口:/academy/train/course/?id=

id.txt文件內容如下:

1
2
3
4
5

1.添加參數txt文件的get請求,則lua腳本如下:

idmap = {}
counter = 0
function init(args)
        for line in io.lines("id.txt") do
                print(line)
                idmap[counter] = line
                counter = counter + 1
        end
        counter = 0
end

request = fuction()
        local path = "/academy/train/course/?id="
        parms = idmap[counter%(table.getn(idmap)+1)]
        path = string.format(path,parms)
        counter = counter + 1
        return wrk.format(nil,path)
end

2.執行wrk,開始壓力測試。

wrk -t 16 -c 100 -d 30s --latency --timeout 5s -s id.lua http://192.168.160.30:8080

壓測結果如下:

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

wrk壓力測試添加參數txt文件的post請求--以本地項目地址爲例:http://192.168.180.126

登錄接口:/api/user/login/

login.txt文件內容如下:

admin

1.添加參數txt文件的post請求,則lua腳本如下:

loginmap = {}
counter = 0
function init(args)
        for line in io.lines("login.txt") do
                loginmap[counter] = line
                counter = counter + 1
        end
        counter = 0
end

request = function()
        local body1 = '{"username":"'
        local body2 = '","password":"admin123456","code":666}'
        parms = loginmap[counter%(table.getn(loginmap)+1)]
        path = "/api/user/login/"
        method = "POST"
        wrk.headers["Content-Type"] = "application/json"
        body = body1..parms..body2
        return wrk.format(method,path,wrk.headers,body)
end

2.執行wrk,開始壓力測試。

wrk -t 16 -c 100 -d 30s --latency --timeout 5s -s login_txt.lua http://192.168.180.126

壓測結果如下:

若參數txt中有轉義字符,可用如下方法處理:

parms = string.gsub(urimap[counter%(table.getn(urimap) + 1)],'\r','')

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

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

五、提交不同表單內容例子

wrk壓力測試添加參數txt文件的post請求--以本地項目地址爲例:http://192.168.180.126

登錄接口:/api/user/login/

1.提交不同表單內容的post請求,則lua腳本如下:

wrk.method = "POST"
wrk.body = ""
wrk.headers["Content-Type"] = "application/x-www-form-urlencoded"
wrk.path = "/api/user/login/"

-- 提交不同表單內容
local queries = {
        "username=admin&password=admin123456&code=666",
        "username=ZY&password=666666aa&code=666"
}

local i = 0
request = function()
        local body = wrk.format(method,path,headers,queries[i % #queries+1])
        i = i + 1
        return body
end

2.執行wrk,開始壓力測試。

wrk -t 16 -c 100 -d 30s --latency --timeout 5s -s queries.lua http://192.168.180.126

壓測結果如下:

六、訪問多個url例子

1.需要創建一個文件名爲paths.txt,裏面每行是一個要測試的url網址,paths.txt如下:

 https://www.baidu.com
 https://www.csdn.net

2.lua腳本如下:

#!/usr/bin/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() end
  return f ~= nil
end

function shuffle(paths)
  local j, k
  local n = #paths
  for i = 1, n do
    j, k = math.random(n), math.random(n)
    paths[j], paths[k] = paths[k], paths[j]
  end
  return paths
end

function non_empty_lines_from(file)
  if not file_exists(file) then return {} end
  lines = {}
  for line in io.lines(file) do
    if not (line == '') then
      lines[#lines + 1] = line
    end
  end
  return shuffle(lines)
end

paths = non_empty_lines_from("paths.txt")

if #paths <= 0 then
  print("multiplepaths: No paths found. You have to create a file paths.txt with one path per line")
  os.exit()
end

print("multiplepaths: Found " .. #paths .. " paths")

request = function()
    path = paths[counter]
    counter = counter + 1
    if counter > #paths then
      counter = 0
    end
    return wrk.format(nil, path)
end

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章