gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:51:34 25 4
gpt4 key购买 nike

我是加特林工具的新手。

我想在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com