Run multiple scenarios based on a condition

Hello All, I’m starting using Gatling as well as Scala. I have a project running multiple scenarios and it is working fine, now the requirement extended to run only the passed scenarios via command prompt. By now my working file is:

class SimulationAllScenarios extends Simulation {
var conf = ConfigFactory.load()
var environment: String = System.getProperty(“env”)
environment = environment.toLowerCase()
var baseUrl = conf.getString(environment.concat(“.baseUrl”))

var scenarioConf = ConfigFactory.load(“scenarioProperties.conf”)

val httpsProtocol = http.baseURL(baseUrl).disableFollowRedirect

setUp(PageOneScenario.scn.inject(ramp(scenarioConf.getInt(“PageOne.Users”)) over (scenarioConf.getInt(“PageOne.RampUpTime”))),
PageTwoScenario.scn.inject(ramp(scenarioConf.getInt(“PageTwo.Users”)) over (scenarioConf.getInt(“PageTwo.RampUpTime”)))
PageThreeScenario.scn.inject(ramp(scenarioConf.getInt(“PageThree.Users”)) over (scenarioConf.getInt(“PageThree.RampUpTime”)))
)
.protocols(httpsProtocol)
}

Now I want to modify the class to run optionally the scenarios depending on the system property variable, for example
mvn clean install -Denv=QA -Dscenario=PageTwo,PageThree
This command should run only the scenario two. I know that I can run another simulation but I have 18 different scenarios and most of the times I will run the 18 scenarios, but sometimes I have to run 5 or 6 scenarios at the same time.

Is this possibly or I’m dreaming? I saw that setUp parameters are scenario: ProfiledScenarioBuilder, scenarios: ProfiledScenarioBuilder* so the second parameter is like an Array in Java? because I tried to pass an array and it’s not working. in Java we have the … so I thought it was the same.

Thank you in advance

ProfiledScenarios are not run sequentially, but concurrently. It’s the way to have different populations with different behaviors inside the same simulation.
Would that meet your needs?

Yes I know they run concurrently and that is fine, something I need in pseudocode:
mvn clean install -Denv=QA -Dscenario=PageTwo,PageThree

Simulation class:
read properties for env
create scenarios based on the scenario system property
setUp(with the created scenarios) This will run such scenarios concurrently
done

I found what I need, I will create an Array with the selected scenarios and pass to the setUp function with the _* operator

Thank you for the support

Exactly, I was about to reply.

You’re a quick learner :slight_smile:
Have fun!

Hi Stephane,

I’m a beginner at both Scala and Gatling. I’m currently using Gatling 2.0.0-M3a, and I’m very interested in the approach of passing test case as a command line parameter as suggested by Daniel Ortiz. Based on the instructions provided in this thread, I made an attempt to use the splat operator (provided below), but got stuck at the compiler error, even after some googling.

Could you please provide some guidance?

Thank you much.

Vu

setUp signature was too strict in 2M3 and wouldn’t allow you to do that directly (so we changed it in upcoming version).

use:

val scns = httpScenarios(tests)

setUp(scns.head, scns.tail:_*).protocols(httpProtocol)

I’d never thought of such solution.

Thank you very much, Stephane for your help.

Vu

Hi Stephen,

I am trying to create a scenario with step up load. I created a map to contain my steps. However, I am getting the “no :_* annotation allowed here” error. Can you advise how to apply your solution in this case?

val Params = 1 to pNumSteps map { i =>
workload(pPacing, (pNumSteps - i + 3) * (pRampTime+pStepTime))
.inject(
nothingFor((i-1) * (pRampTime+pStepTime) seconds),
rampUsers(pVusers) over (pRampTime seconds),
nothingFor(pStepTime seconds))
}

setUp(scenario(“Nothing”).inject(nothingFor(1 seconds)),
Params:_*)
.protocols(httpProtocol)
.maxDuration(pTestDuration seconds)

This answer was for an older Gatling version. setUp now takes only one parameter: either an Iterable or a varargs. Concatenate your first nothingFor and your Params into one single Iterable.

Thanks Stephane.

I have got rid of the nothingFor and it works. Will do the concatenation now.

setUp(ionStepParams:_*)

Thanks for your quick response as always.

So this doesn’t work?

setUp(ionStepParams:*, pomStepParams:*)

both args have step up load in it. I want to run the steps of both in parallel.

This answer was for an older Gatling version. setUp now takes only one

parameter: either an `Iterable` or a varargs. Concatenate your first
nothingFor and your Params into one single Iterable.

Sorry, I meant "inject" here.

Sorry, I am still bit confused. An example would be easier to understand? May be if you can modify the one I sent, would be great.

Get a look at the code:
https://github.com/gatling/gatling/blob/master/gatling-core/src/main/scala/io/gatling/core/structure/ScenarioBuilder.scala#L42-L44

https://github.com/gatling/gatling/blob/master/gatling-core/src/main/scala/io/gatling/core/scenario/Simulation.scala#L45-L47

Referring to this (http://gatling.io/docs/2.2.2/advanced_tutorial.html?highlight=simulations#step-02-configure-virtual-users)

the inject functions look correct.

.inject( nothingFor((i-1) * (pRampTime+pStepTime) seconds), rampUsers(pVusers) over (pRampTime seconds), nothingFor(pStepTime seconds))

and passing multiple such ‘inject’ to setUp using

` setUp(ionStepParams:_*, pomStepParams:_*) `

So if setUp(ionStepParams:_*) works, the above should also work.

` setUp(ionStepParams:_*, pomStepParams:_*) `

So if setUp(ionStepParams:_*) works, the above should also work.

Should not

  1. You’re passing TWO varargs to setUp. According to Stephane (and codes), setUp now takes only ONE parameter: either an Iterable or a varargs
    https://github.com/gatling/gatling/blob/master/gatling-core/src/main/scala/io/gatling/core/scenario/Simulation.scala#L45-L47

  2. A method has a field that can contain a variable number of arguments, the varargs field must be the last field in the method signature.
    Scala: How to create methods that take variable-arguments (varargs) fields | alvinalexander.com

Ok its clear now. My exposure to scala is only as much as my exposure to Gatling :slight_smile:
Having multiple varargs would be good have. I am not sure whether the simulation setUp I have is common or not. I am clueless to implement it any other way.

Thanks for the second link. That cleared the my doubts. Have a good day.

Having multiple varargs would be good have. I am not sure whether the simulation setUp I have is common or not. I am clueless to implement it any other way.

I have to take this statement back. I was able to create a single varargs to cater for my both scenarios. :-). Simpler than I thought.

`

var j = 0
var k = 0
val Params = 1 to pNumSteps map { i => {
if(i%2==0){
k = k + 1
workloadType1(k, pPacing, ((pNumSteps - k + 3) * (pRampTime+pStepTime)))
.inject(
nothingFor((k-1) * (pRampTime+pStepTime) seconds),
rampUsers(pVusers) over (pRampTime seconds),
nothingFor(pStepTime seconds))
}
else {
j = j + 1
workloadType2(j, pPacing, ((pNumSteps - j + 3) * (pRampTime+pStepTime)))
.inject(
nothingFor((j-1) * (pRampTime+pStepTime) seconds),
rampUsers(pVusers) over (pRampTime seconds),
nothingFor(pStepTime seconds))
}
}
}

`