英文:
How to send http requests dynamically in Gatling?
问题
我试图将一些序列保存在静态变量中,然后针对序列的每个值进行http获取:
.exec { 会话 =>
居民 = 会话("residents").作为[String]
.拆分(",").成为序列
.映射(f => f.replaceAll("\"", ""))
会话
}
.对于(居民, "居民") {
执行(http("居民").获取("#{居民}"))
}
序列的每个元素都是一个http URL。
在Gatling中是否可以根据序列元素的数量动态发送http请求?我该如何做这件事,因为这段代码不符合我的预期。
英文:
I'm trying to save some seq in static variable and then make http get for each value of the seq:
.exec { session =>
residents = session("residents").as[String]
.split(",").toSeq
.map(f => f.replaceAll("\"", ""))
session
}
.foreach(residents, "resident") {
exec(http("resident").get("#{resident}"))
}
Each element of seq is http url.
Is it possible to send http requests dynamically in Gatling depends on count of seq elements? How can I do this beacuse this code doesn't work as I expect.
答案1
得分: 1
这不是 Gatling 的工作方式。
您的居民可变引用是:
- 全局的,因此在多个用户运行时会遇到竞态条件
- 在构建
foreach
操作时尚未填充
您已经从当前用户的会话中提取了 residents
,您必须保持在这个范围内:
.exec { session =>
val residentsSeq = session("residents").as[String]
.split(",").toSeq
.map(f => f.replaceAll("\"", ""))
// 用 Seq 替换原始的 "residents"
session.set("residents", residentsSeq)
}
// 基本上等同于
// foreach(session => session("residents").as[Seq[String]], "resident")
.foreach("#{residents}", "resident") {
exec(http("resident").get("#{resident}"))
}
英文:
That's not how Gatling works.
Your residents mutable reference is:
- global to your test, hence you'll face race conditions when running with multiple users
- not populated at the time you're building your
foreach
action
You've pulled residents
from the current user's Session
, you must stay in this scope:
.exec { session =>
val residentsSeq = session("residents").as[String]
.split(",").toSeq
.map(f => f.replaceAll("\"", ""))
// replace the original "residents" with a Seq
session.set("residents", residentsSeq)
}
// basically equivalent to
// foreach(session => session("residents").as[Seq[String]], "resident")
.foreach("#{residents}", "resident") {
exec(http("resident").get("#{resident}"))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论