Gatling执行官,有会议

问题描述 投票:6回答:1

我需要在Gatling中提出一个请求,在这个请求中,我能够访问会话项(没有表达式语言)。我需要这样做,因为我想把数据注入到一个名为 "Gatling "的应用程序中。ByteArrayBody 的请求。为了证明我的问题,我有一个小例子(没有实际需要的会话)。

下面的场景运行良好。

val scnBase: ScenarioBuilder = scenario("Test scneario").repeat(1){
  exec(http("Http Test test").get("http://google.de/"))
}

但那个却不行(我得到了一个异常信息 There were no requests sent during the simulation, reports won't be generated):

val scnBase: ScenarioBuilder = scenario("Test scneario").repeat(1){
  exec(session => {
    http("Http Test test").get("http://google.de/")
    session
  })
}

我在IntelliJ中运行我的模拟(到目前为止运行良好),并在以下(此处最小化)模拟文件中运行。

package test.scala

import java.text.SimpleDateFormat
import java.util.Date

import io.gatling.core.Predef._
import io.gatling.core.body.ByteArrayBody
import io.gatling.core.structure.ScenarioBuilder
import io.gatling.http.Predef._
import io.gatling.http.protocol.HttpProtocolBuilder
import org.slf4j.LoggerFactory
import test.scala.TerminalTesterRequest.url
import test.scala.requests._
import test.scala.util.CharsetConverter

import scala.concurrent.duration._
import scala.language.postfixOps

class MySimulation extends Simulation {

  //base URL (actually this URL is different, but it's not important)
  val ecmsServerUri = "http://0.0.0.0"

  //base Protocol
  val httpProtocol: HttpProtocolBuilder = http
    .baseUrl(ecmsServerUri)
    .inferHtmlResources(BlackList(""".*\.js""", """.*\.css""", """.*\.gif""", """.*\.jpeg""", """.*\.jpg""", """.*\.ico""", """.*\.woff""", """.*\.(t|o)tf""", """.*\.png"""), WhiteList())
    .acceptHeader("*/*")
    .acceptEncodingHeader("gzip, deflate")
    .acceptLanguageHeader("en,en-US;q=0.7,de-DE;q=0.3")
    .userAgentHeader("Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 2.0.50727.8762)")

  val scnBase: ScenarioBuilder = scenario("Test scneario").repeat(1){
    exec(session => {
      http("Http Test test").get("http://google.de/")
      session
    })
  }

  setUp(
    scnBase.inject(constantUsersPerSec(1) during(1 seconds)).protocols(httpProtocol)
  ).maxDuration(5 minutes)
}

我怎样才能运行一个 exec 请求中包含会话的信息(或至少是来自馈线的数据)?我正在使用Gatling 3.1.1。

scala gatling
1个回答
0
投票

在一个函数中构建任何你需要的东西,并将结果放在session中,然后在实际请求中引用该值。

val feeder = csv("foo.csv")

scenario("Test scenario")
  .feed(feeder)
  .exec(buildPostData)
  .exec(http("Http Test test")
    .post(createApiURL)  
    .body(ByteArrayBody("${postData}"))
    .check(status.is(200))
  )

def buildPostData: Expression[Session] = session => {
  val postData: Array[Byte] = 
    ... // getting values from csv record: session("csvHeader").as[String]
  session.set("postData", postData)
}
© www.soinside.com 2019 - 2024. All rights reserved.