加特林-动态设置请求名称

时间:2018-06-19 11:52:08

标签: scala gatling

我可以动态定义一个rquest名称吗?我用http(s"/product${prID}"进行了尝试,但出现错误(找不到可变的prID):

    val searchByProductIdTask = exec(http(s"/product${prID}")
        .post(appURL + "/api/product/search")
        .headers(jsonHeader)
        .body(StringBody(my_string_body)).asJSON
        .check(status.is(200), responseTimeInMillis.lessThan("${expectedResponseTime}"))
    ).pause(5)

更新:

这里是整个代码:

import scala.concurrent.duration._
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._

import java.time.Instant

class Product_Szenario extends Simulation {

val scenarioName = "Test_Product"

val baseURL="My_Base_URL"

val appURL="My_URL"

val httpProtocol = http
    .baseURL(baseURL)
    .acceptEncodingHeader("gzip, deflate")
    .acceptLanguageHeader("de,en-US;q=0.7,en;q=0.3")
    .userAgentHeader("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0")       
    .connectionHeader("keep-alive")
    .disableWarmUp
    .disableCaching

val jsonHeader = Map(
    "Accept" -> "*/*",
    "Content-Type" -> "application/json",
    "X-Requested-With" -> "XMLHttpRequest")

object Product {

    val searchByProductIdTask = exec(http(s"/product${prID}")
            .post(appURL + "/api/product/search")
            .headers(jsonHeader)
            .body(StringBody("""{"prID":"${prID}"}""")).asJSON
            .check(status.is(200), responseTimeInMillis.lessThan("${expectedResponseTime}"))
        )
}

object Szenario{

    val products = csv("product.csv").records
    val filialFeeder = csv("filial.csv").circular //50%

    val start= exec(repeat(products.size, "n"){
        feed(products.queue)
        .feed(filialFeeder)
        .exec(Product.searchByProductIdTask)
        .pause(5)
    })
}

val scnProduct = scenario(scenarioName)
    .exec(
        session =>{ 
            session.set("appUrl",baseURL + appURL + "/")
            .set("expectedResponseTime", 20000) // set to time you want check fails - find long time responses
        }
    ).exec(
        Szenario.start
    )

setUp(
    scnProduct.inject(atOnceUsers(1))//, First Scenario
).protocols(httpProtocol)

}

3 个答案:

答案 0 :(得分:1)

使用字符串插值,您错过了倒数第二行的s"${expectedResponseTime}"

val searchByProductIdTask = exec(http(s"/product${prID}")
        .post(appURL + "/api/product/search")
        .headers(jsonHeader)
        .body(StringBody(my_string_body)).asJSON
        .check(status.is(200), responseTimeInMillis.lessThan(s"${expectedResponseTime}"))
    ).pause(5)

答案 1 :(得分:0)

我明白了

        val searchByProductIdTask = exec(session => session.set("request_name", s"/product${session("prID").as[String]}"))
        .exec(http("${request_name}")
        .post(appURL + "/api/product/search")
        .headers(jsonHeader)
        .body(StringBody("""{"prID":"${prID}"}""")).asJSON
        .check(status.is(200), responseTimeInMillis.lessThan("${expectedResponseTime}"))
    )

答案 2 :(得分:0)

您可以摆脱以摆脱s而成为-

http("/product${prID}")

通过使用字符串插值,编译器正在查找名为prID的Scala字符串变量,但是prID是Galing会话属性,因此您可以使用Gatling EL将其带入请求名称。