【问题标题】:Iterating through an array in Scala遍历Scala中的数组
【发布时间】:2020-08-04 23:38:16
【问题描述】:

我是 Scala 新手,正在尝试使用 Gatling 编写一些用于负载测试的脚本。 我正在尝试定义一个可重用的方法来发送负载,因此我创建了以下方法:

def startLoad(scenario: Array[ScenarioBuilder]) = {
        setUp(
            scnGetAuthorizationToken.inject(
              atOnceUsers(1)
              ),
            
        for (i <- 0 until scenario.length) {
            scenario(i).inject(
               nothingFor(5 seconds),
               atOnceUsers(atOnceUserCount)
               ).throttle(
                   reachRps(maxRps/2) in (testDuration/4 seconds),
                   reachRps(maxRps/2) in (testDuration/4 seconds),
                   reachRps(maxRps) in (testDuration/2 seconds)),
             }
            ).protocols(httpConf.inferHtmlResources())
             .maxDuration(testDuration seconds)
             .assertions(
                 global.responseTime.max.lt(maxResponseTime),
                 global.successfulRequests.percent.gt(successfulRequests)
                 )
        }

但是,编译器在 for 语句中抛出错误。我在使用“foreach”时也遇到了同样的错误:

type mismatch; found : Unit required: io.gatling.core.structure.PopulationBuilder

有人可以帮我解决这个错误吗?

一般来说,我希望这段代码采用以下格式(在 ScenarioBuilder 类型的不同变量上调用几行代码),因此试图提出一个如上所述的可重用方法:

def startLoad(scenario: Array[ScenarioBuilder]) = {
        setUp(
            scnGetAuthorizationToken.inject(
              atOnceUsers(1)
              ),
            
            scenario1.inject(
               nothingFor(5 seconds),
               atOnceUsers(atOnceUserCount)
               ).throttle(
                   reachRps(maxRps/2) in (testDuration/4 seconds),
                   reachRps(maxRps/2) in (testDuration/4 seconds),
                   reachRps(maxRps) in (testDuration/2 seconds)),
             
            scenario2.inject(
               nothingFor(5 seconds),
               atOnceUsers(atOnceUserCount)
               ).throttle(
                   reachRps(maxRps/2) in (testDuration/4 seconds),
                   reachRps(maxRps/2) in (testDuration/4 seconds),
                   reachRps(maxRps) in (testDuration/2 seconds)),
                   
            scenario3.inject(
               nothingFor(5 seconds),
               atOnceUsers(atOnceUserCount)
               ).throttle(
                   reachRps(maxRps/2) in (testDuration/4 seconds),
                   reachRps(maxRps/2) in (testDuration/4 seconds),
                   reachRps(maxRps) in (testDuration/2 seconds)),
           ------        

            ).protocols(httpConf.inferHtmlResources())
             .maxDuration(testDuration seconds)
             .assertions(
                 global.responseTime.max.lt(maxResponseTime),
                 global.successfulRequests.percent.gt(successfulRequests)
                 )
        }

所以,基本上,我想为作为参数传递给方法的数组的每个元素重复下面的代码行。

             inject(
                   nothingFor(5 seconds),
                   atOnceUsers(atOnceUserCount)
                   ).throttle(
                       reachRps(maxRps/2) in (testDuration/4 seconds),
                       reachRps(maxRps/2) in (testDuration/4 seconds),
                       reachRps(maxRps) in (testDuration/2 seconds)),

【问题讨论】:

  • 这段代码非常难读,因为缩进全乱了。如果您可以编辑您的问题以包含正确缩进的代码,那么有人会更容易帮助您。

标签: scala gatling scala-gatling


【解决方案1】:

Gatling 不直接执行模拟类代码。将其视为由框架读取的测试配置的构建器。

每个模拟类只能调用一次方法setUp。如果您想使用相同的注入配置文件、节流和断言创建 3 个不同的场景,最好的方法是创建一个定义了所有这些设置的抽象类,并在其他 3 个中扩展该类,例如:

import io.gatling.core.Predef.{Simulation, _}
import io.gatling.core.structure.ScenarioBuilder
import io.gatling.http.Predef.{http, status, _}

abstract class BaseScenario(scenario: ScenarioBuilder) extends Simulation{
  setUp(scenario.inject(
    atOnceUsers(5))
  ).protocols(
    http.shareConnections
  ).assertions(
    global.failedRequests.percent.lte(1)
  )
}

class ScenarioA extends BaseScenario(
  scenario("Example Scenario A")
    .exec(
      http("Get A")
        .get("http://aaaa.com")
        .check(status.is(200))
    )
)

class ScenarioB extends BaseScenario(
  scenario("Example Scenario A")
    .exec(
      http("Get B")
        .get("http://bbbb.com")
        .check(status.is(200))
    )
)

【讨论】:

    猜你喜欢
    • 2016-12-23
    • 1970-01-01
    • 2012-12-03
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    • 2014-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多