wrk批量压测:一次压测多个http/https url

wrk是个很好的http/https压测工具,同时也支持通过lua脚本一次发多个url测试,详见github
https://github.com/timotta/wrk-scripts。但是这个脚本有一个bug,需要改一下,才能运行正常。修改后的代码如下:

//counter = 0
counter = 1

-- 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
     counter = 1
    end
    return wrk.format(nil, path)
end

因为lua数组下标是从1开始,上诉代码如果从0开始,则会发送很多空请求。
具体使用方式:
在wrk当前目录下创建一个paths.txt,将要压测的url保存在这个文件中,一行一个。注意paths.txt如果是dos格式,会报错,得改成unix格式。
wrk -c 100 -t 4 -d 30s -s wrk-scripts/multiplepaths.lua http://localhost

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值