scala - 如何在加特林中的 StringBody() 中给出随机字符串?

标签 scala performance-testing gatling

我是加特林工具的新手。

我想在 StringBody() 方法中给出一个随机字符串。 例如,对于 100 个请求,将传递 100 个不同的字符串。

这是我的示例代码:

import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._
class LoadTest extends Simulation {

  private val csvFeeder = csv("data.csv").random
  val httpConf = http
    .baseURL("http://localhost:5000/sample")   
    .acceptHeader("text/html,application/xhtml+xml,application/json,application/xml;q=0.9,*/*;q=0.8")

  val scn = scenario("load test")
    .feed(csvFeeder)
    .exec(http("request")
    .post("http://localhost:5000/sample")
    .body(StringBody(s"""{"inputData": ${csvFeeder} }""")).asJSON
  setUp(
    scn.inject(
      constantUsersPerSec(50)
      during (10 seconds) randomized).protocols(httpConf))
}

但是 CSV 文件中的所有字符串都被传递了。

我的代码有什么问题? 如何解决?

最佳答案

我会这样做:

import scala.util.Random

    var randomString = Iterator.continually(Map("randstring" -> ( Random.alphanumeric.take(35).mkString )))// length of the random string is 35 chars here

    val scn = scenario("load test")
        .feed(randomString)
        .exec(http("request")
        .post("http://localhost:5000/sample")
        .body(StringBody("""{"inputData": ${randstring} }""")).asJSON
      setUp(
        scn.inject(
          constantUsersPerSec(50)
          during (10 seconds) randomized).protocols(httpConf))
    }

还在代码中将 .body(StringBody(s"""{"inputData": ${csvFeeder} }""")).asJSON 更改为 .body(StringBody ("""{"inputData": ${csv_header_coloumn_name} }""")).asJSON //csv_header_coloumn_name > 是您要替换的 csv 文件中的字段。

关于scala - 如何在加特林中的 StringBody() 中给出随机字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45278535/

相关文章:

python - Spark : Dangers of using Python

scala - 宇宙是什么意思?

java - This 关键字与 scala 和匿名函数/类一起使用

java - JMeter 中的第一个 HTTP 请求花费了很长时间

c++ - Clang 为直觉上应该等效的表达式提供非常不同的性能

scala - 如何检查 Gatting/Scala 中是否存在属性

Scala - 获取函数名称

ruby - Rails 3.0.12 性能测试问题

testing - 以编程方式执行 Gatling 测试

testing - 在 1 小时内对 50 个用户进行 Gatling 用户注入(inject),每 5 分钟添加 10 个用户