gatling——在一个场景中动态地在map中创建和添加键、值对,并将该Map传递给下一个场景

tzcvj98z  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(317)

我需要在一个场景中创建一个Map并动态添加键值对,然后将该Map传递到另一个场景,在该场景中我可以按键检索值。有人能告诉我怎么做吗。

def createData() = {

     feed(customFeeder).exec(http("create dataset").post("/datasets").header("content-type", "application/json")
       .body(StringBody("""{ "name": "${name}","description": "create dataset"}"""))
       .asJson.check(jsonPath("$.id").saveAs("userId")))
       .exec(session => {
         val name = session("name").asOption[String]
         println("Dataset name ::: "+name.getOrElse("COULD NOT FIND NAME"))
         val datasetId = session("userId").as[String].trim
         println("Dataset ID retrieved from createDataSet Response ::: "+ datasetId)
        val datasetIdList = session("datasetIdList").asOption[List[String]].getOrElse(Nil)
         println("Upload Start Time :::"+Calendar.getInstance().getTime)
    **// add above datasetId, upload start time to the map**
         session.set("datasetIdList", userId :: datasetIdList)
       })
   }

// File Upload for the datasets in the datasetIdList
 def fileUpload() = foreach("${datasetIdList}","datasetId"){
//   println("File Upload Start Time::::"+Calendar.getInstance().getTime+" for datasetId ::: ${datasetId}")
   exec(http("file upload").post("/compute-metaservice/datasets/${datasetId}/uploadFile")
     .formUpload("File","./src/test/resources/data/Scan_good.csv")
     .header("content-type","multipart/form-data")
     .check(status is 200).check(status.saveAs("uploadStatus")))
     .exec(session => {
     **// retrieve the upload time by datasetId from map above**
     session
   })
 }

val scn1 = scenario("create multiple datasets and upload").exec(createDatasetsAndUpload()).exec(fileUpload())
setUp(scn1.inject(atOnceUsers(3))).protocols(httpConf)
fdx2calv

fdx2calv1#

第一步:需要为存储会话定义变量

import scala.collection.mutable.{Map => MutableMap}

val sessionVariable: MutableMap[String, Any] = MutableMap()

然后我们创建两种方法,在不同的场景中保存和获取会话:

def passAnotherSession(currentSession: Session, transferVariable: MutableMap[String, Any]) = {
    currentSession.setAll(transferVariable)
  }

def saveSessionToVariable(currentSession: Session, transferVariable: MutableMap[String, Any]) = {
    val deleteSessionKeys = List("gatling.http.ssl.sslContexts", "gatling.http.cache.contentCache")
    transferVariable ++= currentSession.removeAll(deleteSessionKeys: _*).attributes
    currentSession
  }

并使用它:

val setUpScenario = scenario("Set-up scenario")
    .exec(...)
    .exec(saveSessionToVariable(_, sessionVariable))

val mainScenario = scenario("Main scenario")
    .exec(...)
    .exec(passAnotherSession(_, sessionVariable))

  setUp(
    setUpScenario.inject(atOnceUsers(1))
      .andThen(
        scn.inject(atOnceUsers(1))
      ).protocols(protocol)
  )

相关问题