简体   繁体   English

带有随机数量身体部位的加特林请求

[英]Gatling request with random number of body parts

I want to test a HTTP upload API that accepts a list of files in a single request. 我想测试一个HTTP上传API,该API在单个请求中接受文件列表。 I want to write a Gatling script that generates a request with a random number of body parts each time. 我想编写一个Gatling脚本,该脚本每次生成一个带有随机数量身体部位的请求。

This is what I have: 这就是我所拥有的:

feed(feeder)
  .exec(
    {
      var req = http("My request")
        .post("/${id}")
        .header("Content-Type", "multipart/mixed")

      1 to Random.nextInt(10) foreach {
        i => {
          req = req.bodyPart(
            ByteArrayBodyPart("file-put", session => randomByteArray(10 * 1024 + Random.nextInt(10 * 1024 * 1024)))
              .contentType("application/pdf")
              .fileName(session => s"/$i-UPLOAD-TEST.pdf")
          )
        }
      }
      req
    }
  )

private def randomByteArray(size: Int): Array[Byte] = {
    val bytes = new Array[Byte](size)
    Random.nextBytes(bytes)
    bytes
}

With every request the file sizes and contents are randomized, so the randomByteArray works fine. 对于每个请求,文件大小和内容都是随机的,因此randomByteArray可以正常工作。 But each time I get the same number of body parts. 但是每次我得到相同数量的身体部位。 I assume it's because the request "template" is generated at the start of the simulation, so the foreach loop runs only once and configures the number of body parts for all the future requests. 我认为这是因为请求“模板”是在模拟开始时生成的,因此foreach循环仅运行一次,并为所有将来的请求配置主体部分的数量。

How can I make the number of body parts random each time? 如何每次使身体部位的数量随机?

您必须事先构建每个分支(一个用于一个部分,一个用于2,等等),然后随机切换

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM