【问题标题】:How to execute a scenario using data from the previous scenario?如何使用前一个场景中的数据执行一个场景?
【发布时间】:2016-05-29 10:21:33
【问题描述】:

我想执行两个场景,应该一个接一个地执行,第一个场景“产生”的数据应该用作第二个场景的基础。

因此,一个案例可能是信用卡清算等。第一个场景是在卡上授权/保留一定金额:

val auths = scenario("auths").during(durationInMinutes minutes) {
    feed(credentials)
        .feed(firstNames)
        .feed(lastNames)
        .feed(cards)
        .feed(amounts)
        .exec(http("send auth requests")
             .post(...)
             .check(...))}

第二个是从信用卡中获取/提取金额:

val caps = scenario("caps").during(durationInMinutes minutes) {
    feed(credentials)
        .feed(RESPONSE_IDS_FROM_PREVIOUS_SCENARIO)
        .exec(http("send auth requests")
            .post(...)
            .check(...))}    

我最初考虑在检查时使用 saveAs(...) 选项,但我发现保存的字段仅对给定会话有效。

所以基本上我想保留从 auths 场景中获得的 ID,并在 caps 场景中使用它们。

虽然我不能在一个场景中执行这两个步骤(saveAs 可以解决这个问题),因为我对这两个场景有不同的要求。

【问题讨论】:

    标签: stress-testing gatling


    【解决方案1】:

    引用文档:“目前我们的模拟是一个大的单一场景。所以首先让我们将其拆分为可组合的业务流程,类似于使用 Selenium 的 PageObject 模式。这样,您将能够轻松地重用某些部分和在不牺牲维护的情况下构建复杂的行为。”在gatling.io/Advanced Tutorial

    因此,您没有用于场景之间通信的内置机制 (AFAIK)。建议以这样的方式构建您的代码,以便您随后可以组合对 URI 的调用。在你的情况下(除了实现细节)你应该有这样的东西:

    val auths = feed(credentials)
        .feed(firstNames)
        .feed(lastNames)
        .feed(cards)
        .feed(amounts)
        .exec(http("send auth requests")
             .post(...)
             .check(...) // extract and store RESPONSE_ID to session
        )
    
    val caps = exec(http("send auth requests")
            .post(...) // use of RESPONSE_ID from session
            .check(...))
    

    那么你的场景可能看起来像这样:

    val scn = scenario("auth with caps").exec(auths, caps) // rest omitted 
    

    也许更好的构建代码的方法是使用对象。请参阅提到的教程链接。

    更多说明性示例(可以编译,但我没有在域为 foo.com 时运行它):

    import io.gatling.core.Predef._
    import io.gatling.http.Predef._
    
    class ExampleSimulation extends Simulation {
    
      import scala.util.Random
      import scala.concurrent.duration._
    
      val httpConf = http.baseURL(s"http://foo.com")
    
      val emails = Iterator.continually(Map("email" -> (Random.alphanumeric.take(20).mkString + "@foo.com")))
      val names = Iterator.continually(Map("name" -> Random.alphanumeric.take(20).mkString))
    
      val getIdByEmail = feed(emails)
        .exec(
          http("Get By Email")
            .get("/email/$email")
            .check(
              jsonPath("userId").saveAs("anId")
            )
        )
    
      val getIdByName = feed(names)
        .exec(
          http("Get By Name")
            .get("/name/$name")
            .check(
              jsonPath("userId").is(session =>
                session("anId").as[String]
              )
            )
        )
    
      val scn = scenario("Get and check user id").exec(getIdByEmail, getIdByName).inject(constantUsersPerSec(5) during (5.minutes))
    
      setUp(scn).protocols(httpConf)
    }
    

    希望它是您想要的。

    【讨论】:

    • 不完全是。要求是将这两个场景完全分开,以查看“auths”的性能与“caps”的性能。使用建议的解决方案,它将在其基础上执行一个身份验证和一个上限,因此两者结合起来。我当然可以将第一个场景中的 id 存储到某个文件中,并根据该文件为第二个场景定义一个进纸器,但这并不好,我想知道我是否可以通过现有的 galting 机制来实现它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多