Gatling Rest API 测试 - 从 json 响应中检索一个值并将其添加到列表中,遍历列表
Gatling Rest API Testing - retrieve a value from json response and add it to the list, iterate through list
我是 Gatling 的新手,我正在尝试对几个 rest 调用进行性能测试。在我的场景中,我需要从第一次调用的 json 响应中提取一个值,并在循环几次后将这些值添加到列表中。再次循环几次并将值添加到列表中后,我想通过迭代列表中的值在下一次 rest 调用中重用每个值。任何人都可以建议如何实现这一点。我试过如下,
var datasetIdList = List.empty[String]
val datasetidsFeeder = datasetIdList.map(datasetId => Map("datasetId" -> datasetId)).iterator
def createData() = {
repeat(20){
feed("").exec(http("create dataset").post("/create/data").header("content-type", "application/json")
.body(StringBody("""{"name":"name"}"""))
.asJson.check(jsonPath("$.id").saveAs("userId"))))
.exec(session => { var usrid = session("userId").as[String].trim
datasetIdList:+= usrid session})
}}
def upload()= feed(datasetidsFeeder).exec(http("file upload").post("/compute-metaservice/datasets/${datasetId}/uploadFile")
.formUpload("File","./src/test/resources/data/File.csv")
.header("content-type","multipart/form-data")
.check(status is 200))
val scn = scenario("create data and upload").exec(createData()).exec(upload())
setUp(scn.inject(atOnceUsers(1))).protocols(httpConf)
}
我在尝试 运行 以上脚本时看到 ListFeeder 为空的异常。有人可以帮忙吗
更新代码:
class ParallelcallsSimulation extends Simulation{
var idNumbers = (1 to 50).iterator
val customFeeder = Iterator.continually(Map(
"name" -> ("test_gatling_"+ idNumbers.next())
))
val httpConf = http.baseUrl("http://localhost:8080")
.header("Authorization","Bearer 6a4aee03-9172-4e31-a784-39dea65e9063")
def createDatasetsAndUpload() = {
repeat(3) {
//create dataset
feed(customFeeder).exec(http("create data").post("/create/data").header("content-type", "application/json")
.body(StringBody("""{ "name": "${name}","description": "create data and upload file"}"""))
.asJson.check(jsonPath("$.id").saveAs("userId")))
.exec(session => {
val name = session("name").asOption[String]
println(name.getOrElse("COULD NOT FIND NAME"))
val userId = session("userId").as[String].trim
println("%%%%% User ID ====>"+userId)
val datasetIdList = session("datasetIdList").asOption[List[_]].getOrElse(Nil)
session.set("datasetIdList", userId :: datasetIdList)
})
}
}
// File Upload
def fileUpload() = foreach("${datasetIdList}","datasetId"){
exec(http("file upload").post("/uploadFile")
.formUpload("File","./src/test/resources/data/File.csv")
.header("content-type","multipart/form-data")
.check(status is 200))
}
def getDataSetId() = foreach("${datasetIdList}","datasetId"){
exec(http("get datasetId")
.get("/get/data/${datasetId}")
.header("content-type","application/json")
.asJson.check(jsonPath("$.dlp.dlp_job_status").optional
.saveAs("dlpJobStatus")).check(status is 200)
).exec(session => {
val datastId = session("datasetId").asOption[String]
println("request for datasetId >>>>>>>>"+datastId.getOrElse("datasetId not found"))
val jobStatus = session("dlpJobStatus").asOption[String]
println("JOB STATUS:::>>>>>>>>>>"+jobStatus.getOrElse("Dlp Job Status not Found"))
println("Time: >>>>>>"+System.currentTimeMillis())
session
}).pause(10)
}
val scn1 = scenario("create multiple datasets and upload").exec(createDatasetsAndUpload()).exec(fileUpload())
val scn2 = scenario("get datasetId").pause(100).exec(getDataSetId())
setUp(scn1.inject(atOnceUsers(1)),scn2.inject(atOnceUsers(1))).protocols(httpConf)
}
当我尝试执行上面的脚本时,我看到以下错误
[错误] i.g.c.s.LoopBlock$ - 条件评估崩溃并显示消息“未定义名为 'datasetIdList' 的属性”,退出循环
var datasetIdList = List.empty[String]
定义一个指向不可变列表的可变变量。
val datasetidsFeeder = datasetIdList.map(datasetId => Map("datasetId" -> datasetId)).iterator
使用不可变列表。对 datasetIdList
的进一步更改与 datasetidsFeeder
.
无关
用你的虚拟用户改变一个全局变量通常不是一个好主意。
您可以将该值保存到用户的 session 中。
在exec块中,可以这样写:
val userId = session("userId").as[String].trim
val datasetIdList = session("datasetIdList").asOption[List[_]].getOrElse(Nil)
session.set("datasetIdList", userId :: datasetIdList)
然后您可以使用 foreach 将它们全部迭代,而无需使用进纸器。
foreach("${datasetIdList}", "datasetId") {
exec(http("file upload")
...
}
你应该在你的问题上做更多的工作。
- 您的代码没有语法高亮,并且格式不正确。
- 您说“我看到 ListFeeder 为空的异常”,但在任何地方都看不到“ListFeeder”字样。
- 您应该 post 错误消息,以便更容易看出哪里出了问题。
在链接的文档中,有一个警告。引用如下:
Session
instances are immutable!
Why is that so? Because Sessions are messages that are dealt with in a multi-threaded concurrent way, so immutability is the best way to deal with state without relying on synchronization and blocking.
A very common pitfall is to forget that set
and setAll
actually return new instances.
这就是更新问题中的代码不更新列表的原因。
session => {
...
session.set("datasetIdList", userId :: datasetIdList)
println("%%%% List =====>>>" + datasetIdList.toString())
session
}
更新的会话被简单地丢弃。并在匿名函数中返回原始会话。
我是 Gatling 的新手,我正在尝试对几个 rest 调用进行性能测试。在我的场景中,我需要从第一次调用的 json 响应中提取一个值,并在循环几次后将这些值添加到列表中。再次循环几次并将值添加到列表中后,我想通过迭代列表中的值在下一次 rest 调用中重用每个值。任何人都可以建议如何实现这一点。我试过如下,
var datasetIdList = List.empty[String]
val datasetidsFeeder = datasetIdList.map(datasetId => Map("datasetId" -> datasetId)).iterator
def createData() = {
repeat(20){
feed("").exec(http("create dataset").post("/create/data").header("content-type", "application/json")
.body(StringBody("""{"name":"name"}"""))
.asJson.check(jsonPath("$.id").saveAs("userId"))))
.exec(session => { var usrid = session("userId").as[String].trim
datasetIdList:+= usrid session})
}}
def upload()= feed(datasetidsFeeder).exec(http("file upload").post("/compute-metaservice/datasets/${datasetId}/uploadFile")
.formUpload("File","./src/test/resources/data/File.csv")
.header("content-type","multipart/form-data")
.check(status is 200))
val scn = scenario("create data and upload").exec(createData()).exec(upload())
setUp(scn.inject(atOnceUsers(1))).protocols(httpConf)
}
我在尝试 运行 以上脚本时看到 ListFeeder 为空的异常。有人可以帮忙吗
更新代码:
class ParallelcallsSimulation extends Simulation{ var idNumbers = (1 to 50).iterator val customFeeder = Iterator.continually(Map( "name" -> ("test_gatling_"+ idNumbers.next()) )) val httpConf = http.baseUrl("http://localhost:8080") .header("Authorization","Bearer 6a4aee03-9172-4e31-a784-39dea65e9063") def createDatasetsAndUpload() = { repeat(3) { //create dataset feed(customFeeder).exec(http("create data").post("/create/data").header("content-type", "application/json") .body(StringBody("""{ "name": "${name}","description": "create data and upload file"}""")) .asJson.check(jsonPath("$.id").saveAs("userId"))) .exec(session => { val name = session("name").asOption[String] println(name.getOrElse("COULD NOT FIND NAME")) val userId = session("userId").as[String].trim println("%%%%% User ID ====>"+userId) val datasetIdList = session("datasetIdList").asOption[List[_]].getOrElse(Nil) session.set("datasetIdList", userId :: datasetIdList) }) } } // File Upload def fileUpload() = foreach("${datasetIdList}","datasetId"){ exec(http("file upload").post("/uploadFile") .formUpload("File","./src/test/resources/data/File.csv") .header("content-type","multipart/form-data") .check(status is 200)) } def getDataSetId() = foreach("${datasetIdList}","datasetId"){ exec(http("get datasetId") .get("/get/data/${datasetId}") .header("content-type","application/json") .asJson.check(jsonPath("$.dlp.dlp_job_status").optional .saveAs("dlpJobStatus")).check(status is 200) ).exec(session => { val datastId = session("datasetId").asOption[String] println("request for datasetId >>>>>>>>"+datastId.getOrElse("datasetId not found")) val jobStatus = session("dlpJobStatus").asOption[String] println("JOB STATUS:::>>>>>>>>>>"+jobStatus.getOrElse("Dlp Job Status not Found")) println("Time: >>>>>>"+System.currentTimeMillis()) session }).pause(10) } val scn1 = scenario("create multiple datasets and upload").exec(createDatasetsAndUpload()).exec(fileUpload()) val scn2 = scenario("get datasetId").pause(100).exec(getDataSetId()) setUp(scn1.inject(atOnceUsers(1)),scn2.inject(atOnceUsers(1))).protocols(httpConf) }
当我尝试执行上面的脚本时,我看到以下错误 [错误] i.g.c.s.LoopBlock$ - 条件评估崩溃并显示消息“未定义名为 'datasetIdList' 的属性”,退出循环
var datasetIdList = List.empty[String]
定义一个指向不可变列表的可变变量。
val datasetidsFeeder = datasetIdList.map(datasetId => Map("datasetId" -> datasetId)).iterator
使用不可变列表。对 datasetIdList
的进一步更改与 datasetidsFeeder
.
用你的虚拟用户改变一个全局变量通常不是一个好主意。 您可以将该值保存到用户的 session 中。
在exec块中,可以这样写:
val userId = session("userId").as[String].trim
val datasetIdList = session("datasetIdList").asOption[List[_]].getOrElse(Nil)
session.set("datasetIdList", userId :: datasetIdList)
然后您可以使用 foreach 将它们全部迭代,而无需使用进纸器。
foreach("${datasetIdList}", "datasetId") {
exec(http("file upload")
...
}
你应该在你的问题上做更多的工作。
- 您的代码没有语法高亮,并且格式不正确。
- 您说“我看到 ListFeeder 为空的异常”,但在任何地方都看不到“ListFeeder”字样。
- 您应该 post 错误消息,以便更容易看出哪里出了问题。
在链接的文档中,有一个警告。引用如下:
Session
instances are immutable!Why is that so? Because Sessions are messages that are dealt with in a multi-threaded concurrent way, so immutability is the best way to deal with state without relying on synchronization and blocking.
A very common pitfall is to forget that
set
andsetAll
actually return new instances.
这就是更新问题中的代码不更新列表的原因。
session => {
...
session.set("datasetIdList", userId :: datasetIdList)
println("%%%% List =====>>>" + datasetIdList.toString())
session
}
更新的会话被简单地丢弃。并在匿名函数中返回原始会话。