简体   繁体   English

通过扩展盖特林来定制DSL:如何扩展Scala?

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

We're using Gatling to load test our application (and it works great). 我们正在使用加特林(Gatling)对应用程序进行负载测试(并且效果很好)。 We're attempting to DRY up some of the code by making composable extensions on the Gatling classes (like ScenarioBuilder / ChainBuilder / etc. found in io.gatling.core.structure ). 我们正在尝试通过对Gatling类进行可组合的扩展来干燥某些代码(例如io.gatling.core.structure找到的ScenarioBuilder / ChainBuilder /等)。

Here is an example of one of our scenarios: 这是我们其中一种情况的示例:

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())
  }

What we'd like to do is start to make some of that composable so we can re-use them in other scenarios. 我们想要是开始做一些组合的,所以我们可以在其他情况下再使用它们。 Something like this: 像这样:

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())
    }
  }
}

Full disclosure; 全面披露; I'm not super familiar with Scala. 我对Scala不太熟悉。 This works, but the problem is in the uploadFor in that at that point it's working with a ChainBuilder vs. a ScenarioBuilder . 这行得通,但是问题出在uploadFor上,因为那时它正在使用ChainBuilderScenarioBuilder

I thought 我想

Oh, simple! 哦,简单! Just use generics! 只需使用泛型!

Except I cannot get it to work :( It looks like most of these extend StructureBuilder[T] but I cannot seem to get a generic definition defined where I can use my WhateverScenarios in any context of a StructureBuilder[T] . 除非我无法使它起作用:( 看起来其中大多数extend StructureBuilder[T]但似乎无法获得定义的通用定义,在哪里可以在StructureBuilder[T] 任何上下文中使用WhateverScenarios

Thanks in advance for any information that can be provided. 预先感谢您可以提供的任何信息。

So I was able to get past my issue, but I'm not entirely sure why I had to do this. 因此,我能够解决我的问题,但是我不确定为什么要这样做。 What fixed my issue is the following: 解决我的问题的方法如下:

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())
    }
  }
}

The thing that was throwing me for a loop, was originally I tried to define it like this: 最初使我陷入困境的东西是这样定义的:

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

But when I did that, it didn't recognize login or uploadFor as being part of either ScenarioBuilder or ChainBuilder . 但是,当我这样做时,它不会将loginuploadFor识别为ScenarioBuilderChainBuilder Not sure why, but I had the specify the fully qualified package name for it to work ¯\\_(ツ)_/¯ 不知道为什么,但是我指定了完全合格的软件包名称来使它工作¯\\ _(ツ)_ /

Edit 编辑

Turns out that the fully qualified package name was some weird artifact that I had on my machine. 事实证明,完全合格的软件包名称是我在计算机上拥有的一些奇怪的工件。 Doing a ./gradlew clean fixed the issue and I no longer have to use the fully qualified path name. 进行./gradlew clean解决此问题,并且我不再需要使用完全限定的路径名​​。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM