Gatling – 基于配置的动态场景、注入配置和断言创建

huangapple go评论72阅读模式
英文:

Gatling - Dynamic Scenario, InjectionProfile, Assertion creation based on Configuration

问题

我正试图编写一个模拟程序,它可以从配置文件中读取一组具有一组属性的API。以下是您提供的代码的翻译部分:

  1. // 从配置文件获取活动场景
  2. val activeApiScenarios: List[String] = Utils.getStringListProperty("my.active_scenarios")
  3. // 从配置文件构建所有活动场景
  4. var activeScenarios: Set[CommonScenario] = Set[CommonScenario]()
  5. activeApiScenarios.foreach { scenario =>
  6. activeScenarios += CommonScenarioBuilder()
  7. .withRequestName(Utils.getProperty("my." + scenario + ".request_name"))
  8. .withRegion(Utils.getProperty("my." + scenario + ".region"))
  9. .withConstQps(Utils.getDoubleProperty("my." + scenario + ".const_qps"))
  10. .withStartQps(Utils.getDoubleListProperty("my." + scenario + ".toth_qps").head)
  11. .withPeakQps(Utils.getDoubleListProperty("my." + scenario + ".toth_qps")(1))
  12. .withEndQps(Utils.getDoubleListProperty("my." + scenario + ".toth_qps")(2))
  13. .withFeeder(Utils.getProperty("my." + scenario + ".feeder"))
  14. .withAssertionP99(Utils.getDoubleProperty("my." + scenario + ".p99_lte_assertion"))
  15. .build
  16. }
  17. // 通过添加注入配置值构建人口生成器集合
  18. var injectScenarios: Set[PopulationBuilder] = Set[PopulationBuilder]()
  19. var assertions : Set[Assertion] = Set[Assertion]()
  20. activeScenarios.foreach { scenario =>
  21. // 从CommonScenarios创建注入配置
  22. injectScenarios += scenario.getCommonScenarioBuilder
  23. .inject(nothingFor(5 seconds),
  24. rampUsersPerSec(scenario.startQps).to(scenario.rampUpQps).during(rampOne seconds),
  25. rampUsersPerSec(scenario.rampUpQps).to(scenario.peakQps).during(rampTwo seconds),
  26. rampUsersPerSec(scenario.peakQps).to(scenario.rampDownQps) during (rampTwo seconds),
  27. rampUsersPerSec(scenario.rampDownQps).to(scenario.endQps).during(rampOne seconds)).protocols(httpProtocol)
  28. // 创建场景断言,但是存在问题
  29. assertions += Assertion(Details(List(scenario.requestName)), TimeTarget(ResponseTime, Percentiles(4)), Lte(scenario.assertionP99))
  30. }
  31. setUp(injectScenarios.toList)
  32. .assertions(assertions)

