首页 > 解决方案 > 生成身份验证令牌并在 Gatling 的整个会话中使用它

问题描述

我有一个场景,我正在点击 keyCloak 请求并将令牌用作其他 HTTP 请求中的标头。

我的问题是对于 100 个用户,它会生成 100 个令牌,因此我正在寻找仅命中一次 keyCloak 请求并且在整个性能运行过程中使用令牌的解决方案。

供参考的示例代码片段:

   scenario("CMS service")
   .exec(KeycloakToken.request(conf))
   .exec(getOffers.request(conf))
   .exec(offerById.request(conf))

标签: scalagatling

解决方案


使用馈线https://gatling.io/docs/current/session/feeder/

1) 创建馈线

val feeder = Iterator.continually(Map("token" -> "dG9rZW4="))

2) 将馈线添加到场景

 scenario("CMS service")
   .feed(feeder)
   .exec(...)

3) 通过 Gatling EL https://gatling.io/docs/current/session/expression_el传递值

在您的 http 请求中添加令牌

http(...)
    .post(...)
    .header("your token header", "${token}")

推荐阅读