CMM
CMM

Reputation: 573

Iterating through an array in Scala

I'm new to Scala, and trying to write few scripts for Load testing using Gatling. I'm trying to define a re-usable method to send the load, hence I created below method:

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

But, compiler is throwing below error for the for statement. I got same error when I used "foreach" also:

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

Can someone please help me how to get rid off this error?

In general, I would like to have this code in the following format (few lines of code is called on different variables of ScenarioBuilder type), and hence trying to come up with a re-usable method as defined above:

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

So, basically, I would like to repeat below lines of code for every element of the array that is passed as an argument to the method.

             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)),

Upvotes: 1

Views: 284

Answers (1)

Mateusz Gruszczynski
Mateusz Gruszczynski

Reputation: 1500

Gatling is not executing Simulation classes code directly. Think of it as a builder for a test configuration that is read by a framework.

Method setUp can be called only once per Simulation class. If you want to create 3 different scenarios with the same injection profile, throttle and assertions the best way will be to create an abstract class with all those settings defined and extend that class in 3 other ones like:

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

Upvotes: 2

Related Questions