scala - Gatling exec 与会话
问题描述
我需要在 Gatling 中提出请求,我可以在其中访问会话项目(没有表达式语言)。我需要这样做,因为我想将数据注入ByteArrayBody
来自 csv 馈送器的请求中。为了演示我的问题,我有一个小例子(没有会话的实际需要)。
以下场景运行良好:
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
使用会话信息(或至少来自馈线的数据)运行请求?我正在使用加特林 3.1.1
解决方案
在函数中构建您需要的任何内容并将结果放入会话中,然后在实际请求中引用该值
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)
}
推荐阅读
- ios - 升级 Swift Realm Braking Changes,我可以更改类名或如何解决冲突?
- javascript - 开玩笑地测试 jquery 选择器
- sql - 简单的 sql 合并
- ios - 使用嵌入式 webview 测试即将对 Google OAuth 进行的安全更改时没有错误
- laravel - strpos(): 参数 #1 ($haystack) 必须是字符串类型,给定数组
- c# - 在 Blazor 中使用 Blazored.fluentValidator 验证模型列表
- html - 顶部:设置为相对位置时,50% 不起作用
- python - 有没有比 PIL Image.rotate() 更有效的旋转图像的方法
- matrix - 从矩阵获取旋转,OpenMaya
- logging - 如何关闭 dbt 日志?