簡體   English   中英

通過擴展蓋特林來定制DSL:如何擴展Scala?

[英]Custom DSL by extending Gatling: How to Scala?

我們正在使用加特林(Gatling)對應用程序進行負載測試(並且效果很好)。 我們正在嘗試通過對Gatling類進行可組合的擴展來干燥某些代碼(例如io.gatling.core.structure找到的ScenarioBuilder / ChainBuilder /等)。

這是我們其中一種情況的示例:

val scn = scenario("Whatever")
  .exec(Authentication.FeederLogin(userCsvFile, password))
  .exec(User.ExtractId(User.SignIn()))
  .exec(FindPeople(50, "personIds"))

  // SPA load
  .exec(User.FetchLandingPageFor("${userId}"))

  .during(durationSeconds.seconds) {
    pace(3.seconds, 8.seconds)
      .exec(Person.Search("${personIds.random()}"))
      .pause(3.seconds, 10.seconds)

      // start the upload
      .exec(Upload.create())
  }

我們想要是開始做一些組合的,所以我們可以在其他情況下再使用它們。 像這樣:

val scn = scenario("Whatever")
  .login()

  .during(durationSeconds.seconds) {
    pace(3.seconds, 8.seconds)
      .uploadFor("${personIds.random()}")
  }

// ...

object WhateverScenarios {
  implicit class ScenarioBuilderWithWhatevers(b: ScenarioBuilder) {

    def login() : ScenarioBuilder = {
      b.exec(Authentication.FeederLogin(userCsvFile, password))
      .exec(User.ExtractId(User.SignIn()))
      .exec(FindPeople(50, "personIds"))
    }

    def uploadFor(whom : String) : ScenarioBuilder {
      b.exec(Person.Search("${personIds.random()}"))
      .pause(3.seconds, 10.seconds)

      // start the upload
      .exec(Upload.create())
    }
  }
}

全面披露; 我對Scala不太熟悉。 這行得通,但是問題出在uploadFor上,因為那時它正在使用ChainBuilderScenarioBuilder

我想

哦,簡單! 只需使用泛型!

除非我無法使它起作用:( 看起來其中大多數extend StructureBuilder[T]但似乎無法獲得定義的通用定義,在哪里可以在StructureBuilder[T] 任何上下文中使用WhateverScenarios

預先感謝您可以提供的任何信息。

因此,我能夠解決我的問題,但是我不確定為什么要這樣做。 解決我的問題的方法如下:

object WhateverScenarios {
  implicit class StructureBuilderWithWhatevers[B <: io.gatling.core.structure.StructureBuilder[B]](b: B) {

    def login() : B = {
      b.exec(Authentication.FeederLogin(userCsvFile, password))
      .exec(User.ExtractId(User.SignIn()))
      .exec(FindPeople(50, "personIds"))
    }

    def uploadFor(whom : String) : ScenarioBuilder {
      b.exec(Person.Search("${personIds.random()}"))
      .pause(3.seconds, 10.seconds)

      // start the upload
      .exec(Upload.create())
    }
  }
}

最初使我陷入困境的東西是這樣定義的:

object WhateverScenarios {
  implicit class StructureBuilderWithWhatevers[B <: StructureBuilder[B]](b: B) {
  // ...
}

但是,當我這樣做時,它不會將loginuploadFor識別為ScenarioBuilderChainBuilder 不知道為什么,但是我指定了完全合格的軟件包名稱來使它工作¯\\ _(ツ)_ /

編輯

事實證明,完全合格的軟件包名稱是我在計算機上擁有的一些奇怪的工件。 進行./gradlew clean解決此問題,並且我不再需要使用完全限定的路徑名​​。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM