如何在会话中提取向量?

时间:2017-03-28 15:35:51

标签: java scala gatling

我在会话中保存了向量,我想使用向量中的随机值,但不知道如何在会话中提取值。

错误:

  

'的HttpRequest-6'未能执行:Vector(437420,443940,443932,   437437,443981,443956,443973,443915,437445)命名为'termIds'不   不支持.random功能

  

在第二种情况下,它以这种方式传递get请求中的向量,http://someurl/api/thr/Vector(435854)/terms/Vector(437420,443940,   443932,4377437,443981,443956,443973,443915,437445)

而不是使用 http://someurl/api/thr/435854/terms/443973

::这是我的脚本::

class getTerm extends Simulation {


 val repeatCount = Integer.getInteger("repeatCount", 1).toInt
 val userCount = Integer.getInteger("userCount", 1).toInt
 val turl =  System.getProperty("turl", "some url")

 val httpProtocol = http
 .baseURL("http://" + turl)

 val headers_10 = Map("Content-Type" -> """application/json""")

 var thrIds = ""
 var termIds = ""


// Scenario - 1 
 val getTerms = scenario("Scn 1")
    .exec(http("list_of_term")
    .get("/api/abc")
    .headers(headers_10)
    .check(jsonPath("$[*].id")
    .findAll.saveAs("thrIds"))
 )

   .exec(http("get_all_terms")
     .get("""/api/thr/${thrIds.random()}/terms""")
     .headers(headers_10)
     .check(jsonPath("$[*].id")
     .findAll.saveAs("termIds"))
 )

  .exec(session => {
     thrIds = session("thrIds").as[Long].toString
     termIds = session("termIds").as[Long].toString
     println("***************************************")
     println("Session ====>>>> " + session)
     println("Ths ID ====>>>> " + thrIds)  
     println("Term ID ====>>>> " + termIds)  
     println("***************************************")
   session}       
 ) 

// Scenario - 2 
// Want to extract vectors here and pass its value into get call
 val getKnownTerms = scenario("Get Known Term")
    .exec(_.set("thrIds", thrIds))
    .exec(_.set("termIds", termIds))

    .repeat (repeatCount){
        exec(http("get_k_term")
       .get("""/api/thr/${thrIds}/terms/${termIds.random()}""")
    .headers(headers_10))
 }

 val scn = List(getTerms.inject(atOnceUsers(1)), getKnownTerms.inject(nothingFor(20 seconds), atOnceUsers(userCount)))
 setUp(scn).protocols(httpProtocol)

}

1 个答案:

答案 0 :(得分:1)

以下是可以帮助他人的解决方案。

class getTerm extends Simulation {

 val repeatCount = Integer.getInteger("repeatCount", 1).toInt
 val userCount = Integer.getInteger("userCount", 1).toInt
 val turl =  System.getProperty("turl", "some url")

 val httpProtocol = http
 .baseURL("http://" + turl)

 val headers_10 = Map("Content-Type" -> """application/json""")

// Change - 1 
var thrIds: Seq[String] = _   
var termIds: Seq[String] = _   

// Scenario - 1 
 val getTerms = scenario("Scn 1")
    .exec(http("list_of_term")
    .get("/api/abc")
    .headers(headers_10)
    .check(jsonPath("$[*].id")
    .findAll
    .transform { v => thrIds = v; v }
    .saveAs("thrIds"))
 )

   .exec(http("get_all_trms")
     .get("""/api/thr/${thrIds.random()}/terms""")
     .headers(headers_10)
     .check(jsonPath("$[*].id")
     .findAll
     .transform { v => termIds = v; v }
    .saveAs("termIds"))
 )

// Scenario - 2 
 val getKnownTerms = scenario("Get Known Term")
    .exec(_.set("thrIds", thrIds))
    .exec(_.set("termIds", termIds))

    .repeat (repeatCount){
        exec(http("get_k_term")
       .get("""/api/thr/${thrIds.random()}/terms/${termIds.random()}""")
    .headers(headers_10))
 }

 val scn = List(getTerms.inject(atOnceUsers(1)), getKnownTerms.inject(nothingFor(20 seconds), atOnceUsers(userCount)))
 setUp(scn).protocols(httpProtocol)

}