压测: wrk压测的lua脚本使用随机列表与字符串参数

🍎 问chatgpt的结果

✅ 只随机school

问:帮我写一个lua脚本,功能是使用wrk压测一个http接口,这个http接口使用post请求,并且body的参数userList是一个列表,另外一个body参数school帮我用随机字符串替代

答:当然可以!以下是一个示例的Lua脚本,使用wrk工具来进行HTTP接口的压测。该脚本使用POST请求,其中userList参数为一个列表,school参数使用随机字符串替代。

wrk.method = "POST"
wrk.body = '{"userList": [1, 2, 3], "school": "' .. randomString(10) .. '"}'
wrk.headers["Content-Type"] = "application/json"

function randomString(length)
  local charset = {}
  for i = 48, 57 do table.insert(charset, string.char(i)) end -- 0-9
  for i = 65, 90 do table.insert(charset, string.char(i)) end -- A-Z
  for i = 97, 122 do table.insert(charset, string.char(i)) end -- a-z
  math.randomseed(os.time())
  local str = ""
  for _ = 1, length do
    str = str .. charset[math.random(1, #charset)]
  end
  return str
end

上述脚本中,wrk.method被设置为"POST",wrk.body是一个JSON字符串,其中userList参数是一个包含三个元素的列表,school参数通过randomString函数生成一个长度为10的随机字符串。wrk.headers用于设置请求的Content-Type为"application/json"。

请注意,上述脚本中使用了randomString函数来生成随机字符串,其中使用了随机数种子math.randomseed(os.time())。这是为了确保每次运行脚本时都会生成不同的随机字符串。

✅ 用户列表也随机

问:上面的lua脚本还有一个问题 就是 userList中的参数我也想用随机的int类型数据代替 帮我完善一下 谢谢

答:当然,请见下面更新后的Lua脚本,其中userList参数将被随机生成的整数列表替代:

wrk.method = "POST"
wrk.body = '{"userList": ' .. generateRandomIntList(3) .. ', "school": "' .. randomString(10) .. '"}'
wrk.headers["Content-Type"] = "application/json"

function generateRandomIntList(length)
  math.randomseed(os.time())
  local list = {}
  for _ = 1, length do
    table.insert(list, math.random(1, 100))
  end
  return table.concat(list, ', ')
end

function randomString(length)
  local charset = {}
  for i = 48, 57 do table.insert(charset, string.char(i)) end -- 0-9
  for i = 65, 90 do table.insert(charset, string.char(i)) end -- A-Z
  for i = 97, 122 do table.insert(charset, string.char(i)) end -- a-z
  math.randomseed(os.time())
  local str = ""
  for _ = 1, length do
    str = str .. charset[math.random(1, #charset)]
  end
  return str
end

在上述脚本中,我添加了一个名为generateRandomIntList的函数,用于生成随机整数列表。在该函数中,我使用math.random(1, 100)来生成1到100之间的随机整数,并将其添加到列表中。最后,使用table.concat将列表中的元素连接为一个字符串,并将其作为userList参数的值插入到请求体中。

请注意,为了确保每次运行脚本时都会生成不同的随机数和随机字符串,我在两个函数中均使用了math.randomseed(os.time())来设置随机数种子。

✅ 用户列表从固定的列表中随机获取

问:非常好 还差一个问题了,userList这个参数如果使用随机的int数据的话会一直无法命中数据库,我想从一个长度为50的列表中随机获取3个用户id当作userList的参数 帮我完善一下lua脚本 谢谢

答:当然,请见下面更新后的Lua脚本,其中userList参数将从长度为50的列表中随机获取3个用户ID:

wrk.method = "POST"
wrk.body = '{"userList": ' .. generateRandomUserList() .. ', "school": "' .. randomString(10) .. '"}'
wrk.headers["Content-Type"] = "application/json"

function generateRandomUserList()
  local userList = {}
  local userIds = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
                   21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
                   41, 42, 43, 44, 45, 46, 47, 48, 49, 50}
  math.randomseed(os.time())
  for _ = 1, 3 do
    local index = math.random(1, #userIds)
    table.insert(userList, userIds[index])
    table.remove(userIds, index)
  end
  return table.concat(userList, ', ')
end

function randomString(length)
  local charset = {}
  for i = 48, 57 do table.insert(charset, string.char(i)) end -- 0-9
  for i = 65, 90 do table.insert(charset, string.char(i)) end -- A-Z
  for i = 97, 122 do table.insert(charset, string.char(i)) end -- a-z
  math.randomseed(os.time())
  local str = ""
  for _ = 1, length do
    str = str .. charset[math.random(1, #charset)]
  end
  return str
end

在上述脚本中,我添加了一个名为generateRandomUserList的函数。该函数首先定义了一个长度为50的userIds列表,其中包含了50个用户ID。然后,在每次循环中,我使用math.randomuserIds列表中随机选择一个用户ID,并将其插入到userList列表中。之后,从userIds列表中移除已选择的用户ID,确保不会重复选择。最后,使用table.concatuserList列表中的元素连接为一个字符串,并将其作为userList参数的值插入到请求体中。

请注意,为了确保每次运行脚本时都会生成不同的随机数和随机字符串,我在两个函数中均使用了math.randomseed(os.time())来设置随机数种子。

✅❗️完整版:userList与school都从池中随机获取

问:ok 太棒了 school这个参数 我现在也想从一个长度为10的字符串列表中随机获取一个 userList这个参数还是从长度为50的列表中随机获取3个int数据

答:好的,以下是更新后的Lua脚本,其中school参数将从长度为10的字符串列表中随机获取一个,而userList参数仍然从长度为50的整数列表中随机获取3个:

wrk.method = "POST"
wrk.body = '{"userList": ' .. generateRandomUserList() .. ', "school": "' .. generateRandomSchool() .. '"}'
wrk.headers["Content-Type"] = "application/json"

function generateRandomUserList()
  local userList = {}
  local userIds = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
                   21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
                   41, 42, 43, 44, 45, 46, 47, 48, 49, 50}
  math.randomseed(os.time())
  for _ = 1, 3 do
    local index = math.random(1, #userIds)
    table.insert(userList, userIds[index])
    table.remove(userIds, index)
  end
  return table.concat(userList, ', ')
end

function generateRandomSchool()
  local schools = {"School A", "School B", "School C", "School D", "School E", "School F", "School G", "School H", "School I", "School J"}
  math.randomseed(os.time())
  local index = math.random(1, #schools)
  return schools[index]
end

在上述脚本中,我添加了一个名为generateRandomSchool的函数,用于从长度为10的字符串列表中随机选择一个学校名称。在该函数中,我定义了一个包含10个学校名称的schools列表。然后,使用math.randomschools列表中随机选择一个索引,并返回对应的学校名称作为school参数的值。

请注意,为了确保每次运行脚本时都会生成不同的随机数和随机字符串,我在两个函数中均使用了math.randomseed(os.time())来设置随机数种子。

posted on 2023-06-14 11:19  江湖乄夜雨  阅读(396)  评论(0编辑  收藏  举报