注意,您的代码中有一些HTML转义字符(例如"),在实际代码中应该是双引号"。此外,您的代码似乎在模拟测试方面有一些问题,可能需要进行一些调试和修复。希望这个翻译有助于您更好地理解您的代码。

英文:

I am attempting to write a simulation that can read from a config file for a set of apis that each have a set of properties.

  • I read the config for n active scenarios and create requests from a CommonRequest class
  • Then those requests are built into scenarios from a CommonScenario
  • CommonScenarios have attributes that are using to create their injection profiles

That all seems to work no issue. But when I try to use the properties / CommonScenario request to build a set of Assertions it does not work as expected.

  1. // get active scenarios from the config
  2. val activeApiScenarios: List[String] = Utils.getStringListProperty("my.active_scenarios")
  3. // build all active scenarios from config
  4. var activeScenarios: Set[CommonScenario] = Set[CommonScenario]()
  5. activeApiScenarios.foreach { scenario =>
  6. activeScenarios += CommonScenarioBuilder()
  7. .withRequestName(Utils.getProperty("my." + scenario + ".request_name"))
  8. .withRegion(Utils.getProperty("my." + scenario + ".region"))
  9. .withConstQps(Utils.getDoubleProperty("my." + scenario + ".const_qps"))
  10. .withStartQps(Utils.getDoubleListProperty("my." + scenario + ".toth_qps").head)
  11. .withPeakQps(Utils.getDoubleListProperty("my." + scenario + ".toth_qps")(1))
  12. .withEndQps(Utils.getDoubleListProperty("my." + scenario + ".toth_qps")(2))
  13. .withFeeder(Utils.getProperty("my." + scenario + ".feeder"))
  14. .withAssertionP99(Utils.getDoubleProperty("my." + scenario + ".p99_lte_assertion"))
  15. .build
  16. }
  17. // build population builder set by adding inject profile values to scenarios
  18. var injectScenarios: Set[PopulationBuilder] = Set[PopulationBuilder]()
  19. var assertions : Set[Assertion] = Set[Assertion]()
  20. activeScenarios.foreach { scenario =>
  21. // create injection profiles from CommonScenarios
  22. injectScenarios += scenario.getCommonScenarioBuilder
  23. .inject(nothingFor(5 seconds),
  24. rampUsersPerSec(scenario.startQps).to(scenario.rampUpQps).during(rampOne seconds),
  25. rampUsersPerSec(scenario.rampUpQps).to(scenario.peakQps).during(rampTwo seconds),
  26. rampUsersPerSec(scenario.peakQps).to(scenario.rampDownQps) during (rampTwo seconds),
  27. rampUsersPerSec(scenario.rampDownQps).to(scenario.endQps).during(rampOne seconds)).protocols(httpProtocol)
  28. // create scenario assertions this does not work for some reason
  29. assertions += Assertion(Details(List(scenario.requestName)), TimeTarget(ResponseTime, Percentiles(4)), Lte(scenario.assertionP99))
  30. }
  31. setUp(injectScenarios.toList)
  32. .assertions(assertions)

Note scenario.requestName is straight from the build scenario

  1. .feed(feederBuilder)
  2. .exec(commonRequest)

I would expect the Assertions get built from their scenarios into an iterable and pass into setUp().

What I get:

When I print out everything the scenarios, injects all look good but then I print my "assertions" and get 4 assertions for the same scenario name with 4 different Lte() values. This is generalized but I configured 12 apis all with different names and Lte() values, etc.

Details(List(Request Name)) - TimeTarget(ResponseTime,Percentiles(4.0)) - Lte(500.0)
Details(List(Request Name)) - TimeTarget(ResponseTime,Percentiles(4.0)) - Lte(1500.0)
Details(List(Request Name)) - TimeTarget(ResponseTime,Percentiles(4.0)) - Lte(1000.0)
Details(List(Request Name)) - TimeTarget(ResponseTime,Percentiles(4.0)) - Lte(2000.0)

After the simulation the assertions all run like normal:

Request Name: 4th percentile of response time is less than or equal to 500.0 : false
Request Name: 4th percentile of response time is less than or equal to 1500.0 : false
Request Name: 4th percentile of response time is less than or equal to 1000.0 : false
Request Name: 4th percentile of response time is less than or equal to 2000.0 : false

Not sure what I am doing wrong when building my assertions. Is this even a valid approach? I wanted to ask for help before I abandon this for a different approach.

答案1

得分: 0

免责声明:Gatling的创建者在这里。

应该可以工作。

然后,有几件事情我非常不喜欢。

assertions += Assertion(Details(List(scenario.requestName)), TimeTarget(ResponseTime, Percentiles(4)), Lte(scenario.assertionP99))

你不应该在这里使用内部AST。你应该像你对注入配置文件所做的那样使用DSL。

var assertions: Set[Assertion] = SetAssertion
;
activeScenarios.foreach { scenario =>

你应该在activeScenarios上使用map(类似于Java的Stream API),而不是使用可变的累加器。

  1. val activeScenarios = activeApiScenarios.map(???)
  2. val injectScenarios = activeScenarios.map(???)
  3. val assertions = activeScenarios.map(???)

另外,由于你似乎不熟悉Scala,也许应该考虑切换到Java(支持超过1年)。

英文:

Disclaimer: Gatling creator here.

It should work.

Then, there are several things I'm super not found of.

> assertions += Assertion(Details(List(scenario.requestName)), TimeTarget(ResponseTime, Percentiles(4)), Lte(scenario.assertionP99))

You shouldn't be using the internal AST here. You should use the DSL like you've done for the injection profile.

> var assertions : Set[Assertion] = SetAssertion
>
> activeScenarios.foreach { scenario =>

You should use map on activeScenarios (similar to Java's Stream API), not use a mutable accumulator.

  1. val activeScenarios = activeApiScenarios.map(???)
  2. val injectScenarios = activeScenarios.map(???)
  3. val assertions = activeScenarios.map(???)

Also, as you seem to not be familiar with Scala, you should maybe switch to Java (supported for more than 1 year).

huangapple
  • 本文由 发表于 2023年2月10日 05:32:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404620.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定